View Javadoc

1   /**
2    * This file is part of GoldenGate Project (named also GoldenGate or GG).
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author
5    * tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    * 
8    * All GoldenGate Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   * 
13   * GoldenGate is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * GoldenGate . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package goldengate.snmp;
21  
22  import goldengate.snmp.interf.GgInterfaceVariableFactory;
23  import goldengate.snmp.utils.GgDefaultVariableFactory;
24  import goldengate.snmp.utils.GgMORow;
25  import goldengate.snmp.utils.GgMOScalar;
26  
27  import org.snmp4j.agent.MOAccess;
28  import org.snmp4j.agent.mo.MOAccessImpl;
29  import org.snmp4j.smi.Counter32;
30  import org.snmp4j.smi.Counter64;
31  import org.snmp4j.smi.Gauge32;
32  import org.snmp4j.smi.Integer32;
33  import org.snmp4j.smi.IpAddress;
34  import org.snmp4j.smi.OID;
35  import org.snmp4j.smi.OctetString;
36  import org.snmp4j.smi.Opaque;
37  import org.snmp4j.smi.SMIConstants;
38  import org.snmp4j.smi.TimeTicks;
39  import org.snmp4j.smi.Variable;
40  
41  /**
42   * This class creates and returns ManagedObjects
43   * 
44   * @author Frederic Bregier
45   * 
46   */
47  public class GgMOFactory {
48      /**
49       * To be setup to default Factory to be used or kept as null for default one
50       */
51      public static GgInterfaceVariableFactory factory = null;
52  
53      /**
54       * Default one
55       */
56      private static GgInterfaceVariableFactory defaultFactory = new GgDefaultVariableFactory();
57  
58      /**
59       * 
60       * @param oid
61       * @param value
62       * @param type
63       * @return an MOScalar according to the argument
64       */
65      public static GgMOScalar createReadOnly(OID oid, Object value, int type,
66              GgMORow row, int mibLevel, int entry) {
67          return new GgMOScalar(oid, MOAccessImpl.ACCESS_READ_ONLY, getVariable(
68                  oid, value, type, mibLevel, entry), row);
69      }
70  
71      /**
72       * 
73       * @param oid
74       * @param value
75       * @param type
76       * @param access
77       * @return an MOScalar according to the argument
78       */
79      public static GgMOScalar create(OID oid, Object value, int type,
80              MOAccess access, GgMORow row, int mibLevel, int entry) {
81          return new GgMOScalar(oid, access, getVariable(oid, value, type,
82                  mibLevel, entry), row);
83      }
84  
85      /**
86       * Create a Variable using the arguments
87       * 
88       * @param oid
89       * @param value
90       * @param type
91       * @param mibLevel
92       * @param entry
93       * @return a Variable using the arguments
94       */
95      public static Variable getVariable(OID oid, Object value, int type,
96              int mibLevel, int entry) {
97          Variable var = null;
98          GgInterfaceVariableFactory vf;
99          if (factory == null) {
100             vf = defaultFactory;
101         } else {
102             vf = factory;
103         }
104         var = vf.getVariable(oid, type, mibLevel, entry);
105         if (value != null) {
106             switch (type) {
107                 case SMIConstants.SYNTAX_INTEGER:
108                     // case SMIConstants.SYNTAX_INTEGER32:
109                     ((Integer32) var).setValue((Integer) value);
110                     break;
111                 case SMIConstants.SYNTAX_OCTET_STRING:
112                     // case SMIConstants.SYNTAX_BITS:
113                     ((OctetString) var).setValue(value.toString());
114                     break;
115                 case SMIConstants.SYNTAX_NULL:
116                     break;
117                 case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
118                     ((OID) var).setValue(value.toString());
119                     break;
120                 case SMIConstants.SYNTAX_IPADDRESS:
121                     ((IpAddress) var).setValue(value.toString());
122                     break;
123                 case SMIConstants.SYNTAX_COUNTER32:
124                     ((Counter32) var).setValue((Long) value);
125                     break;
126                 case SMIConstants.SYNTAX_GAUGE32:
127                     // case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
128                     ((Gauge32) var).setValue((Long) value);
129                     break;
130                 case SMIConstants.SYNTAX_TIMETICKS:
131                     if (value instanceof TimeTicks) {
132                         ((TimeTicks) var).setValue(((TimeTicks) value)
133                                 .toString());
134                     } else {
135                         ((TimeTicks) var).setValue((Long) value);
136                     }
137                     break;
138                 case SMIConstants.SYNTAX_OPAQUE:
139                     ((Opaque) var).setValue((byte[]) value);
140                     break;
141                 case SMIConstants.SYNTAX_COUNTER64:
142                     ((Counter64) var).setValue((Long) value);
143                     break;
144                 default:
145                     throw new IllegalArgumentException("Unmanaged Type: " +
146                             value.getClass());
147             }
148         }
149         return var;
150     }
151 
152     /**
153      * Set a Variable value
154      * 
155      * @param var
156      * @param value
157      * @param type
158      */
159     public static void setVariable(Variable var, Object value, int type) {
160         if (value != null) {
161             switch (type) {
162                 case SMIConstants.SYNTAX_INTEGER:
163                     // case SMIConstants.SYNTAX_INTEGER32:
164                     ((Integer32) var).setValue((Integer) value);
165                     break;
166                 case SMIConstants.SYNTAX_OCTET_STRING:
167                     // case SMIConstants.SYNTAX_BITS:
168                     ((OctetString) var).setValue(value.toString());
169                     break;
170                 case SMIConstants.SYNTAX_NULL:
171                     break;
172                 case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
173                     ((OID) var).setValue(value.toString());
174                     break;
175                 case SMIConstants.SYNTAX_IPADDRESS:
176                     ((IpAddress) var).setValue(value.toString());
177                     break;
178                 case SMIConstants.SYNTAX_COUNTER32:
179                     ((Counter32) var).setValue((Long) value);
180                     break;
181                 case SMIConstants.SYNTAX_GAUGE32:
182                     // case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
183                     ((Gauge32) var).setValue((Long) value);
184                     break;
185                 case SMIConstants.SYNTAX_TIMETICKS:
186                     if (value instanceof TimeTicks) {
187                         ((TimeTicks) var).setValue(((TimeTicks) value)
188                                 .toString());
189                     } else {
190                         ((TimeTicks) var).setValue((Long) value);
191                     }
192                     break;
193                 case SMIConstants.SYNTAX_OPAQUE:
194                     ((Opaque) var).setValue((byte[]) value);
195                     break;
196                 case SMIConstants.SYNTAX_COUNTER64:
197                     ((Counter64) var).setValue((Long) value);
198                     break;
199                 default:
200                     throw new IllegalArgumentException("Unmanaged Type: " +
201                             value.getClass());
202             }
203         }
204     }
205 }