Add polatis netconf alarm consumer
Change-Id: I9aca49caa347213bec0c7e01f594aa26e8d6ee3e
diff --git a/drivers/polatis/netconf/BUCK b/drivers/polatis/netconf/BUCK
index 8eee937..061ae0d 100644
--- a/drivers/polatis/netconf/BUCK
+++ b/drivers/polatis/netconf/BUCK
@@ -5,6 +5,7 @@
'//protocols/netconf/api:onos-protocols-netconf-api',
'//lib:onos-yang-model',
'//apps/optical-model:onos-apps-optical-model',
+ '//incubator/api:onos-incubator-api',
]
APPS = [
@@ -12,6 +13,7 @@
'org.onosproject.models.common',
'org.onosproject.models.polatis',
'org.onosproject.optical-model',
+ 'org.onosproject.faultmanagement',
]
TEST_DEPS = [
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisAlarmConsumer.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisAlarmConsumer.java
new file mode 100644
index 0000000..df5318f
--- /dev/null
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisAlarmConsumer.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2018 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.drivers.polatis.netconf;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.commons.configuration.HierarchicalConfiguration;
+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.net.driver.DriverHandler;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.mastership.MastershipService;
+
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.time.format.DateTimeFormatter;
+import java.time.DateTimeException;
+import java.time.OffsetDateTime;
+
+import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_SYSTEMALARMS;
+import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_SYSTEMALARMS_XMLNS;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Polatis specific implementation to provide a list of current alarms.
+ */
+public class PolatisAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer {
+ private final Logger log = getLogger(getClass());
+
+ private static final String ALARM_TIME = "alarm-time";
+ private static final String ALARM_TYPE = "alarm-type";
+ private static final String ALARM_MESSAGE = "alarm-message";
+
+ private DeviceId deviceId;
+
+ @Override
+ public List<Alarm> consumeAlarms() {
+ DriverHandler handler = handler();
+ NetconfController controller = handler.get(NetconfController.class);
+ checkNotNull(controller, "Netconf controller is null");
+
+ MastershipService mastershipService = handler.get(MastershipService.class);
+ deviceId = handler.data().deviceId();
+
+ List<Alarm> alarms = new ArrayList<>();
+ if (!mastershipService.isLocalMaster(deviceId)) {
+ log.warn("Not master for {} Use {} to execute command",
+ deviceId,
+ mastershipService.getMasterFor(deviceId));
+ return ImmutableList.copyOf(alarms);
+ }
+
+ try {
+ String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS);
+ String reply = controller.getDevicesMap()
+ .get(deviceId)
+ .getSession()
+ .get(request, null);
+ if (reply != null) {
+ alarms = parseAlarms(reply);
+ }
+ } catch (NetconfException e) {
+ log.error("Error reading alarms for device {} exception {}", deviceId, e);
+ }
+
+ return ImmutableList.copyOf(alarms);
+ }
+
+ private List<Alarm> parseAlarms(String content) {
+ List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS);
+ List<Alarm> alarms = new ArrayList<>();
+ for (HierarchicalConfiguration alarm : subtrees) {
+ alarms.add(parseAlarm(alarm));
+ }
+ return alarms;
+ }
+
+ private Alarm parseAlarm(HierarchicalConfiguration cfg) {
+ boolean cleared = false;
+ // TODO: Use the type for severity or in the description?
+ String alarmType = cfg.getString(ALARM_TYPE);
+ String alarmMessage = cfg.getString(ALARM_MESSAGE);
+ SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE;
+ long timeRaised = getTimeRaised(cfg);
+ DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder(
+ AlarmId.alarmId(deviceId, Long.toString(timeRaised)),
+ deviceId, alarmMessage, alarmLevel, timeRaised);
+ return alarmBuilder.build();
+ }
+
+ private long getTimeRaised(HierarchicalConfiguration cfg) {
+ long timeRaised;
+
+ String alarmTime = cfg.getString(ALARM_TIME);
+ try {
+ OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
+ timeRaised = date.toInstant().toEpochMilli();
+ return timeRaised;
+ } catch (DateTimeException e) {
+ log.error("Cannot parse exception {} {}", alarmTime, e);
+ }
+ return System.currentTimeMillis();
+ }
+}
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java
index 1cb8031..1516912 100644
--- a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisNetconfUtility.java
@@ -40,18 +40,23 @@
public final class PolatisNetconfUtility {
public static final String KEY_XMLNS = "xmlns=\"http://www.polatis.com/yang/optical-switch\"";
+ public static final String KEY_POLATIS_XMLNS = "xmlns=\"http://www.polatis.com/yang/polatis-switch\"";
public static final String KEY_DATA = "data";
public static final String KEY_PORT = "port";
public static final String KEY_PORTID = "port-id";
public static final String KEY_PORTCONFIG = "port-config";
+ public static final String KEY_SYSTEMALARMS = "system-alarms";
+ public static final String KEY_ALARM = "alarm";
public static final String KEY_CONNS = "cross-connects";
public static final String KEY_PRODINF = "product-information";
public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS);
+ public static final String KEY_SYSTEMALARMS_XMLNS = String.format("%s %s", KEY_SYSTEMALARMS, KEY_POLATIS_XMLNS);
public static final String KEY_CONNS_XMLNS = String.format("%s %s", KEY_CONNS, KEY_XMLNS);
public static final String KEY_PRODINF_XMLNS = String.format("%s %s", KEY_PRODINF, KEY_XMLNS);
public static final String KEY_DATA_CONNS = String.format("%s.%s", KEY_DATA, KEY_CONNS);
public static final String KEY_DATA_PRODINF = String.format("%s.%s", KEY_DATA, KEY_PRODINF);
public static final String KEY_DATA_PORTCONFIG = String.format("%s.%s.%s", KEY_DATA, KEY_PORTCONFIG, KEY_PORT);
+ public static final String KEY_DATA_SYSTEMALARMS = String.format("%s.%s.%s", KEY_DATA, KEY_SYSTEMALARMS, KEY_ALARM);
public static final String KEY_OPM = "opm-power";
public static final String KEY_OPM_XMLNS = String.format("%s %s", KEY_OPM, KEY_XMLNS);
public static final String KEY_DATA_OPM = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT);
diff --git a/drivers/polatis/netconf/src/main/resources/polatis-drivers.xml b/drivers/polatis/netconf/src/main/resources/polatis-drivers.xml
index eb348bd..88a9844 100644
--- a/drivers/polatis/netconf/src/main/resources/polatis-drivers.xml
+++ b/drivers/polatis/netconf/src/main/resources/polatis-drivers.xml
@@ -25,6 +25,8 @@
impl="org.onosproject.drivers.polatis.netconf.PolatisPowerConfig"/>
<behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
impl="org.onosproject.drivers.polatis.netconf.PolatisFlowRuleProgrammable"/>
+ <behaviour api="org.onosproject.incubator.net.faultmanagement.alarm.AlarmConsumer"
+ impl="org.onosproject.drivers.polatis.netconf.PolatisAlarmConsumer"/>
<property name="uiType">policon</property>
</driver>
</drivers>