ONOS-5917 SNMP Alarm Provider
Change-Id: I9eb39021584f3b1a5baed9dfa638569818901197
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DeviceAlarmConfig.java b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DeviceAlarmConfig.java
new file mode 100644
index 0000000..4baa357
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/faultmanagement/alarm/DeviceAlarmConfig.java
@@ -0,0 +1,52 @@
+/*
+ * 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.incubator.net.faultmanagement.alarm;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.net.driver.HandlerBehaviour;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Abstraction of a device behaviour capable of translating a list of
+ * alarms from a device. Also provides a method to configure the address where to
+ * send the alarms/traps/notifications.
+ */
+public interface DeviceAlarmConfig extends HandlerBehaviour {
+
+ /**
+ * Configures the device to send alarms to a particular Ip and port combination.
+ *
+ * @param address address to wich the device should send alarms
+ * @param port port on which the controller is listening
+ * @param protocol tcp or udp
+ * @return boolean true if the device was properly configured
+ */
+ boolean configureDevice(IpAddress address, int port, String protocol);
+
+ /**
+ * Returns the list of translated alarms from device-specific representation
+ * to ONOS alarms.
+ *
+ * @param unparsedAlarms alarms arrived from the device depending on protocol
+ * @param <T> type of object given from the device
+ * @return list of alarms consumed from the device
+ */
+ <T> Set<Alarm> translateAlarms(List<T> unparsedAlarms);
+
+}
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/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java
similarity index 62%
rename from providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java
rename to protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java
index f1085c0..b39180a 100644
--- a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceConfig.java
+++ b/protocols/snmp/api/src/main/java/org/onosproject/snmp/SnmpDeviceConfig.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package org.onosproject.provider.snmp.device.impl;
+package org.onosproject.snmp;
import com.google.common.annotations.Beta;
import org.apache.commons.lang3.tuple.Pair;
@@ -28,8 +28,12 @@
@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";
@@ -40,6 +44,24 @@
}
/**
+ * 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
@@ -58,6 +80,15 @@
}
/**
+ * 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
@@ -77,13 +108,14 @@
private Pair<String, Integer> extractIpPort() {
- String info = subject.toString();
- if (info.startsWith(SnmpDeviceProvider.SCHEME)) {
- //+1 is due to length of colon separator
- String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
- int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
- return Pair.of(ip, port);
+ String info = subject.uri().getSchemeSpecificPart();
+ int portSeparator = info.lastIndexOf(':');
+ if (portSeparator == -1) {
+ return Pair.of(info, DEFAULT_PORT);
}
- return null;
+
+ String ip = info.substring(0, portSeparator);
+ int port = Integer.parseInt(info.substring(portSeparator + 1));
+ return Pair.of(ip, port);
}
-}
\ No newline at end of file
+}
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());
-
}
}
diff --git a/providers/snmp/BUCK b/providers/snmp/BUCK
index 24d8a97..598515d 100644
--- a/providers/snmp/BUCK
+++ b/providers/snmp/BUCK
@@ -1,5 +1,6 @@
BUNDLES = [
'//providers/snmp/device:onos-providers-snmp-device',
+ '//providers/snmp/alarm:onos-providers-snmp-alarm',
'//protocols/snmp/api:onos-protocols-snmp-api',
'//protocols/snmp/ctl:onos-protocols-snmp-ctl',
'//lib:org.apache.servicemix.bundles.snmp4j',
@@ -16,4 +17,5 @@
included_bundles = BUNDLES,
description = 'Provides means for ONOS to discover and trigger the initial handshake procedure ' +
'with SNMP enabled devices from information given by network configuration.',
+ required_apps = [ 'org.onosproject.faultmanagement'],
)
diff --git a/providers/snmp/alarm/BUCK b/providers/snmp/alarm/BUCK
new file mode 100644
index 0000000..a5f0c4b
--- /dev/null
+++ b/providers/snmp/alarm/BUCK
@@ -0,0 +1,13 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:JACKSON',
+ '//incubator/api:onos-incubator-api',
+ '//protocols/snmp/api:onos-protocols-snmp-api',
+ '//protocols/snmp/ctl:onos-protocols-snmp-ctl',
+ '//providers/snmp/device:onos-providers-snmp-device',
+ '//lib:org.apache.servicemix.bundles.snmp4j',
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+)
diff --git a/providers/snmp/alarm/pom.xml b/providers/snmp/alarm/pom.xml
new file mode 100644
index 0000000..bebe648
--- /dev/null
+++ b/providers/snmp/alarm/pom.xml
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present Open Networking Laboratory
+ ~
+ ~ 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.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-snmp-providers</artifactId>
+ <version>1.9.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>onos-snmp-provider-alarm</artifactId>
+ <packaging>bundle</packaging>
+
+ <description>ONOS SNMP protocol alarm provider</description>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-snmp-api</artifactId>
+ <version>1.9.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-snmp-ctl</artifactId>
+ <version>1.9.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicemix.bundles</groupId>
+ <artifactId>org.apache.servicemix.bundles.snmp4j</artifactId>
+ <version>2.3.4_1</version>
+ <exclusions>
+ <exclusion>
+ <artifactId>log4j</artifactId>
+ <groupId>log4j</groupId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>snmp-core</artifactId>
+ <version>1.3-20161021.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>mibbler-mibs-bti7000</artifactId>
+ <version>1.0-20151221.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>mibbler-mibs-net-snmp</artifactId>
+ <version>1.0-20151221.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-core-net</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-api</artifactId>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-snmp-provider-device</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+</project>
\ No newline at end of file
diff --git a/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProvider.java b/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProvider.java
new file mode 100644
index 0000000..3a3c964
--- /dev/null
+++ b/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProvider.java
@@ -0,0 +1,295 @@
+/*
+ * 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.provider.snmp.alarm.impl;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderRegistry;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderService;
+import org.onosproject.incubator.net.faultmanagement.alarm.DeviceAlarmConfig;
+import org.onosproject.mastership.MastershipEvent;
+import org.onosproject.mastership.MastershipListener;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DefaultDriverData;
+import org.onosproject.net.driver.DefaultDriverHandler;
+import org.onosproject.net.driver.Driver;
+import org.onosproject.net.driver.DriverData;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.provider.AbstractProvider;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.snmp.SnmpController;
+import org.onosproject.snmp.SnmpDevice;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.snmp4j.CommandResponder;
+import org.snmp4j.CommandResponderEvent;
+import org.snmp4j.Snmp;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.smi.Address;
+import org.snmp4j.smi.GenericAddress;
+import org.snmp4j.smi.TcpAddress;
+import org.snmp4j.smi.UdpAddress;
+import org.snmp4j.transport.DefaultTcpTransportMapping;
+import org.snmp4j.transport.DefaultUdpTransportMapping;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provider which will listen for traps generated SNMP devices and configure
+ * the devices with the Ip and Protocol fo the end point to send traps to.
+ */
+@Component(immediate = true)
+public class SnmpAlarmProvider extends AbstractProvider
+ implements AlarmProvider {
+
+ public static final String COLON = ":";
+ private final Logger log = getLogger(SnmpAlarmProvider.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected SnmpController controller;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected AlarmProviderRegistry providerRegistry;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DriverService driverService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected MastershipService mastershipService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected DeviceService deviceService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ClusterService clusterService;
+
+ private static final String APP_NAME = "org.onosproject.snmp";
+ private static final String SCHEME = "snmp";
+
+ protected AlarmProviderService providerService;
+
+ protected ApplicationId appId;
+
+ protected final DeviceListener deviceListener = new InternalDeviceListener();
+
+ protected final MastershipListener mastershipListener = new InternalMastershipListener();
+
+ protected final CommandResponder internalCommandResponder = new InternalCommandResponder();
+
+ private IpAddress localIp;
+
+ private final Map<Integer, Snmp> portSessionMap = new HashMap<>();
+
+ private Snmp snmp;
+
+
+ /**
+ * Creates a provider with the supplier identifier.
+ */
+ public SnmpAlarmProvider() {
+ super(new ProviderId("snmp", "org.onosproject.provider.alarm"));
+ }
+
+ @Activate
+ public void activate(ComponentContext context) {
+
+ providerService = providerRegistry.register(this);
+ appId = coreService.registerApplication(APP_NAME);
+ deviceService.addListener(deviceListener);
+ mastershipService.addListener(mastershipListener);
+ controller.getDevices().stream().forEach(d -> {
+ triggerProbe(d.deviceId());
+ configureListeningConnection(d);
+ });
+ localIp = clusterService.getLocalNode().ip();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate(ComponentContext context) {
+ for (Map.Entry<Integer, Snmp> entry : portSessionMap.entrySet()) {
+ try {
+ entry.getValue().close();
+ } catch (IOException e) {
+ log.warn("Can't close SNMP notification session on Ip {} and Port {}",
+ localIp, entry.getKey(), e);
+ }
+ }
+ mastershipService.addListener(mastershipListener);
+ deviceService.removeListener(deviceListener);
+ providerRegistry.unregister(this);
+ providerService = null;
+ log.info("Stopped");
+ }
+
+ @Modified
+ public void modified() {
+ log.info("Modified");
+ }
+
+ @Override
+ public void triggerProbe(DeviceId deviceId) {
+ requestTraps(deviceId);
+ }
+
+ private void configureListeningConnection(SnmpDevice device) {
+ int notificationPort = device.getNotificationPort();
+ if (notificationPort == 0 || portSessionMap.containsKey(notificationPort)) {
+ return;
+ }
+ String protocol = device.getProtocol();
+ try {
+ Address listenAddress = GenericAddress.parse(protocol +
+ ":" + localIp.toString() +
+ "/" + notificationPort);
+ TransportMapping tm;
+ if (protocol == GenericAddress.TYPE_TCP) {
+ tm = new DefaultTcpTransportMapping((TcpAddress) listenAddress);
+ } else {
+ tm = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
+ }
+ snmp = new Snmp(tm);
+ snmp.addCommandResponder(internalCommandResponder);
+ snmp.listen();
+ log.info("Initiated SNMP notification connection on Ip {} and Port {}",
+ localIp.toString(), notificationPort);
+ } catch (IOException e) {
+ log.error("Can't initiate SNMP transport on Protocol {}, Ip {} and Port {}",
+ protocol, localIp, notificationPort, e);
+ return;
+ }
+
+ portSessionMap.put(notificationPort, snmp);
+ }
+
+ private boolean requestTraps(DeviceId deviceId) {
+ SnmpDevice device = controller.getDevice(deviceId);
+ DeviceAlarmConfig alarmTranslator = getAlarmTranslator(device);
+ if (alarmTranslator != null) {
+ return alarmTranslator.configureDevice(localIp, device.getNotificationPort(),
+ device.getNotificationProtocol());
+ } else {
+ log.warn("Device {} does not support alarms.", deviceId);
+ return false;
+ }
+ }
+
+ private DeviceAlarmConfig getAlarmTranslator(SnmpDevice device) {
+ Driver deviceDriver = driverService.getDriver(device.deviceId());
+ if (deviceDriver != null && deviceDriver.hasBehaviour(DeviceAlarmConfig.class)) {
+ DriverData driverData = new DefaultDriverData(deviceDriver, device.deviceId());
+ DeviceAlarmConfig alarmTranslator = deviceDriver
+ .createBehaviour(driverData, DeviceAlarmConfig.class);
+ alarmTranslator.setHandler(new DefaultDriverHandler(driverData));
+ return alarmTranslator;
+ } else {
+ log.warn("Device does not support alarm {}", device.deviceId());
+ }
+ return null;
+ }
+
+ private class InternalCommandResponder implements CommandResponder {
+ @Override
+ public void processPdu(CommandResponderEvent event) {
+ try {
+ log.debug("received trap {}", event);
+ String[] deviceInfo = event.getPeerAddress().toString().split("/");
+
+ //TODO This should be done via device service.
+ //searching only for Ip since the port from which the trap is sent
+ // could be different from the one used for SNMP
+ SnmpDevice device = controller.getDevice(new URI(deviceInfo[0]));
+ if (device != null) {
+ DeviceId deviceId = DeviceId.deviceId(SCHEME + COLON + deviceInfo[0]
+ + COLON + device.getSnmpPort());
+ DeviceAlarmConfig alarmTranslator = getAlarmTranslator(controller.getDevice(deviceId));
+ if (alarmTranslator != null) {
+ Set<Alarm> alarms = alarmTranslator.translateAlarms(ImmutableList.of(event));
+ providerService.updateAlarmList(deviceId, alarms);
+ } else {
+ log.warn("Device {} does not support alarm", device.deviceId());
+ }
+ } else {
+ log.error("Device {} does not exist in ONOS SNMP subsystem", deviceInfo[0]);
+ }
+ //Catching generic exception due to otherwise hidden URISyntax
+ } catch (Exception e) {
+ log.error("Exception while processing PDU {}", e);
+ }
+ }
+ }
+
+ private class InternalDeviceListener implements DeviceListener {
+
+ @Override
+ public void event(DeviceEvent event) {
+ DeviceId deviceId = event.subject().id();
+ switch (event.type()) {
+ case DEVICE_ADDED:
+ triggerProbe(deviceId);
+ case DEVICE_UPDATED:
+ triggerProbe(deviceId);
+ default:
+ break;
+ }
+ }
+
+ @Override
+ public boolean isRelevant(DeviceEvent event) {
+ return event.subject().id().toString().startsWith(SCHEME)
+ && mastershipService.isLocalMaster(event.subject().id());
+ }
+ }
+
+ private class InternalMastershipListener implements MastershipListener {
+
+ @Override
+ public void event(MastershipEvent event) {
+ triggerProbe(event.subject());
+ }
+
+ @Override
+ public boolean isRelevant(MastershipEvent event) {
+ return event.subject().toString().startsWith(SCHEME)
+ && mastershipService.isLocalMaster(event.subject());
+ }
+ }
+}
diff --git a/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/package-info.java b/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/package-info.java
new file mode 100644
index 0000000..ee22ea7
--- /dev/null
+++ b/providers/snmp/alarm/src/main/java/org/onosproject/provider/snmp/alarm/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provider that uses SNMP capability to configure and receive alarms from devices.
+ */
+package org.onosproject.provider.snmp.alarm.impl;
diff --git a/providers/snmp/alarm/src/test/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProviderTest.java b/providers/snmp/alarm/src/test/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProviderTest.java
new file mode 100644
index 0000000..2ac50b8
--- /dev/null
+++ b/providers/snmp/alarm/src/test/java/org/onosproject/provider/snmp/alarm/impl/SnmpAlarmProviderTest.java
@@ -0,0 +1,25 @@
+/*
+ * 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.provider.snmp.alarm.impl;
+
+/**
+ * Testing class for SnmpDeviceProvider.
+ */
+public class SnmpAlarmProviderTest {
+
+ //TODO unitests
+}
diff --git a/providers/snmp/app/app.xml b/providers/snmp/app/app.xml
index 848ceff..fd39042 100644
--- a/providers/snmp/app/app.xml
+++ b/providers/snmp/app/app.xml
@@ -17,9 +17,10 @@
<app name="org.onosproject.snmp" origin="BTI Systems" version="${project.version}"
category="Provider" url="http://onosproject.org" title="SNMP Provider"
featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
- features="${project.artifactId}">
+ features="${project.artifactId}" apps="org.onosproject.faultmanagement">
<description>${project.description}</description>
<artifact>mvn:${project.groupId}/onos-snmp-provider-device/${project.version}</artifact>
+ <artifact>mvn:${project.groupId}/onos-snmp-provider-alarm/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-snmp-api/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-snmp-ctl/${project.version}</artifact>
diff --git a/providers/snmp/app/features.xml b/providers/snmp/app/features.xml
index 212ddca..a130030 100644
--- a/providers/snmp/app/features.xml
+++ b/providers/snmp/app/features.xml
@@ -22,6 +22,7 @@
<bundle>mvn:${project.groupId}/onos-snmp-api/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-snmp-ctl/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-snmp-provider-device/${project.version}</bundle>
+ <bundle>mvn:${project.groupId}/onos-snmp-provider-alarm/${project.version}</bundle>
<bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.snmp4j/2.3.4_1</bundle>
<bundle>mvn:org.onosproject/snmp-core/1.3-20161021.1</bundle>
diff --git a/providers/snmp/app/pom.xml b/providers/snmp/app/pom.xml
index 51c26f7..20d29d0 100644
--- a/providers/snmp/app/pom.xml
+++ b/providers/snmp/app/pom.xml
@@ -36,6 +36,11 @@
<artifactId>onos-snmp-provider-device</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.onosproject</groupId>
+ <artifactId>onos-snmp-provider-alarm</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</project>
diff --git a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
index c4fdcd1..c81ba59 100644
--- a/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
+++ b/providers/snmp/device/src/main/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProvider.java
@@ -48,6 +48,7 @@
import org.onosproject.net.provider.ProviderId;
import org.onosproject.snmp.SnmpController;
import org.onosproject.snmp.SnmpDevice;
+import org.onosproject.snmp.SnmpDeviceConfig;
import org.onosproject.snmp.ctl.DefaultSnmpDevice;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
@@ -165,8 +166,7 @@
deviceSubjects.forEach(deviceId -> {
SnmpDeviceConfig config =
netCfgService.getConfig(deviceId, SnmpDeviceConfig.class);
- buildDevice(new DefaultSnmpDevice(config.ip().toString(),
- config.port(), config.username(), config.password()));
+ buildDevice(new DefaultSnmpDevice(config));
});
}
diff --git a/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java b/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java
index 5dfedd3..a3534ec 100644
--- a/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java
+++ b/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpControllerAdapter.java
@@ -23,6 +23,7 @@
import org.onosproject.snmp.SnmpDevice;
import java.io.IOException;
+import java.net.URI;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -44,6 +45,11 @@
}
@Override
+ public SnmpDevice getDevice(URI uri) {
+ return null;
+ }
+
+ @Override
public void removeDevice(DeviceId deviceId) {
devices.remove(deviceId);
}
@@ -54,6 +60,11 @@
}
@Override
+ public void addDevice(SnmpDevice device) {
+ devices.put(device.deviceId(), device);
+ }
+
+ @Override
public ISnmpSession getSession(DeviceId deviceId) throws IOException {
return null;
}
diff --git a/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java b/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java
index 49374cc..cfd39db 100644
--- a/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java
+++ b/providers/snmp/device/src/test/java/org/onosproject/provider/snmp/device/impl/SnmpDeviceProviderTest.java
@@ -52,6 +52,7 @@
import org.onosproject.net.driver.DriverServiceAdapter;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.snmp.SnmpController;
+import org.onosproject.snmp.SnmpDeviceConfig;
import java.io.InputStream;
import java.util.HashMap;
diff --git a/providers/snmp/pom.xml b/providers/snmp/pom.xml
index 429dcf4..c952ad2 100644
--- a/providers/snmp/pom.xml
+++ b/providers/snmp/pom.xml
@@ -33,5 +33,6 @@
<modules>
<module>device</module>
<module>app</module>
+ <module>alarm</module>
</modules>
</project>