Commits

stingh711  committed cbbff75

refactor code

  • Participants
  • Parent commits da867c9

Comments (0)

Files changed (3)

File config.properties

 interface.table.description.3=wan3
 interface.table.type.3=2
 
+#data for current session
+session.number=5
+session.application.1=qq
+session.direction.1=1
+session.interface.1=wan1
+session.protocol.1=qq
+session.src.ip.1=1.1.1.1
+session.src.port.1=34
+session.dst.ip.1=2.2.2.2
+session.dst.port.1=80
+session.sent.1=3434
+session.recv.1=32434
+session.status.1=1
+session.application.2=qq
+session.direction.2=1
+session.interface.2=wan1
+session.protocol.2=qq
+session.src.ip.2=1.1.1.1
+session.src.port.2=34
+session.dst.ip.2=2.2.2.2
+session.dst.port.2=80
+session.sent.2=3434
+session.recv.2=32434
+session.status.2=1
+session.application.3=qq
+session.direction.3=1
+session.interface.3=wan1
+session.protocol.3=qq
+session.src.ip.3=1.1.1.1
+session.src.port.3=34
+session.dst.ip.3=2.2.2.2
+session.dst.port.3=80
+session.sent.3=3434
+session.recv.3=32434
+session.status.3=1
+session.application.4=qq
+session.direction.4=1
+session.interface.4=wan1
+session.protocol.4=qq
+session.src.ip.4=1.1.1.1
+session.src.port.4=34
+session.dst.ip.4=2.2.2.2
+session.dst.port.4=80
+session.sent.4=3434
+session.recv.4=32434
+session.status.4=1
+session.application.5=qq
+session.direction.5=1
+session.interface.5=wan1
+session.protocol.5=qq
+session.src.ip.5=1.1.1.1
+session.src.port.5=34
+session.dst.ip.5=2.2.2.2
+session.dst.port.5=80
+session.sent.5=3434
+session.recv.5=32434
+session.status.5=1
+
+#data for nsFlowEntry
+flow.number=4
+flow.interface.index.1=1
+flow.sent.1=4324
+flow.recv.1=432123
+flow.interface.index.2=2
+flow.sent.2=4324
+flow.recv.2=432123
+flow.interface.index.3=2
+flow.sent.3=4324
+flow.recv.3=432123
+flow.interface.index.4=2
+flow.sent.4=4324
+flow.recv.4=432123
+
+#data for lan interfaces
+lan.interface.number=2
+lan.interface.index.1=1
+lan.interface.ip.1=192.168.1.1
+lan.interface.mask.1=255.255.255.0
+lan.interface.enable.nat.1=1
+lan.interface.recv.1=3431234
+lan.interface.sent.1=3451
+lan.interface.mac.1=00:a0:c5:11:12:34
+lan.interface.mtu.1=1000
+lan.interface.index.2=2
+lan.interface.ip.2=192.168.1.2
+lan.interface.mask.2=255.255.255.0
+lan.interface.enable.nat.2=1
+lan.interface.recv.2=3431234
+lan.interface.sent.2=3451
+lan.interface.mac.2=00:a0:c5:13:12:34
+lan.interface.mtu.2=1000
+
+#Top arp sender table
+arp.sender.number=3
+arp.sender.index.1=1
+arp.sender.ip.1=192.168.1.1
+arp.sender.index.2=2
+arp.sender.ip.2=192.168.1.2
+arp.sender.index.3=3
+arp.sender.ip.3=192.168.1.3
+
+
 #traps parameters
 server.ip=172.25.21.69
 nsTrapVarAdmin=sting

File src/com/zyxel/enc/netbar/agent/AbstractTableGenerator.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package com.zyxel.enc.netbar.agent;
+
+import org.snmp4j.agent.mo.DefaultMOTable;
+import org.snmp4j.agent.mo.MOColumn;
+import org.snmp4j.agent.mo.MOMutableTableModel;
+import org.snmp4j.agent.mo.MOTableIndex;
+import org.snmp4j.agent.mo.MOTableSubIndex;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.SMIConstants;
+
+/**
+ *
+ * @author sting
+ */
+public abstract class AbstractTableGenerator {
+    private MOTableSubIndex[] indexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
+    private MOTableIndex index = new MOTableIndex(indexs, false);
+    
+    public DefaultMOTable generateTable(OID oid) {
+        DefaultMOTable table = new DefaultMOTable(oid, index, defineColumns());
+        MOMutableTableModel model = (MOMutableTableModel) table.getModel();
+        setVariables(model);
+        table.setVolatile(true);
+        
+        return table;
+    }
+    
+    public abstract MOColumn[] defineColumns();
+    public abstract void setVariables(MOMutableTableModel model);
+}

File src/com/zyxel/enc/netbar/agent/NRrouterMib.java

 package com.zyxel.enc.netbar.agent;
 
+import com.zyxel.enc.netbar.agent.generator.AppStatisticsTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.CurrentSessionTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.FlowTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.InterfaceTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.LanInterfaceTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.TopArpSenderTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.TopologyDeviceTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.TopologyLinkTableGenerator;
+import com.zyxel.enc.netbar.agent.generator.WanInterfaceTableGenerator;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
 import org.snmp4j.agent.DuplicateRegistrationException;
 import org.snmp4j.agent.MOGroup;
 import org.snmp4j.agent.MOServer;
-import org.snmp4j.agent.mo.DefaultMOMutableRow2PC;
 import org.snmp4j.agent.mo.DefaultMOTable;
 import org.snmp4j.agent.mo.MOAccessImpl;
-import org.snmp4j.agent.mo.MOColumn;
-import org.snmp4j.agent.mo.MOMutableTableModel;
-import org.snmp4j.agent.mo.MOTableIndex;
-import org.snmp4j.agent.mo.MOTableSubIndex;
 import org.snmp4j.agent.mo.snmp.DateAndTimeScalar;
-import org.snmp4j.smi.Counter32;
-import org.snmp4j.smi.Integer32;
-import org.snmp4j.smi.IpAddress;
 import org.snmp4j.smi.OID;
 import org.snmp4j.smi.OctetString;
-import org.snmp4j.smi.SMIConstants;
 import org.snmp4j.smi.TimeTicks;
-import org.snmp4j.smi.Variable;
 
 public class NRrouterMib implements MOGroup {
 
     private static final OID nsIfTable = new OID("1.3.6.1.4.1.890.1.5.17.1.6.1.1");
     private static final OID nsWanIfTable = new OID("1.3.6.1.4.1.890.1.5.17.1.7.1.1");
     private static final OID oidTopologyLinks = new OID("1.3.6.1.4.1.890.1.5.17.1.9.2.1.1");
-    private static final OID oidCurrentLinks = new OID("1.3.6.1.4.1.890.1.5.17.1.3.1.1");
+    private static final OID oidCurrentSessions = new OID("1.3.6.1.4.1.890.1.5.17.1.3.1.1");
+    private static final OID oidFlowTable = new OID("1.3.6.1.4.1.890.1.5.17.1.4.1.1");
+    private static final OID oidLanInterfaceTable = new OID("1.3.6.1.4.1.890.1.5.17.1.8.1.1");
+    private static final OID oidTopArpTable = new OID("1.3.6.1.4.1.890.1.5.17.1.10.1.1.1");
     private static long startTime = 0;
-    //Topology devices table
-    private DefaultMOTable deviceTable;
-    //Topology links table
-    private DefaultMOTable linkTable;
-    //App Statistics 10 mins table
-    private DefaultMOTable app10MinsTable;
-    private DefaultMOTable app3hoursTable;
-    private DefaultMOTable app24hoursTable;
-    private DefaultMOTable appAllTable;
-    //Router interface table
-    private DefaultMOTable interfaceTable;
-    //Router wan interface table
-    private DefaultMOTable wanInterfaceTable;
-    private MOTableSubIndex[] topologyDevicesIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex topologyDevicesIndex = new MOTableIndex(topologyDevicesIndexs, false);
-    private MOTableSubIndex[] app10MinsTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex app10MinsTableIndex = new MOTableIndex(app10MinsTableIndexs, false);
-    private MOTableSubIndex[] app3hoursTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex app3hoursTableIndex = new MOTableIndex(app3hoursTableIndexs, false);
-    private MOTableSubIndex[] app24hoursTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex app24hoursTableIndex = new MOTableIndex(app24hoursTableIndexs, false);
-    private MOTableSubIndex[] appAllTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex appAllTableIndex = new MOTableIndex(appAllTableIndexs, false);
-    private MOTableSubIndex[] wanInterfaceTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex wanInterfaceTableIndex = new MOTableIndex(wanInterfaceTableIndexs, false);
-    private MOTableSubIndex[] interfaceTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex interfaceTableIndex = new MOTableIndex(interfaceTableIndexs, false);
-    private MOTableSubIndex[] linkTableIndexs = new MOTableSubIndex[]{new MOTableSubIndex(SMIConstants.SYNTAX_INTEGER, 1, 1)};
-    private MOTableIndex linkTableIndex = new MOTableIndex(linkTableIndexs, false);
 
     private static String getCurrentTimeAsString() {
         return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
         server.register(MOCreator.createReadOnly(nsSysMgmtActiveHost, Integer.valueOf(Config.get("host.active"))), context);
         server.register(MOCreator.createTimeTicks(nsSysUptime, getStartTime()), context);
         server.register(new DateAndTimeScalar(nsSysDateTime, MOAccessImpl.ACCESS_READ_ONLY, new OctetString(getCurrentTimeAsString())), context);
-        createTopologyTables();
-        server.register(deviceTable, context);
         
-        createLinkTables();
-        server.register(linkTable, context);
-        
-        createAppStatisticsTables();
-        server.register(app10MinsTable, context);
-        server.register(app3hoursTable, context);
-        server.register(app24hoursTable, context);
-        server.register(appAllTable, context);
-
-        createRouterWanInterfaceTable();
-        server.register(wanInterfaceTable, context);
-        
-        createRouterInterfaceTable();
-        server.register(interfaceTable, context);
+        server.register(createTopologyTables(), context);
+        server.register(createLinkTables(), context);
+        registerAppStatisticsTables(server, context);
+        server.register(createRouterWanInterfaceTable(), context);
+        server.register(createRouterInterfaceTable(), context);
+        server.register(createCurrentSessionTable(), context);
+        server.register(createFlowTable(), context);
+        server.register(createLanInterfaceTable(), context);
+        server.register(createTopArpSenderTable(), context);
     }
 
     @Override
         return v;
     }
 
-    private void createTopologyTables() {
-        MOColumn[] columns = new MOColumn[8];
-        columns[0] = new MOColumn(1, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[1] = new MOColumn(2, SMIConstants.SYNTAX_IPADDRESS, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[2] = new MOColumn(3, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[3] = new MOColumn(4, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[4] = new MOColumn(5, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[5] = new MOColumn(6, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[6] = new MOColumn(7, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[7] = new MOColumn(8, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        deviceTable = new DefaultMOTable(oidTopologyDevices, topologyDevicesIndex, columns);
-        MOMutableTableModel model = (MOMutableTableModel) deviceTable.getModel();
-
-        int number = Integer.parseInt(Config.get("topology.device.number"));
-
-        for (int i = 1; i <= number; i++) {
-            Variable[] v = new Variable[]{
-                new OctetString(Config.get("topology.device.hostname." + i)),
-                new IpAddress(Config.get("topology.device.ip." + i)),
-                new OctetString(Config.get("topology.device.mac." + i)),
-                new OctetString(Config.get("topology.device.type." + i)),
-                new OctetString(Config.get("topology.device.model." + i)),
-                new OctetString(Config.get("topology.device.vender." + i)),
-                new OctetString(Config.get("topology.device.firmware." + i)),
-                new OctetString(Config.get("topology.device.hardware." + i))
-            };
-            model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)), v));
-        }
-
-        deviceTable.setVolatile(true);
+    private DefaultMOTable createTopologyTables() {
+        return new TopologyDeviceTableGenerator().generateTable(oidTopologyDevices);
     }
 
-    private void createLinkTables() {
-        MOColumn[] columns = new MOColumn[5];
-        columns[0] = new MOColumn(1, SMIConstants.SYNTAX_IPADDRESS, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[1] = new MOColumn(2, SMIConstants.SYNTAX_INTEGER32, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[2] = new MOColumn(3, SMIConstants.SYNTAX_IPADDRESS, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[3] = new MOColumn(4, SMIConstants.SYNTAX_INTEGER32, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[4] = new MOColumn(5, SMIConstants.SYNTAX_INTEGER32, MOAccessImpl.ACCESS_READ_ONLY);
-        linkTable = new DefaultMOTable(oidTopologyLinks, linkTableIndex, columns);
-        MOMutableTableModel model = (MOMutableTableModel) linkTable.getModel();
-
-        int number = Integer.parseInt(Config.get("topology.link.number"));
-
-        for (int i = 1; i <= number; i++) {
-            Variable[] v = new Variable[]{
-                new IpAddress(Config.get("topology.link.src.ip." + i)),
-                new Integer32(Integer.valueOf(Config.get("topology.link.src.port." + i))),
-                new IpAddress(Config.get("topology.link.dst.ip." + i)),
-                new Integer32(Integer.valueOf(Config.get("topology.link.dst.port." + i))),
-                new Integer32(Integer.valueOf(Config.get("topology.link.type." + i))),
-            };
-            model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)), v));
-        }
-
-        linkTable.setVolatile(true);
+    private DefaultMOTable createLinkTables() {
+        return new TopologyLinkTableGenerator().generateTable(oidTopologyLinks);
     }
     
-    private void createRouterWanInterfaceTable() {
-        MOColumn[] columns = new MOColumn[8];
-        columns[0] = new MOColumn(1, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[1] = new MOColumn(2, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[2] = new MOColumn(3, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[3] = new MOColumn(4, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[4] = new MOColumn(5, SMIConstants.SYNTAX_IPADDRESS, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[5] = new MOColumn(6, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[6] = new MOColumn(7, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[7] = new MOColumn(8, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        wanInterfaceTable = new DefaultMOTable(nsWanIfTable, wanInterfaceTableIndex, columns);
-        MOMutableTableModel model = (MOMutableTableModel) wanInterfaceTable.getModel();
-
-        int number = Integer.parseInt(Config.get("wan.interface.table.number"));
-
-        for (int i = 1; i <= number; i++) {
-            Variable[] v = new Variable[]{
-                new OctetString(Config.get("wan.interface.table.name." + i)),
-                new Integer32(Integer.valueOf(Config.get("wan.interface.table.status." + i))),
-                new Integer32(Integer.valueOf(Config.get("wan.interface.table.type." + i))),
-                new OctetString(Config.get("wan.interface.table.ipmask." + i)),
-                new IpAddress(Config.get("wan.interface.table.gateway." + i)),
-                new OctetString(Config.get("wan.interface.table.dns." + i)),
-                new Counter32(Integer.valueOf(Config.get("wan.interface.table.recv." + i))),
-                new Counter32(Integer.valueOf(Config.get("wan.interface.table.sent." + i)))
-            };
-            model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)), v));
-        }
-
-        wanInterfaceTable.setVolatile(true);
+    private DefaultMOTable createRouterWanInterfaceTable() {
+        return new WanInterfaceTableGenerator().generateTable(nsWanIfTable);
     }
 
-    private void createRouterInterfaceTable() {
-        MOColumn[] columns = new MOColumn[4];
-        columns[0] = new MOColumn(1, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[1] = new MOColumn(2, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[2] = new MOColumn(3, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[3] = new MOColumn(4, SMIConstants.SYNTAX_INTEGER, MOAccessImpl.ACCESS_READ_ONLY);
-        interfaceTable = new DefaultMOTable(nsIfTable, interfaceTableIndex, columns);
-        MOMutableTableModel model = (MOMutableTableModel) interfaceTable.getModel();
-
-        int number = Integer.parseInt(Config.get("interface.table.number"));
-
-        for (int i = 1; i <= number; i++) {
-            Variable[] v = new Variable[]{
-                new Integer32(Integer.valueOf(Config.get("interface.table.index." + i))),
-                new OctetString(Config.get("interface.table.name." + i)),
-                new OctetString(Config.get("interface.table.description." + i)),
-                new Integer32(Integer.valueOf(Config.get("interface.table.type." + i)))
-            };
-            model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)), v));
-        }
-
-        interfaceTable.setVolatile(true);
+    private DefaultMOTable createRouterInterfaceTable() {
+        return new InterfaceTableGenerator().generateTable(nsIfTable);
     }
     
-    private void createAppStatisticsTables() {
-        app10MinsTable = createAppStatisticsTable(nsAppStatistics10Min, app10MinsTableIndex, "app.statistics.10mins.number", "app.statistics.10mins.name.", "app.statistics.10mins.traffic.");
-        app3hoursTable = createAppStatisticsTable(nsAppStatistics3hours, app3hoursTableIndex, "app.statistics.3hours.number", "app.statistics.3hours.name.", "app.statistics.3hours.traffic.");
-        app24hoursTable = createAppStatisticsTable(nsAppStatistics24hours, app24hoursTableIndex, "app.statistics.24hours.number", "app.statistics.24hours.name.", "app.statistics.24hours.traffic.");
-        appAllTable = createAppStatisticsTable(nsAppStatisticsAll, appAllTableIndex, "app.statistics.all.number", "app.statistics.all.name.", "app.statistics.all.traffic.");
+    private DefaultMOTable createCurrentSessionTable() {
+        return new CurrentSessionTableGenerator().generateTable(oidCurrentSessions);
     }
-
-    private DefaultMOTable createAppStatisticsTable(OID oid, MOTableIndex index, String numberKey, String nameKey, String trafficKey) {
-        MOColumn[] columns = new MOColumn[2];
-        columns[0] = new MOColumn(1, SMIConstants.SYNTAX_OCTET_STRING, MOAccessImpl.ACCESS_READ_ONLY);
-        columns[1] = new MOColumn(2, SMIConstants.SYNTAX_COUNTER32, MOAccessImpl.ACCESS_READ_ONLY);
-        DefaultMOTable table = new DefaultMOTable(oid, index, columns);
-        MOMutableTableModel model = (MOMutableTableModel) table.getModel();
-
-        int number = Integer.parseInt(Config.get(numberKey));
-
-        for (int i = 1; i <= number; i++) {
-            Variable[] v = new Variable[]{
-                new OctetString(Config.get(nameKey + i)),
-                new Counter32(Long.parseLong(Config.get(trafficKey + i)))
-            };
-            model.addRow(new DefaultMOMutableRow2PC(new OID(String.valueOf(i)), v));
-        }
-
-        table.setVolatile(true);
-        return table;
+    
+    private DefaultMOTable createFlowTable() {
+        return new FlowTableGenerator().generateTable(oidFlowTable);
+    }
+    
+    private DefaultMOTable createLanInterfaceTable() {
+        return new LanInterfaceTableGenerator().generateTable(oidLanInterfaceTable);
+    }
+    
+    private DefaultMOTable createTopArpSenderTable() {
+        return new TopArpSenderTableGenerator().generateTable(oidTopArpTable);
+    }
+    
+    private void registerAppStatisticsTables(MOServer server, OctetString context) throws DuplicateRegistrationException {
+        DefaultMOTable tenMinsTable = new AppStatisticsTableGenerator("app.statistics.10mins.number", "app.statistics.10mins.name.", "app.statistics.10mins.traffic.").generateTable(nsAppStatistics10Min);
+        server.register(tenMinsTable, context);
+        DefaultMOTable threeHoursTable = new AppStatisticsTableGenerator("app.statistics.3hours.number", "app.statistics.3hours.name.", "app.statistics.3hours.traffic.").generateTable(nsAppStatistics3hours);
+        server.register(threeHoursTable, context);
+        DefaultMOTable oneDayTable = new AppStatisticsTableGenerator("app.statistics.24hours.number", "app.statistics.24hours.name.", "app.statistics.24hours.traffic.").generateTable(nsAppStatistics24hours);
+        server.register(oneDayTable, context);
+        DefaultMOTable appTable = new AppStatisticsTableGenerator("app.statistics.all.number", "app.statistics.all.name.", "app.statistics.all.traffic.").generateTable(nsAppStatisticsAll);
+        server.register(appTable, context);
     }
 }