Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Commit 03f13cb

Browse files
committed
SNMP driver implementation in progress
1 parent 6fe7285 commit 03f13cb

11 files changed

Lines changed: 522 additions & 80 deletions

File tree

eu.dariolucia.reatmetric.driver.snmp.util/Documentation.adoc

Lines changed: 0 additions & 5 deletions
This file was deleted.

eu.dariolucia.reatmetric.driver.snmp.util/src/main/java/eu/dariolucia/reatmetric/driver/snmp/util/BasicComputerDriverGenerator.java

Lines changed: 109 additions & 51 deletions
Large diffs are not rendered by default.

eu.dariolucia.reatmetric.driver.snmp/Documentation.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ snmp module registration for each endpoint that requires such driver.
1616
<name>Test System</name>
1717
<log-property-file>$HOME\Reatmetric\reatmetric_test\log.properties</log-property-file>
1818
<definitions-location>$HOME\Reatmetric\reatmetric_test\processing</definitions-location>
19-
<driver name="Equipment 1 SNMP Driver" type="eu.dariolucia.reatmetric.driver.snmp.SnmpDriver"
20-
configuration="$HOME\Reatmetric\reatmetric_test\snmp1"/>
19+
<driver name="SNMP Driver" type="eu.dariolucia.reatmetric.driver.snmp.SnmpDriver"
20+
configuration="$HOME\Reatmetric\reatmetric_test\snmp"/>
2121
</ns1:core>
2222
----
2323

@@ -30,3 +30,5 @@ eu.dariolucia.reatmetric.driver.snmp.configuration. It is an XML file named _con
3030
namespace definition _http://dariolucia.eu/reatmetric/driver/snmp_.
3131

3232
To be written
33+
34+
===== Device Configuration File

eu.dariolucia.reatmetric.driver.snmp/src/main/java/eu/dariolucia/reatmetric/driver/snmp/SnmpTransportConnector.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ protected SnmpTransportConnector(String driverName, SnmpDevice device, IRawDataB
7272
this.rawDataBroker = rawDataBroker;
7373
this.processingModel = processingModel;
7474
this.deviceTimer = new Timer("SNMP Device " + getName() + " Timer Service", true);
75+
// Initialise
76+
this.device.getDeviceConfiguration().initialise(device.getPath(), this.processingModel);
7577
// Build the target
7678
this.target = new CommunityTarget<>();
7779
Address targetAddress = GenericAddress.parse(device.getConnectionString());

eu.dariolucia.reatmetric.driver.snmp/src/main/java/eu/dariolucia/reatmetric/driver/snmp/configuration/OidEntry.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public class OidEntry {
3838
private String path;
3939

4040
@XmlAttribute(name = "type", required = true)
41-
private ValueTypeEnum type;
41+
private OidEntryType type;
4242

4343
public OidEntry() {
4444
// Nothing
4545
}
4646

47-
public OidEntry(String oid, String path, ValueTypeEnum type) {
47+
public OidEntry(String oid, String path, OidEntryType type) {
4848
this.oid = oid;
4949
this.path = path;
5050
this.type = type;
@@ -70,11 +70,11 @@ public void setPath(String path) {
7070
this.path = path;
7171
}
7272

73-
public ValueTypeEnum getType() {
73+
public OidEntryType getType() {
7474
return type;
7575
}
7676

77-
public void setType(ValueTypeEnum type) {
77+
public void setType(OidEntryType type) {
7878
this.type = type;
7979
}
8080

@@ -96,16 +96,21 @@ public int getExternalId() {
9696

9797
public Object extractValue(Variable variable) {
9898
switch (type) {
99-
case ENUMERATED: return extractInt(variable);
100-
case SIGNED_INTEGER:
101-
case UNSIGNED_INTEGER: return extractLong(variable);
102-
case OCTET_STRING: return extractByteArray(variable);
103-
case CHARACTER_STRING: return extractString(variable);
104-
case REAL: return extractReal(variable);
99+
case INTEGER: return extractInt(variable);
100+
case LONG: return extractLong(variable);
101+
case BYTE_ARRAY: return extractByteArray(variable);
102+
case STRING: return extractString(variable);
103+
case DOUBLE: return extractReal(variable);
104+
case OID: return extractOidLastValue(variable);
105105
default: return null;
106106
}
107107
}
108108

109+
private Object extractOidLastValue(Variable variable) {
110+
String typeString = variable.toString();
111+
return Integer.parseInt(typeString.substring(typeString.lastIndexOf('.') + 1));
112+
}
113+
109114
private Object extractReal(Variable variable) {
110115
if (variable instanceof OctetString) {
111116
String itemValue = variable.toString();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2024 Dario Lucia (https://www.dariolucia.eu)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package eu.dariolucia.reatmetric.driver.snmp.configuration;
19+
20+
import eu.dariolucia.reatmetric.api.value.ValueTypeEnum;
21+
22+
public enum OidEntryType {
23+
STRING,
24+
INTEGER,
25+
OID,
26+
LONG,
27+
DOUBLE,
28+
BYTE_ARRAY;
29+
30+
public ValueTypeEnum toValueTypeEnum() {
31+
switch (this) {
32+
case OID: return ValueTypeEnum.ENUMERATED;
33+
case STRING: return ValueTypeEnum.CHARACTER_STRING;
34+
case BYTE_ARRAY: return ValueTypeEnum.OCTET_STRING;
35+
case INTEGER: return ValueTypeEnum.ENUMERATED;
36+
case LONG: return ValueTypeEnum.SIGNED_INTEGER;
37+
case DOUBLE: return ValueTypeEnum.REAL;
38+
default: throw new IllegalStateException("Type " + this + " cannot be mapped to ValueTypeEnum");
39+
}
40+
}
41+
}

eu.dariolucia.reatmetric.driver.snmp/src/main/java/eu/dariolucia/reatmetric/driver/snmp/configuration/SnmpConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
import jakarta.xml.bind.JAXBContext;
2222
import jakarta.xml.bind.JAXBException;
2323
import jakarta.xml.bind.Unmarshaller;
24-
import jakarta.xml.bind.annotation.XmlAccessType;
25-
import jakarta.xml.bind.annotation.XmlAccessorType;
26-
import jakarta.xml.bind.annotation.XmlAttribute;
27-
import jakarta.xml.bind.annotation.XmlElement;
24+
import jakarta.xml.bind.annotation.*;
2825

2926
import java.io.IOException;
3027
import java.io.InputStream;
3128
import java.util.LinkedList;
3229
import java.util.List;
3330

31+
@XmlRootElement(name = "snmp-configuration", namespace = "http://dariolucia.eu/reatmetric/driver/snmp")
3432
@XmlAccessorType(XmlAccessType.FIELD)
3533
public class SnmpConfiguration {
3634

eu.dariolucia.reatmetric.driver.snmp/src/main/java/eu/dariolucia/reatmetric/driver/snmp/configuration/SnmpDeviceConfiguration.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616

1717
package eu.dariolucia.reatmetric.driver.snmp.configuration;
1818

19-
import eu.dariolucia.reatmetric.api.common.exceptions.ReatmetricException;
19+
import eu.dariolucia.reatmetric.api.processing.IProcessingModel;
2020
import jakarta.xml.bind.JAXBContext;
2121
import jakarta.xml.bind.JAXBException;
22+
import jakarta.xml.bind.Marshaller;
2223
import jakarta.xml.bind.Unmarshaller;
23-
import jakarta.xml.bind.annotation.*;
24+
import jakarta.xml.bind.annotation.XmlAccessType;
25+
import jakarta.xml.bind.annotation.XmlAccessorType;
26+
import jakarta.xml.bind.annotation.XmlElement;
27+
import jakarta.xml.bind.annotation.XmlRootElement;
2428

2529
import java.io.IOException;
2630
import java.io.InputStream;
31+
import java.io.OutputStream;
2732
import java.util.LinkedList;
2833
import java.util.List;
2934

@@ -35,10 +40,19 @@ public static SnmpDeviceConfiguration load(InputStream is) throws IOException {
3540
try {
3641
JAXBContext jc = JAXBContext.newInstance(SnmpDeviceConfiguration.class);
3742
Unmarshaller u = jc.createUnmarshaller();
38-
SnmpDeviceConfiguration sc = (SnmpDeviceConfiguration) u.unmarshal(is);
39-
sc.initialise();
40-
return sc;
41-
} catch (JAXBException | ReatmetricException e) {
43+
return (SnmpDeviceConfiguration) u.unmarshal(is);
44+
} catch (JAXBException e) {
45+
throw new IOException(e);
46+
}
47+
}
48+
49+
public static void save(SnmpDeviceConfiguration d, OutputStream out) throws IOException {
50+
try {
51+
JAXBContext context = JAXBContext.newInstance(SnmpDeviceConfiguration.class);
52+
Marshaller marshaller = context.createMarshaller();
53+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
54+
marshaller.marshal(d, out);
55+
} catch (JAXBException e) {
4256
throw new IOException(e);
4357
}
4458
}
@@ -54,7 +68,7 @@ public void setGroupConfigurationList(List<GroupConfiguration> groupConfiguratio
5468
this.groupConfigurationList = groupConfigurationList;
5569
}
5670

57-
private void initialise() throws ReatmetricException {
58-
//
71+
public void initialise(String prefix, IProcessingModel theModel) {
72+
this.groupConfigurationList.forEach(gc -> gc.initialise(prefix, theModel));
5973
}
6074
}

0 commit comments

Comments
 (0)