ONOS-5917 SNMP Alarm Provider
Change-Id: I9eb39021584f3b1a5baed9dfa638569818901197
diff --git a/protocols/snmp/api/BUCK b/protocols/snmp/api/BUCK
index 067c6ea..897a0ac 100644
--- a/protocols/snmp/api/BUCK
+++ b/protocols/snmp/api/BUCK
@@ -1,5 +1,6 @@
COMPILE_DEPS = [
'//lib:CORE_DEPS',
+ '//lib:JACKSON',
'//incubator/api:onos-incubator-api',
'//lib:org.apache.servicemix.bundles.snmp4j',
'//lib:snmp-core',
diff --git a/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpController.java b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpController.java
index 0369fc2..4868ea0 100644
--- a/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpController.java
+++ b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpController.java
@@ -17,15 +17,19 @@
package org.onosproject.snmp;
import com.btisystems.pronx.ems.core.snmp.ISnmpSession;
+import com.google.common.annotations.Beta;
+
import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
import org.onosproject.net.DeviceId;
import java.io.IOException;
+import java.net.URI;
import java.util.Collection;
/**
* Snmp Controller.
*/
+@Beta
public interface SnmpController {
/**
@@ -42,6 +46,14 @@
SnmpDevice getDevice(DeviceId deviceId);
/**
+ * Gets a device for a specific host address.
+ *
+ * @param uri URI of the device
+ * @return SnmpDevice for given deviceId
+ */
+ SnmpDevice getDevice(URI uri);
+
+ /**
* Removes a specific device.
* @param deviceId device id of the device to be removed
*/
@@ -50,9 +62,18 @@
/**
* Add a device with a specific DeviceId.
* @param deviceId deviceId of the device
+ * @param device device to add to this controller
+ * @deprecated 1.10.0 moved to single parameter method with boolean return
+ */
+ @Deprecated
+ void addDevice(DeviceId deviceId, SnmpDevice device);
+
+ /**
+ * Add a snmp device.
+ *
* @param device device to add to this controller
*/
- void addDevice(DeviceId deviceId, SnmpDevice device);
+ void addDevice(SnmpDevice device);
/**
* Gets an Instance of ISnmpSession for a specific device.
@@ -60,7 +81,9 @@
* @param deviceId device to retrieve the session for.
* @return ISnmp session.
* @throws IOException if the session can't be established.
+ * @deprecated 1.14.0 moved to snmp4j
*/
+ @Deprecated
ISnmpSession getSession(DeviceId deviceId) throws IOException;
/**
@@ -70,4 +93,6 @@
* @return default alarm error
*/
DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId);
+
+
}
diff --git a/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDevice.java b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDevice.java
index bd7b3c4..587e2b1 100644
--- a/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDevice.java
+++ b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDevice.java
@@ -16,11 +16,14 @@
package org.onosproject.snmp;
+import com.google.common.annotations.Beta;
import org.onosproject.net.DeviceId;
+import org.snmp4j.Snmp;
/**
* Abstraction a default Snmp Device.
*/
+@Beta
public interface SnmpDevice {
/**
@@ -31,6 +34,13 @@
String deviceInfo();
/**
+ * Returns an Snmp session context for this device.
+ *
+ * @return snmp session
+ */
+ Snmp getSession();
+
+ /**
* Terminates the device connection.
*/
void disconnect();
@@ -57,6 +67,13 @@
int getSnmpPort();
/**
+ * Returns the notification port for asynchronous messages from the device.
+ *
+ * @return notification port
+ */
+ int getNotificationPort();
+
+ /**
* Retrieves the username of the device.
*
* @return username
@@ -71,6 +88,20 @@
String getCommunity();
/**
+ * Returns the protocol of the device.
+ *
+ * @return protocol
+ */
+ String getProtocol();
+
+ /**
+ * Returns the notification protocol of the device.
+ *
+ * @return notification protocol
+ */
+ String getNotificationProtocol();
+
+ /**
* Return the SNMP device deviceID.
*
* @return DeviceId
diff --git a/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java
new file mode 100644
index 0000000..b39180a
--- /dev/null
+++ b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.snmp;
+
+import com.google.common.annotations.Beta;
+import org.apache.commons.lang3.tuple.Pair;
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.config.Config;
+
+/**
+ * Configuration to push devices to the SNMP provider.
+ */
+@Beta
+public class SnmpDeviceConfig extends Config<DeviceId> {
+
+ private static final String PROTOCOL = "protocol";
+ private static final String NOTIFICATION_PROTOCOL = "notificationProtocol";
+ private static final String IP = "ip";
+ private static final String PORT = "port";
+ private static final int DEFAULT_PORT = 161;
+ private static final String NOTIFICATION_PORT = "notificationPort";
+ private static final String USERNAME = "username";
+ private static final String PASSWORD = "password";
+
+ @Override
+ public boolean isValid() {
+ return hasOnlyFields(IP, PORT, USERNAME, PASSWORD) &&
+ ip() != null;
+ }
+
+ /**
+ * Gets the protocol of the SNMP device.
+ *
+ * @return protocol
+ */
+ public String protocol() {
+ return get(PROTOCOL, "udp");
+ }
+
+ /**
+ * Gets the notification protocol of the SNMP device.
+ *
+ * @return notification protocol
+ */
+ public String notificationProtocol() {
+ return get(NOTIFICATION_PROTOCOL, "udp");
+ }
+
+ /**
+ * Gets the Ip of the SNMP device.
+ *
+ * @return ip
+ */
+ public IpAddress ip() {
+ return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
+ }
+
+ /**
+ * Gets the port of the SNMP device.
+ *
+ * @return port
+ */
+ public int port() {
+ return get(PORT, extractIpPort().getValue());
+ }
+
+ /**
+ * Gets the notification port of the SNMP device.
+ *
+ * @return notification port
+ */
+ public int notificationPort() {
+ return get(NOTIFICATION_PORT, 0);
+ }
+
+ /**
+ * Gets the username of the SNMP device.
+ *
+ * @return username
+ */
+ public String username() {
+ return get(USERNAME, "");
+ }
+
+ /**
+ * Gets the password of the SNMP device.
+ *
+ * @return password
+ */
+ public String password() {
+ return get(PASSWORD, "");
+ }
+
+
+ private Pair<String, Integer> extractIpPort() {
+ String info = subject.uri().getSchemeSpecificPart();
+ int portSeparator = info.lastIndexOf(':');
+ if (portSeparator == -1) {
+ return Pair.of(info, DEFAULT_PORT);
+ }
+
+ String ip = info.substring(0, portSeparator);
+ int port = Integer.parseInt(info.substring(portSeparator + 1));
+ return Pair.of(ip, port);
+ }
+}
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
index 8ddbb6b..c02218e 100644
--- a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpController.java
@@ -38,6 +38,7 @@
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -73,6 +74,7 @@
}
@Override
+ @Deprecated
public ISnmpSession getSession(DeviceId deviceId) throws IOException {
if (!sessionMap.containsKey(deviceId)) {
SnmpDevice device = snmpDeviceMap.get(deviceId);
@@ -112,16 +114,32 @@
}
@Override
+ public SnmpDevice getDevice(URI uri) {
+ //this assumes that only one device is associated with one deviceId
+ return snmpDeviceMap.entrySet()
+ .stream().filter(p -> p.getValue()
+ .getSnmpHost().equals(uri.toString()))
+ .map(Map.Entry::getValue).findFirst().orElse(null);
+ }
+
+ @Override
public void removeDevice(DeviceId did) {
snmpDeviceMap.remove(did);
}
@Override
+ @Deprecated // in 1.14.0
public void addDevice(DeviceId did, SnmpDevice device) {
snmpDeviceMap.put(did, device);
}
@Override
+ public void addDevice(SnmpDevice device) {
+ log.info("Adding device {}", device.deviceId());
+ snmpDeviceMap.put(device.deviceId(), device);
+ }
+
+ @Override
public DefaultAlarm buildWalkFailedAlarm(DeviceId deviceId) {
long timeRaised = System.currentTimeMillis();
return new DefaultAlarm.Builder(
diff --git a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java
index b15277c..2442240 100644
--- a/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java
+++ b/protocols/snmp/ctl/src/main/java/org/onosproject/snmp/ctl/DefaultSnmpDevice.java
@@ -17,8 +17,15 @@
import org.onosproject.net.DeviceId;
import org.onosproject.snmp.SnmpDevice;
+import org.onosproject.snmp.SnmpDeviceConfig;
import org.slf4j.Logger;
+import org.snmp4j.Snmp;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.transport.DefaultTcpTransportMapping;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -33,28 +40,62 @@
private final Logger log = getLogger(DefaultSnmpDevice.class);
-
- private static final int DEFAULT_SNMP_PORT = 161;
-
private static final String SCHEME = "snmp";
private final String snmpHost;
private final DeviceId deviceId;
- private int snmpPort = DEFAULT_SNMP_PORT;
+ private final int snmpPort;
+ private final int notificationPort;
private final String username;
//Community is a conventional name for password in SNMP.
private final String community;
private boolean reachable = false;
+ private final String protocol;
+ private final String notificationProtocol;
+ private Snmp session;
- public DefaultSnmpDevice(String snmpHost, int snmpPort, String username, String community) {
-
+ public DefaultSnmpDevice(String snmpHost, int snmpPort,
+ String username, String community) {
+ this.protocol = GenericAddress.TYPE_UDP;
this.snmpHost = checkNotNull(snmpHost, "SNMP Device IP cannot be null");
- this.snmpPort = snmpPort;
+ this.snmpPort = checkNotNull(snmpPort, "SNMP Device port cannot be null");
+ this.notificationProtocol = GenericAddress.TYPE_UDP;
+ this.notificationPort = 0;
this.username = username;
this.community = community;
this.deviceId = createDeviceId();
+ initializeSession();
+ }
+
+ public DefaultSnmpDevice(SnmpDeviceConfig snmpDeviceConfig) {
+ checkNotNull(snmpDeviceConfig.ip(), "SNMP Device IP address cannot be null");
+ this.protocol = snmpDeviceConfig.protocol();
+ this.notificationProtocol = snmpDeviceConfig.notificationProtocol();
+ this.snmpHost = checkNotNull(snmpDeviceConfig.ip().toString(), "SNMP Device IP cannot be null");
+ this.snmpPort = checkNotNull(snmpDeviceConfig.port(), "SNMP Device port cannot be null");
+ this.notificationPort = checkNotNull(snmpDeviceConfig.notificationPort(),
+ "SNMP Device notification port cannot be null");
+ this.username = snmpDeviceConfig.username();
+ this.community = snmpDeviceConfig.password();
+ this.deviceId = createDeviceId();
+ initializeSession();
+ }
+
+ private void initializeSession() {
reachable = true;
+ try {
+ TransportMapping transport;
+ if (protocol == GenericAddress.TYPE_TCP) {
+ transport = new DefaultTcpTransportMapping();
+ } else {
+ transport = new DefaultUdpTransportMapping();
+ }
+ transport.listen();
+ session = new Snmp(transport);
+ } catch (IOException e) {
+ log.error("Failed to connect to device: ", e);
+ }
}
@Override
@@ -64,6 +105,11 @@
}
@Override
+ public Snmp getSession() {
+ return session;
+ }
+
+ @Override
public void disconnect() {
log.info("disconnect");
reachable = false;
@@ -86,6 +132,11 @@
}
@Override
+ public int getNotificationPort() {
+ return notificationPort;
+ }
+
+ @Override
public String getUsername() {
return username;
}
@@ -100,6 +151,16 @@
return deviceId;
}
+ @Override
+ public String getProtocol() {
+ return protocol;
+ }
+
+ @Override
+ public String getNotificationProtocol() {
+ return notificationProtocol;
+ }
+
private DeviceId createDeviceId() {
String additionalSsp = new StringBuilder(
snmpHost).append(":")
@@ -114,4 +175,5 @@
throw new IllegalArgumentException("Can't create device ID from " + additionalSsp, e);
}
}
+
}
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
index a649633..e5a630a 100644
--- a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpControllerTest.java
@@ -119,4 +119,4 @@
return snmpSession;
}
}
-}
\ No newline at end of file
+}
diff --git a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
index da26581..e9bda9f 100644
--- a/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
+++ b/protocols/snmp/ctl/src/test/java/org/onosproject/snmp/ctl/DefaultSnmpDeviceTest.java
@@ -32,12 +32,15 @@
private final String community = "test";
private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
private final String deviceInfo = "host: 1.1.1.1. port: 1";
+ private final String defaultProtocol = "udp";
+ private final String tcpProtocol = "tcp";
DefaultSnmpDevice device = new DefaultSnmpDevice("1.1.1.1", 1, "test", "test");
@Test
public void basics() throws Exception {
assertTrue("Device should be reachable", device.isReachable());
+ assertEquals("Incorrect protocol", defaultProtocol, device.getProtocol());
assertEquals("Incorrect host", snmpHost, device.getSnmpHost());
assertEquals("Incorrect port", snmpPort, device.getSnmpPort());
assertEquals("Incorrect username", username, device.getUsername());
@@ -46,6 +49,5 @@
assertEquals("Incorrect deviceInfo", deviceInfo, device.deviceInfo());
device.disconnect();
assertFalse("Device should not be reachable", device.isReachable());
-
}
}