[ONOS-4294] AlarmConsumer for Lumentum e-cord device

Change-Id: I653c5136ae5d162e04d67d1a54ce0dd4b95b3866
diff --git a/drivers/lumentum/BUCK b/drivers/lumentum/BUCK
index 50c1c26..6e90129 100644
--- a/drivers/lumentum/BUCK
+++ b/drivers/lumentum/BUCK
@@ -7,6 +7,8 @@
     '//lib:CORE_DEPS',
     '//lib:org.apache.servicemix.bundles.snmp4j',
     '//drivers/utilities:onos-drivers-utilities',
+    '//protocols/snmp/api:onos-snmp-api',
+    '//incubator/api:onos-incubator-api',
 ]
 
 osgi_jar(
diff --git a/drivers/lumentum/features.xml b/drivers/lumentum/features.xml
index 7ded641..3eabdb5 100644
--- a/drivers/lumentum/features.xml
+++ b/drivers/lumentum/features.xml
@@ -22,5 +22,7 @@
 
         <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.snmp4j/2.3.4_1</bundle>
 
+        <bundle>mvn:${project.groupId}/onos-snmp-api/${project.version}</bundle>
+
     </feature>
 </features>
diff --git a/drivers/lumentum/pom.xml b/drivers/lumentum/pom.xml
index 3b71d80..fef1cab 100644
--- a/drivers/lumentum/pom.xml
+++ b/drivers/lumentum/pom.xml
@@ -36,7 +36,8 @@
         <onos.app.title>Lumentum Device Drivers</onos.app.title>
         <onos.app.url>http://onosproject.org</onos.app.url>
         <onos.app.requires>
-            org.onosproject.snmp
+            org.onosproject.snmp,
+            org.onosproject.faultmanagement
         </onos.app.requires>
     </properties>
 
@@ -46,5 +47,10 @@
             <artifactId>org.apache.servicemix.bundles.snmp4j</artifactId>
             <version>2.3.4_1</version>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-snmp-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumAlarmConsumer.java b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumAlarmConsumer.java
new file mode 100644
index 0000000..acaaa19
--- /dev/null
+++ b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumAlarmConsumer.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2016-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.
+ */
+
+package org.onosproject.drivers.lumentum;
+
+import com.google.common.collect.ImmutableList;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer;
+import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
+import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.snmp.SnmpController;
+import org.onosproject.snmp.SnmpDevice;
+import org.slf4j.Logger;
+import org.snmp4j.smi.OID;
+import org.snmp4j.smi.VariableBinding;
+import org.snmp4j.util.TreeEvent;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Alarm Consumer for Lumentum devices.
+ */
+public class LumentumAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String ALARM_TABLE = ".1.3.6.1.4.1.46184.1.3.2";
+    private static final String ALARM_ID = ".1.3.6.1.4.1.46184.1.3.2.1.1";
+    private static final OID ALARMS_TABLE_OID = new OID(ALARM_TABLE);
+    private static final OID ALARMS_ID_OID = new OID(ALARM_ID);
+    private LumentumSnmpDevice snmp;
+
+    @Override
+    public List<Alarm> consumeAlarms() {
+
+        SnmpController controller = checkNotNull(handler().get(SnmpController.class));
+        List<Alarm> alarms = new ArrayList<>();
+        DeviceId deviceId = handler().data().deviceId();
+        SnmpDevice device = controller.getDevice(deviceId);
+        try {
+            snmp = new LumentumSnmpDevice(device.getSnmpHost(), device.getSnmpPort());
+        } catch (IOException e) {
+            log.error("Failed to connect to device: ", e);
+        }
+
+        // Gets the alarm table and for each entry get the ID and create the proper alarm.
+        snmp.get(ALARMS_TABLE_OID).stream()
+                .forEach(alarm -> snmp.get(ALARMS_ID_OID).forEach(alarmIdEvent -> {
+                    int alarmId = getAlarmId(alarmIdEvent);
+                    alarms.add(new DefaultAlarm.Builder(deviceId, getMessage(alarmId),
+                                                        getSeverity(alarmId),
+                                                        System.currentTimeMillis())
+                                       .withId(AlarmId.alarmId(alarmId))
+                                       .build());
+                }));
+        return ImmutableList.copyOf(alarms);
+    }
+
+    //Walks the tree and retrieves the alarmId
+    private int getAlarmId(TreeEvent treeEvents) {
+        VariableBinding[] varBindings = treeEvents.getVariableBindings();
+        for (VariableBinding varBinding : varBindings) {
+            return varBinding.getVariable().toInt();
+        }
+        return -1;
+    }
+
+    //Returns the severity level.
+    private SeverityLevel getSeverity(int alarmId) {
+        switch (alarmId) {
+            case 14:
+                return SeverityLevel.INDETERMINATE;
+            default:
+                return SeverityLevel.MAJOR;
+        }
+    }
+
+    //Returns a string message based on the id of the alarm as per .mib file.
+    private String getMessage(int alarmId) {
+        switch (alarmId) {
+            case 1:
+                return "Port Los";
+            case 2:
+                return "Port Degrade";
+            case 3:
+                return "Port High Power";
+            case 4:
+                return "Ta Failure";
+            case 5:
+                return "Force Apr";
+            case 6:
+                return "Force Shutoff";
+            case 7:
+                return "Gain Oor";
+            case 8:
+                return "Low Orl";
+            case 9:
+                return "Apr";
+            case 10:
+                return "Los Shutoff";
+            case 11:
+                return "Amp Degrade";
+            case 12:
+                return "Channel Los";
+            case 13:
+                return "Channel Degrade";
+            case 14:
+                return "Unsupported";
+            default:
+                return "Unknown";
+        }
+    }
+}
diff --git a/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleDriver.java b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleProgrammable.java
similarity index 98%
rename from drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleDriver.java
rename to drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleProgrammable.java
index 56ca057..7a25ad3 100644
--- a/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleDriver.java
+++ b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumFlowRuleProgrammable.java
@@ -58,10 +58,10 @@
 import static com.google.common.base.Preconditions.checkArgument;
 
 // TODO: need to convert between OChSignal and XC channel number
-public class LumentumFlowRuleDriver extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
+public class LumentumFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
 
     private static final Logger log =
-            LoggerFactory.getLogger(LumentumFlowRuleDriver.class);
+            LoggerFactory.getLogger(LumentumFlowRuleProgrammable.class);
 
     // Default values
     private static final int DEFAULT_TARGET_GAIN_PREAMP = 150;
diff --git a/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumSnmpDevice.java b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumSnmpDevice.java
index 526173d..dd16f8b 100644
--- a/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumSnmpDevice.java
+++ b/drivers/lumentum/src/main/java/org/onosproject/drivers/lumentum/LumentumSnmpDevice.java
@@ -40,7 +40,7 @@
 
 /**
  * Quick and dirty device abstraction for SNMP-based Lumentum devices.
- *
+ * <p>
  * TODO: Refactor once SnmpDevice is finished
  */
 public class LumentumSnmpDevice {
@@ -67,6 +67,14 @@
         String ipAddress = deviceComponents[1];
         String port = deviceComponents[2];
 
+        createDevice(ipAddress, Integer.parseInt(port));
+    }
+
+    public LumentumSnmpDevice(String ipAddress, int port) throws IOException {
+        createDevice(ipAddress, port);
+    }
+
+    private void createDevice(String ipAddress, int port) throws IOException {
         Address targetAddress = GenericAddress.parse("udp:" + ipAddress + "/" + port);
         TransportMapping transport = new DefaultUdpTransportMapping();
         transport.listen();
diff --git a/drivers/lumentum/src/main/resources/lumentum-drivers.xml b/drivers/lumentum/src/main/resources/lumentum-drivers.xml
index cbbc58d..ecdbc3f 100644
--- a/drivers/lumentum/src/main/resources/lumentum-drivers.xml
+++ b/drivers/lumentum/src/main/resources/lumentum-drivers.xml
@@ -21,7 +21,9 @@
         <behaviour api="org.onosproject.net.behaviour.LambdaQuery"
                    impl="org.onosproject.drivers.lumentum.LambdaQueryLumentumRoadm"/>
         <behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
-                   impl="org.onosproject.drivers.lumentum.LumentumFlowRuleDriver"/>
+                   impl="org.onosproject.drivers.lumentum.LumentumFlowRuleProgrammable"/>
+        <behaviour api="org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer"
+                   impl="org.onosproject.drivers.lumentum.LumentumAlarmConsumer"/>
     </driver>
 </drivers>