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.utils;
21  
22  import org.snmp4j.agent.DuplicateRegistrationException;
23  import org.snmp4j.agent.MOGroup;
24  import org.snmp4j.agent.MOServer;
25  import org.snmp4j.smi.OID;
26  import org.snmp4j.smi.OctetString;
27  import org.snmp4j.smi.Variable;
28  import org.waarp.snmp.WaarpMOFactory;
29  import org.waarp.snmp.interf.WaarpInterfaceMib;
30  
31  /**
32   * MORow implementation for GoldenGate
33   */
34  public class WaarpMORow implements MOGroup {
35    /**
36     * Row access
37     */
38    private WaarpMOScalar[] row;
39    /**
40     * Type access
41     */
42    final int[] type;
43    /**
44     * Base OID
45     */
46    final OID reference;
47    /**
48     * MIB from which this Row is issued
49     */
50    final WaarpInterfaceMib mib;
51    /**
52     * Mib Level entry identification
53     */
54    final int mibLevel;
55  
56    /**
57     * @param mib
58     * @param reference
59     * @param entries
60     * @param mibLevel this integer identifies this Row in the MIB
61     */
62    public WaarpMORow(final WaarpInterfaceMib mib, final OID reference,
63                      final WaarpEntry[] entries, final int mibLevel) {
64      this.mib = mib;
65      this.reference = reference;
66      this.mibLevel = mibLevel;
67      setRow(new WaarpMOScalar[entries.length]);
68      type = new int[entries.length];
69      final int[] ref = this.reference.getValue();
70      final int[] add = new int[2];
71      add[1] = 0;
72      for (int i = 0; i < entries.length; i++) {
73        final WaarpEntry entry = entries[i];
74        type[i] = entry.smiConstantsType;
75        add[0] = i + 1;
76        final OID oid = new OID(ref, add);
77        // the value is null at the creation, meaning values have to be
78        // setup once just after
79        getRow()[i] =
80            WaarpMOFactory.create(oid, null, entry.smiConstantsType, entry.access,
81                                  this, mibLevel, i);
82      }
83    }
84  
85    /**
86     * Set a Value in this Row
87     *
88     * @param index
89     * @param value
90     *
91     * @throws IllegalArgumentException
92     */
93    public final void setValue(final int index, final Object value)
94        throws IllegalArgumentException {
95      if (index >= getRow().length) {
96        throw new IllegalArgumentException("Index exceed Row size");
97      }
98      final Variable var = getRow()[index].getValue();
99      WaarpMOFactory.setVariable(var, value, type[index]);
100   }
101 
102   @Override
103   public final void registerMOs(final MOServer server,
104                                 final OctetString context)
105       throws DuplicateRegistrationException {
106     for (int i = 0; i < getRow().length; i++) {
107       final WaarpMOScalar scalar = getRow()[i];
108       server.register(scalar, context);
109     }
110   }
111 
112   @Override
113   public final void unregisterMOs(final MOServer server,
114                                   final OctetString context) {
115     for (int i = 0; i < getRow().length; i++) {
116       final WaarpMOScalar scalar = getRow()[i];
117       server.unregister(scalar, context);
118     }
119   }
120 
121   /**
122    * @return the row
123    */
124   public final WaarpMOScalar[] getRow() {
125     return row;
126   }
127 
128   /**
129    * @param row the row to set
130    */
131   final void setRow(final WaarpMOScalar[] row) {
132     this.row = row;
133   }
134 }