1 /*
   2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.jmx.snmp.agent;
  27 
  28 import java.util.Enumeration;
  29 import java.util.Vector;
  30 
  31 
  32 import com.sun.jmx.snmp.SnmpPdu;
  33 import com.sun.jmx.snmp.SnmpVarBind;
  34 import com.sun.jmx.snmp.SnmpEngine;
  35 
  36 /**
  37  * This class implements the SnmpMibRequest interface.
  38  * It represents the part of a SNMP request that involves a specific
  39  * MIB. One instance of this class will be created for every MIB
  40  * involved in a SNMP request, and will be passed to the SnmpMibAgent
  41  * in charge of handling that MIB.
  42  *
  43  * Instances of this class are allocated by the SNMP engine. You will
  44  * never need to use this class directly. You will only access
  45  * instances of this class through their SnmpMibRequest interface.
  46  *
  47  */
  48 final class SnmpMibRequestImpl implements SnmpMibRequest {
  49 
  50     /**
  51      * @param engine The local engine.
  52      * @param reqPdu The received pdu.
  53      * @param vblist The vector of SnmpVarBind objects in which the
  54      *        MIB concerned by this request is involved.
  55      * @param protocolVersion  The protocol version of the SNMP request.
  56      * @param userData     User allocated contextual data. This object must
  57      *        be allocated on a per SNMP request basis through the
  58      *        SnmpUserDataFactory registered with the SnmpAdaptorServer,
  59      *        and is handed back to the user through SnmpMibRequest objects.
  60      */
  61     public SnmpMibRequestImpl(SnmpEngine engine,
  62                               SnmpPdu reqPdu,
  63                               Vector<SnmpVarBind> vblist,
  64                               int protocolVersion,
  65                               Object userData,
  66                               String principal,
  67                               int securityLevel,
  68                               int securityModel,
  69                               byte[] contextName,
  70                               byte[] accessContextName) {
  71         varbinds   = vblist;
  72         version    = protocolVersion;
  73         data       = userData;
  74         this.reqPdu = reqPdu;
  75         this.engine = engine;
  76         this.principal = principal;
  77         this.securityLevel = securityLevel;
  78         this.securityModel = securityModel;
  79         this.contextName = contextName;
  80         this.accessContextName = accessContextName;
  81     }
  82     // -------------------------------------------------------------------
  83     // PUBLIC METHODS from SnmpMibRequest
  84     // -------------------------------------------------------------------
  85 
  86     /**
  87      * Returns the local engine. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
  88      * @return the local engine.
  89      */
  90     @Override
  91     public SnmpEngine getEngine() {
  92         return engine;
  93     }
  94 
  95     /**
  96      * Gets the incoming request principal. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
  97      * @return The request principal.
  98      **/
  99     @Override
 100     public String getPrincipal() {
 101         return principal;
 102     }
 103 
 104     /**
 105      * Gets the incoming request security level. This level is defined in {@link com.sun.jmx.snmp.SnmpEngine SnmpEngine}. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
 106      * @return The security level.
 107      */
 108     @Override
 109     public int getSecurityLevel() {
 110         return securityLevel;
 111     }
 112     /**
 113      * Gets the incoming request security model. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise -1 is returned.
 114      * @return The security model.
 115      */
 116     @Override
 117     public int getSecurityModel() {
 118         return securityModel;
 119     }
 120     /**
 121      * Gets the incoming request context name. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
 122      * @return The context name.
 123      */
 124     @Override
 125     public byte[] getContextName() {
 126         return contextName;
 127     }
 128 
 129     /**
 130      * Gets the incoming request context name used by Access Control Model in order to allow or deny the access to OIDs. This parameter is returned only if <CODE> SnmpV3AdaptorServer </CODE> is the adaptor receiving this request. Otherwise null is returned.
 131      * @return The checked context.
 132      */
 133     @Override
 134     public byte[] getAccessContextName() {
 135         return accessContextName;
 136     }
 137 
 138     // -------------------------------------------------------------------
 139     // Implements the method defined in SnmpMibRequest interface.
 140     // See SnmpMibRequest for the java doc.
 141     // -------------------------------------------------------------------
 142     @Override
 143     public final SnmpPdu getPdu() {
 144         return reqPdu;
 145     }
 146 
 147     // -------------------------------------------------------------------
 148     // Implements the method defined in SnmpMibRequest interface.
 149     // See SnmpMibRequest for the java doc.
 150     // -------------------------------------------------------------------
 151     @Override
 152     public final Enumeration<SnmpVarBind> getElements()  {return varbinds.elements();}
 153 
 154     // -------------------------------------------------------------------
 155     // Implements the method defined in SnmpMibRequest interface.
 156     // See SnmpMibRequest for the java doc.
 157     // -------------------------------------------------------------------
 158     @Override
 159     public final Vector<SnmpVarBind> getSubList()  {return varbinds;}
 160 
 161     // -------------------------------------------------------------------
 162     // Implements the method defined in SnmpMibRequest interface.
 163     // See SnmpMibRequest for the java doc.
 164     // -------------------------------------------------------------------
 165     @Override
 166     public final int getSize()  {
 167         if (varbinds == null) return 0;
 168         return varbinds.size();
 169     }
 170 
 171     // -------------------------------------------------------------------
 172     // Implements the method defined in SnmpMibRequest interface.
 173     // See SnmpMibRequest for the java doc.
 174     // -------------------------------------------------------------------
 175     @Override
 176     public final int         getVersion()  {return version;}
 177 
 178     // -------------------------------------------------------------------
 179     // Implements the method defined in SnmpMibRequest interface.
 180     // See SnmpMibRequest for the java doc.
 181     // -------------------------------------------------------------------
 182     @Override
 183     public final int         getRequestPduVersion()  {return reqPdu.version;}
 184 
 185     // -------------------------------------------------------------------
 186     // Implements the method defined in SnmpMibRequest interface.
 187     // See SnmpMibRequest for the java doc.
 188     // -------------------------------------------------------------------
 189     @Override
 190     public final Object      getUserData() {return data;}
 191 
 192     // -------------------------------------------------------------------
 193     // Implements the method defined in SnmpMibRequest interface.
 194     // See SnmpMibRequest for the java doc.
 195     // -------------------------------------------------------------------
 196     @Override
 197     public final int getVarIndex(SnmpVarBind varbind) {
 198         return varbinds.indexOf(varbind);
 199     }
 200 
 201     // -------------------------------------------------------------------
 202     // Implements the method defined in SnmpMibRequest interface.
 203     // See SnmpMibRequest for the java doc.
 204     // -------------------------------------------------------------------
 205     @Override
 206     public void addVarBind(SnmpVarBind varbind) {
 207         varbinds.addElement(varbind);
 208     }
 209 
 210     // -------------------------------------------------------------------
 211     // PACKAGE METHODS
 212     // -------------------------------------------------------------------
 213 
 214     // -------------------------------------------------------------------
 215     // Allow to pass the request tree built during the check() phase
 216     // to the set() method. Note: the if the tree is `null', then the
 217     // set() method will rebuild a new tree identical to the tree built
 218     // in the check() method.
 219     //
 220     // Passing this tree in the SnmpMibRequestImpl object allows to
 221     // optimize the SET requests.
 222     //
 223     // -------------------------------------------------------------------
 224     final void setRequestTree(SnmpRequestTree tree) {this.tree = tree;}
 225 
 226     // -------------------------------------------------------------------
 227     // Returns the SnmpRequestTree object built in the first operation
 228     // phase for two-phase SNMP requests (like SET).
 229     // -------------------------------------------------------------------
 230     final SnmpRequestTree getRequestTree() {return tree;}
 231 
 232     // -------------------------------------------------------------------
 233     // Returns the underlying vector of SNMP varbinds (used for algorithm
 234     // optimization).
 235     // -------------------------------------------------------------------
 236     final Vector<SnmpVarBind> getVarbinds() {return varbinds;}
 237 
 238     // -------------------------------------------------------------------
 239     // Private variables
 240     // -------------------------------------------------------------------
 241 
 242     // Ideally these variables should be declared final but it makes
 243     // the jdk1.1.x compiler complain (seems to be a compiler bug, jdk1.2
 244     // is OK).
 245     private Vector<SnmpVarBind> varbinds;
 246     private int    version;
 247     private Object data;
 248     private SnmpPdu reqPdu = null;
 249     // Non final variable.
 250     private SnmpRequestTree tree = null;
 251     private SnmpEngine engine = null;
 252     private String principal = null;
 253     private int securityLevel = -1;
 254     private int securityModel = -1;
 255     private byte[] contextName = null;
 256     private byte[] accessContextName = null;
 257 }