View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, 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 Waarp 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   * Waarp 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   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.snmp;
21  
22  import org.snmp4j.agent.MOAccess;
23  import org.snmp4j.agent.mo.MOAccessImpl;
24  import org.snmp4j.smi.Counter32;
25  import org.snmp4j.smi.Counter64;
26  import org.snmp4j.smi.Gauge32;
27  import org.snmp4j.smi.Integer32;
28  import org.snmp4j.smi.IpAddress;
29  import org.snmp4j.smi.OID;
30  import org.snmp4j.smi.OctetString;
31  import org.snmp4j.smi.Opaque;
32  import org.snmp4j.smi.SMIConstants;
33  import org.snmp4j.smi.TimeTicks;
34  import org.snmp4j.smi.Variable;
35  import org.waarp.snmp.interf.WaarpInterfaceVariableFactory;
36  import org.waarp.snmp.utils.WaarpDefaultVariableFactory;
37  import org.waarp.snmp.utils.WaarpMORow;
38  import org.waarp.snmp.utils.WaarpMOScalar;
39  
40  /**
41   * This class creates and returns ManagedObjects
42   */
43  public final class WaarpMOFactory {
44    /**
45     * To be setup to default Factory to be used or kept as null for default one
46     */
47    private static WaarpInterfaceVariableFactory factory;
48  
49    /**
50     * Default one
51     */
52    private static final WaarpInterfaceVariableFactory defaultFactory =
53        new WaarpDefaultVariableFactory();
54  
55    private WaarpMOFactory() {
56    }
57  
58    /**
59     * @param oid
60     * @param value
61     * @param type
62     *
63     * @return an MOScalar according to the argument
64     */
65    public static WaarpMOScalar createReadOnly(final OID oid, final Object value,
66                                               final int type,
67                                               final WaarpMORow row,
68                                               final int mibLevel,
69                                               final int entry) {
70      return new WaarpMOScalar(oid, MOAccessImpl.ACCESS_READ_ONLY,
71                               getVariable(oid, value, type, mibLevel, entry),
72                               row);
73    }
74  
75    /**
76     * @param oid
77     * @param value
78     * @param type
79     * @param access
80     *
81     * @return an MOScalar according to the argument
82     */
83    public static WaarpMOScalar create(final OID oid, final Object value,
84                                       final int type, final MOAccess access,
85                                       final WaarpMORow row, final int mibLevel,
86                                       final int entry) {
87      return new WaarpMOScalar(oid, access,
88                               getVariable(oid, value, type, mibLevel, entry),
89                               row);
90    }
91  
92    /**
93     * Create a Variable using the arguments
94     *
95     * @param oid
96     * @param value
97     * @param type
98     * @param mibLevel
99     * @param entry
100    *
101    * @return a Variable using the arguments
102    */
103   public static Variable getVariable(final OID oid, final Object value,
104                                      final int type, final int mibLevel,
105                                      final int entry) {
106     final Variable var;
107     final WaarpInterfaceVariableFactory vf;
108     if (getFactory() == null) {
109       vf = defaultFactory;
110     } else {
111       vf = getFactory();
112     }
113     var = vf.getVariable(oid, type, mibLevel, entry);
114     if (value != null) {
115       switch (type) {
116         case SMIConstants.SYNTAX_INTEGER:
117           // case SMIConstants.SYNTAX_INTEGER32:
118           ((Integer32) var).setValue((Integer) value);
119           break;
120         case SMIConstants.SYNTAX_OCTET_STRING:
121           // case SMIConstants.SYNTAX_BITS:
122           ((OctetString) var).setValue(value.toString());
123           break;
124         case SMIConstants.SYNTAX_NULL:
125           break;
126         case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
127           ((OID) var).setValue(value.toString());
128           break;
129         case SMIConstants.SYNTAX_IPADDRESS:
130           ((IpAddress) var).setValue(value.toString());
131           break;
132         case SMIConstants.SYNTAX_COUNTER32:
133           ((Counter32) var).setValue((Long) value);
134           break;
135         case SMIConstants.SYNTAX_GAUGE32:
136           // case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
137           ((Gauge32) var).setValue((Long) value);
138           break;
139         case SMIConstants.SYNTAX_TIMETICKS:
140           if (value instanceof TimeTicks) {
141             ((TimeTicks) var).setValue(value.toString());
142           } else {
143             ((TimeTicks) var).setValue((Long) value);
144           }
145           break;
146         case SMIConstants.SYNTAX_OPAQUE:
147           ((Opaque) var).setValue((byte[]) value);
148           break;
149         case SMIConstants.SYNTAX_COUNTER64:
150           ((Counter64) var).setValue((Long) value);
151           break;
152         default:
153           throw new IllegalArgumentException(
154               "Unmanaged Type: " + value.getClass());
155       }
156     }
157     return var;
158   }
159 
160   /**
161    * Set a Variable value
162    *
163    * @param var
164    * @param value
165    * @param type
166    */
167   public static void setVariable(final Variable var, final Object value,
168                                  final int type) {
169     if (value != null) {
170       switch (type) {
171         case SMIConstants.SYNTAX_INTEGER:
172           // case SMIConstants.SYNTAX_INTEGER32:
173           ((Integer32) var).setValue((Integer) value);
174           break;
175         case SMIConstants.SYNTAX_OCTET_STRING:
176           // case SMIConstants.SYNTAX_BITS:
177           ((OctetString) var).setValue(value.toString());
178           break;
179         case SMIConstants.SYNTAX_NULL:
180           break;
181         case SMIConstants.SYNTAX_OBJECT_IDENTIFIER:
182           ((OID) var).setValue(value.toString());
183           break;
184         case SMIConstants.SYNTAX_IPADDRESS:
185           ((IpAddress) var).setValue(value.toString());
186           break;
187         case SMIConstants.SYNTAX_COUNTER32:
188           ((Counter32) var).setValue((Long) value);
189           break;
190         case SMIConstants.SYNTAX_GAUGE32:
191           // case SMIConstants.SYNTAX_UNSIGNED_INTEGER32:
192           ((Gauge32) var).setValue((Long) value);
193           break;
194         case SMIConstants.SYNTAX_TIMETICKS:
195           if (value instanceof TimeTicks) {
196             ((TimeTicks) var).setValue(value.toString());
197           } else {
198             ((TimeTicks) var).setValue((Long) value);
199           }
200           break;
201         case SMIConstants.SYNTAX_OPAQUE:
202           ((Opaque) var).setValue((byte[]) value);
203           break;
204         case SMIConstants.SYNTAX_COUNTER64:
205           ((Counter64) var).setValue((Long) value);
206           break;
207         default:
208           throw new IllegalArgumentException(
209               "Unmanaged Type: " + value.getClass());
210       }
211     }
212   }
213 
214   /**
215    * @return the factory
216    */
217   public static WaarpInterfaceVariableFactory getFactory() {
218     return factory;
219   }
220 
221   /**
222    * @param factory the factory to set
223    */
224   public static void setFactory(final WaarpInterfaceVariableFactory factory) {
225     WaarpMOFactory.factory = factory;
226   }
227 }