javaccを使ってSNMPのASN.1の表現をprotocol buffersの構文に変換してみた

NAME

SNMP(ASN.1) to gRPC(Protocol Buffers) for JavaCC

ダウンロード

なぜ Protocol Buffers を使うの?

そこに JavaCC があるからだ

さぁ始めよう

実行例

例えば、サンプルを動かすには次のようにします。

> cd .\src\main\config
> .\run.bat

or:

$ cd ./src/main/config
$ java -jar snmpToGrpc.jar

ね。簡単でしょう!

トラップの例

From ASN.1 Type

linkDown NOTIFICATION-TYPE
    OBJECTS { ifIndex, ifAdminStatus, ifOperStatus }
    STATUS  current
    DESCRIPTION
            "A linkDown trap signifies that the SNMP entity, acting in
            an agent role, has detected that the ifOperStatus object for
            one of its communication links is about to enter the down
            state from some other state (but not from the notPresent
            state).  This other state is indicated by the included value
            of ifOperStatus."
    ::= { snmpTraps 3 }

To Protocol Buffers Type

rpc TrplinkDown (LInkDownKey) returns (NoKey) {}
 ...
// nodeName=linkDown
message LInkDownKey {
  int32 ifIndex = 1; // Integer32
  int32 ifAdminStatus = 2; // INTEGER
  int32 ifOperStatus = 3; // INTEGER
}

テーブル形式の例

From ASN.1 Type

ifEntry OBJECT-TYPE
    SYNTAX      IfEntry
    MAX-ACCESS  not-accessible
    STATUS      current
    DESCRIPTION
            "An entry containing management information applicable to a
            particular interface."
    INDEX   { ifIndex }
    ::= { ifTable 1 }

IfEntry ::=
    SEQUENCE {
        ifIndex                 InterfaceIndex,
        ifDescr                 DisplayString,
        ifType                  IANAifType,
        ifMtu                   Integer32,
        ifSpeed                 Gauge32,
        ifPhysAddress           PhysAddress,
        ifAdminStatus           INTEGER,
        ifOperStatus            INTEGER,
        ifLastChange            TimeTicks,
        ifInOctets              Counter32,
        ifInUcastPkts           Counter32,
        ifInNUcastPkts          Counter32,  -- deprecated
        ifInDiscards            Counter32,
        ifInErrors              Counter32,
        ifInUnknownProtos       Counter32,
        ifOutOctets             Counter32,
        ifOutUcastPkts          Counter32,
        ifOutNUcastPkts         Counter32,  -- deprecated
        ifOutDiscards           Counter32,
        ifOutErrors             Counter32,
        ifOutQLen               Gauge32,    -- deprecated
        ifSpecific              OBJECT IDENTIFIER -- deprecated
    }

To Protocol Buffers Type

  rpc SetIFEntry (IFEntryKeyIndex) returns (NoKey) {}
  rpc GetIFEntry (IFEntryIndex) returns (IFEntryKey) {}
  rpc SayIFEntry (NoKey) returns (stream IFEntryIndex) {}
  ...
message IFEntryKeyIndex {
  IFEntryKey key = 1; 
  IFEntryIndex value = 2; 
}
message IFEntryIndex {
  int32 ifIndex = 1; // Integer32
}
message IFEntryKey {
  int32 ifType = 1; // IANAifType
  int32 ifMtu = 2; // Integer32
  bytes ifPhysAddress = 3; // OCTET STRING
  uint32 ifInDiscards = 4; // Counter32
  uint32 ifOutUcastPkts = 5; // Counter32
  uint32 ifSpeed = 6; // Gauge32
  uint32 ifInNUcastPkts = 7; // Counter32
  uint32 ifInErrors = 8; // Counter32
  string ifDescr = 9; // DisplayString
  bytes ifLastChange = 10; // TimeTicks
  uint32 ifOutDiscards = 11; // Counter32
  uint32 ifOutQLen = 12; // Gauge32
  uint32 ifInUnknownProtos = 13; // Counter32
  uint32 ifInUcastPkts = 14; // Counter32
  uint32 ifOutOctets = 15; // Counter32
  uint32 ifInOctets = 16; // Counter32
  uint32 ifOutErrors = 17; // Counter32
  uint32 ifOutNUcastPkts = 18; // Counter32
}

値の例

From ASN.1 Type

ifTestId         OBJECT-TYPE
    SYNTAX       TestAndIncr
    MAX-ACCESS   read-write
    STATUS       deprecated
    DESCRIPTION
            "This object identifies the current invocation of the
            interface's test."
    ::= { ifTestEntry 1 }

To Protocol Buffers Type

  rpc SetIFTestId (IFTestIdKey) returns (NoKey) {}
  rpc GetIFTestId (NoKey) returns (IFTestIdKey) {}
  ...
message IFTestIdKey {
  int32 ifTestId = 1; // INTEGER
}

クライアント側実装例

To Protocol Buffers(Java) Type

        LinkDownKey key = LinkDownKey
                .newBuilder()
                .setIfIndex(1)
                .setIfAdminStatus(2)
                .setIfOperStatus(3)
                .build();

To SNMP4j Type

        LinkDownKey key = LinkDownKeySnmpBuilder
                .newBuilder()
                .setIfIndex(new org.snmp4j.smi.Integer32(1))
                .setIfAdminStatus(new org.snmp4j.smi.Integer32(2))
                .setIfOperStatus(new org.snmp4j.smi.Integer32(3))
                .build();

サーバ側実装例

To Protocol Buffers(Java) Type

        LinkDownKey key = ...
        //
        int ifIndex = key.getIfIndex();
        int ifAdmin = key.getIfAdminStatus();
        int ifOper  = key.getIfOperStatus();

To SNMP4j Type

        LinkDownKey work = ...
        LinkDownKeySnmpWrapper key = new LinkDownKeySnmpWrapper(work);
        //
        org.snmp4j.smi.Integer32 ifIndex = key.getIfIndex();
        org.snmp4j.smi.Integer32 ifAdmin = key.getIfAdminStatus();
        org.snmp4j.smi.Integer32 ifOper  = key.getIfOperStatus();

型変換

@see "jp.ne.ruru.park.ando.jstg.MessageCreator"

ASN.1 Type SNMP4j Type Java Type Protocol Buffers Type
DisplayString java.lang.String java.lang.String string
AutonomousType org.snmp4j.smi.OctetString byte [] bytes
OBJECT IDENTIFIER org.snmp4j.smi.OID int [] repeated uint32
OCTET STRING org.snmp4j.smi.OctetString byte [] bytes
Opaque org.snmp4j.smi.OctetString byte [] bytes
INTEGER org.snmp4j.smi.Integer32 int int32
IANAifType org.snmp4j.smi.Integer32 int int32
IANAtunnelType org.snmp4j.smi.Integer64 long int64
Integer org.snmp4j.smi.Integer32 int int32
Integer32 org.snmp4j.smi.Integer32 int int32
Integer64 org.snmp4j.smi.Counter64 long int64
Unsigned org.snmp4j.smi.UnsignedInteger32 long uint64
Unsigned32 org.snmp4j.smi.UnsignedInteger32 long uint64
Unsigned64 org.snmp4j.smi.Counter64 long uint64
Counter org.snmp4j.smi.Counter32 long uint64
Counter32 org.snmp4j.smi.Counter32 long uint64
Counter64 org.snmp4j.smi.Counter64 long uint64
Gauge org.snmp4j.smi.Gauge32 long uint64
Gauge32 org.snmp4j.smi.Gauge32 long uint64
Gauge64 org.snmp4j.smi.Counter64 long uint64
TimeTicks org.snmp4j.smi.TimeTicks long uint64
InetAddressIPv6 org.snmp4j.smi.OctetString byte [] bytes
IpAddress org.snmp4j.smi.OctetString byte [] bytes
InetAddress org.snmp4j.smi.OctetString byte [] bytes
BITS org.snmp4j.smi.OctetString byte [] bytes
PhysAddress org.snmp4j.smi.OctetString byte [] bytes
MacAddress org.snmp4j.smi.OctetString byte [] bytes
TruthValue org.snmp4j.smi.OctetString byte [] bytes
TestAndIncr org.snmp4j.smi.OctetString byte [] bytes
AutonomousType org.snmp4j.smi.OctetString byte [] bytes
InstancePointer org.snmp4j.smi.OctetString byte [] bytes
CHOICE org.snmp4j.smi.OctetString byte [] bytes