[ONOS-4849] NETCONF function for FUJITSU OLT #6

- Add alart command for FUJITSU OLT
   volt-notification-alertfilter <netconf:target>
   volt-notification-setalertfilter <netconf:target> <alert-severity>
   volt-notification-subscribe <netconf:target> {disable}
- Update fujitsu-drivers.xml and shell-config.xml in FUJITSU directory
- Apply Yuta's and Andrea's suggestion to startSubscription method and other methods which related to startSubscription method.
  -> Remove startSubscriptionConnection() and createSubscriptionString() method in 4th patch-set.
  -> Modify cosmetic issue related to Static-string.
  -> Update "No replay" to "No reply" in VoltGetPonLinksCommand.java

Change-Id: I2c8d5484ea0ff9f0b1b970fe8b183bec12193c46
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltAlertConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltAlertConfig.java
new file mode 100644
index 0000000..fbefb5a
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltAlertConfig.java
@@ -0,0 +1,162 @@
+/*
+ * 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.fujitsu;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.netconf.NetconfController;
+import org.onosproject.mastership.MastershipService;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableSet;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation to get and set parameters available in vOLT
+ * through the Netconf protocol.
+ */
+public class FujitsuVoltAlertConfig extends AbstractHandlerBehaviour
+        implements VoltAlertConfig {
+
+    private final Logger log = getLogger(FujitsuVoltAlertConfig.class);
+    private static final String VOLT_ALERTS = "volt-alerts";
+    private static final String ALERT_FILTER = "alert-filter";
+    private static final String NOTIFY_ALERT = "notify-alert";
+    private final Set<String> severityLevels = ImmutableSet.of(
+            "none", "info", "minor", "major", "critical");
+    private static final String DISABLE = "disable";
+
+
+    @Override
+    public String getAlertFilter() {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        MastershipService mastershipService = handler.get(MastershipService.class);
+        DeviceId ncDeviceId = handler.data().deviceId();
+        checkNotNull(controller, "Netconf controller is null");
+        String reply = null;
+
+        if (!mastershipService.isLocalMaster(ncDeviceId)) {
+            log.warn("Not master for {} Use {} to execute command",
+                     ncDeviceId,
+                     mastershipService.getMasterFor(ncDeviceId));
+            return reply;
+        }
+
+        try {
+            StringBuilder request = new StringBuilder();
+            request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
+            request.append(ANGLE_RIGHT).append(NEW_LINE);
+            request.append(buildStartTag(VOLT_ALERTS));
+            request.append(buildEmptyTag(ALERT_FILTER));
+            request.append(buildEndTag(VOLT_ALERTS));
+            request.append(VOLT_NE_CLOSE);
+
+            reply = controller.
+                    getDevicesMap().get(ncDeviceId).getSession().
+                    get(request.toString(), REPORT_ALL);
+        } catch (IOException e) {
+            log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
+        }
+        return reply;
+    }
+
+    @Override
+    public void setAlertFilter(String severity) {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        MastershipService mastershipService = handler.get(MastershipService.class);
+        DeviceId ncDeviceId = handler.data().deviceId();
+        checkNotNull(controller, "Netconf controller is null");
+
+        if (!mastershipService.isLocalMaster(ncDeviceId)) {
+            log.warn("Not master for {} Use {} to execute command",
+                     ncDeviceId,
+                     mastershipService.getMasterFor(ncDeviceId));
+            return;
+        }
+
+        if (!severityLevels.contains(severity)) {
+            log.error("Invalid severity level: " + severity);
+            return;
+        }
+
+        try {
+            StringBuilder request = new StringBuilder();
+            request.append(VOLT_NE_OPEN).append(VOLT_NE_NAMESPACE);
+            request.append(ANGLE_RIGHT).append(NEW_LINE);
+            request.append(buildStartTag(VOLT_ALERTS));
+            request.append(buildStartTag(ALERT_FILTER, false));
+            request.append(severity);
+            request.append(buildEndTag(ALERT_FILTER));
+            request.append(buildEndTag(VOLT_ALERTS));
+            request.append(VOLT_NE_CLOSE);
+
+            controller.getDevicesMap().get(ncDeviceId).getSession().
+                    editConfig(RUNNING, null, request.toString());
+        } catch (IOException e) {
+            log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
+        }
+    }
+
+    @Override
+    public void subscribe(String mode) {
+        DriverHandler handler = handler();
+        NetconfController controller = handler.get(NetconfController.class);
+        MastershipService mastershipService = handler.get(MastershipService.class);
+        DeviceId ncDeviceId = handler.data().deviceId();
+        checkNotNull(controller, "Netconf controller is null");
+
+        if (!mastershipService.isLocalMaster(ncDeviceId)) {
+            log.warn("Not master for {} Use {} to execute command",
+                     ncDeviceId,
+                     mastershipService.getMasterFor(ncDeviceId));
+            return;
+        }
+
+        if (mode != null) {
+            if (!DISABLE.equals(mode)) {
+                log.error("Invalid mode: " + mode);
+                return;
+            }
+        }
+
+        try {
+            if (mode != null) {
+                controller.getDevicesMap().get(ncDeviceId).getSession().
+                        endSubscription();
+            } else {
+                StringBuilder request = new StringBuilder();
+                request.append(ANGLE_LEFT).append(NOTIFY_ALERT).append(SPACE);
+                request.append(VOLT_NE_NAMESPACE).append(SLASH).append(ANGLE_RIGHT);
+
+                controller.getDevicesMap().get(ncDeviceId).getSession().
+                        startSubscription(request.toString());
+            }
+        } catch (IOException e) {
+            log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
+        }
+    }
+
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltAlertConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltAlertConfig.java
new file mode 100644
index 0000000..9385361
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltAlertConfig.java
@@ -0,0 +1,48 @@
+/*
+ * 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.fujitsu.behaviour;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.driver.HandlerBehaviour;
+
+/**
+ * Device behaviour to obtain and set alert filter in vOLT.
+ * Device behaviour to subscribe to receive notifications from vOLT.
+ */
+@Beta
+public interface VoltAlertConfig extends HandlerBehaviour {
+
+    /**
+     * Get alert filter severity level.
+     *
+     * @return response string
+     */
+    String getAlertFilter();
+
+    /**
+     * Set alert filter severity level.
+     *
+     * @param severity input data in string
+     */
+    void setAlertFilter(String severity);
+
+    /**
+     * Subscribe to receive notifications or unsubscribe.
+     *
+     * @param mode disable subscription
+     */
+    void subscribe(String mode);
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetAlertFilterCommand.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetAlertFilterCommand.java
new file mode 100644
index 0000000..334c750
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetAlertFilterCommand.java
@@ -0,0 +1,52 @@
+/*
+ * 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.fujitsu.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+
+/**
+ * Gets alert filter severity level in vOLT.
+ */
+@Command(scope = "onos", name = "volt-notification-alertfilter",
+        description = "Gets alert filter severity level in vOLT")
+public class VoltGetAlertFilterCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = true, multiValued = false)
+    String uri = null;
+
+    private DeviceId deviceId;
+
+    @Override
+    protected void execute() {
+        DriverService service = get(DriverService.class);
+        deviceId = DeviceId.deviceId(uri);
+        DriverHandler h = service.createHandler(deviceId);
+        VoltAlertConfig voltNe = h.behaviour(VoltAlertConfig.class);
+        String reply = voltNe.getAlertFilter();
+        if (reply != null) {
+            print("%s", reply);
+        } else {
+            print("No reply from %s", deviceId.toString());
+        }
+    }
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetPonLinksCommand.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetPonLinksCommand.java
index 9129d94..1cb73ca 100644
--- a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetPonLinksCommand.java
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltGetPonLinksCommand.java
@@ -50,7 +50,7 @@
         if (reply != null) {
             print("%s", reply);
         } else {
-            print("No replay from %s", deviceId.toString());
+            print("No reply from %s", deviceId.toString());
         }
     }
 
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSetAlertFilterCommand.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSetAlertFilterCommand.java
new file mode 100644
index 0000000..2719f31
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSetAlertFilterCommand.java
@@ -0,0 +1,51 @@
+/*
+ * 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.fujitsu.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+
+/**
+ * Sets alert filter severity level in vOLT.
+ */
+@Command(scope = "onos", name = "volt-notification-setalertfilter",
+        description = "Sets alert filter severity level in vOLT")
+public class VoltSetAlertFilterCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = true, multiValued = false)
+    String uri = null;
+
+    @Argument(index = 1, name = "target", description = "Severity level",
+            required = true, multiValued = false)
+    String severity = null;
+
+    private DeviceId deviceId;
+
+    @Override
+    protected void execute() {
+        DriverService service = get(DriverService.class);
+        deviceId = DeviceId.deviceId(uri);
+        DriverHandler h = service.createHandler(deviceId);
+        VoltAlertConfig volt = h.behaviour(VoltAlertConfig.class);
+        volt.setAlertFilter(severity);
+    }
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSubscribeCommand.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSubscribeCommand.java
new file mode 100644
index 0000000..3ed1ada
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltSubscribeCommand.java
@@ -0,0 +1,51 @@
+/*
+ * 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.fujitsu.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+
+/**
+ * Subscribes to receive notifications of vOLT or unsubscribes.
+ */
+@Command(scope = "onos", name = "volt-notification-subscribe",
+        description = "Subscribes to receive notifications of vOLT")
+public class VoltSubscribeCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = true, multiValued = false)
+    String uri = null;
+
+    @Argument(index = 1, name = "mode", description = "Disable subscription",
+            required = false, multiValued = false)
+    String mode = null;
+
+    private DeviceId deviceId;
+
+    @Override
+    protected void execute() {
+        DriverService service = get(DriverService.class);
+        deviceId = DeviceId.deviceId(uri);
+        DriverHandler h = service.createHandler(deviceId);
+        VoltAlertConfig volt = h.behaviour(VoltAlertConfig.class);
+        volt.subscribe(mode);
+    }
+}
diff --git a/drivers/fujitsu/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/drivers/fujitsu/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 1f72cc6..a361f7d 100644
--- a/drivers/fujitsu/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/drivers/fujitsu/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -59,8 +59,25 @@
                 <ref component-id="deviceIdCompleter"/>
             </completers>
         </command>
+        <command>
+            <action class="org.onosproject.drivers.fujitsu.cli.VoltGetAlertFilterCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+            </completers>
+        </command>
+        <command>
+            <action class="org.onosproject.drivers.fujitsu.cli.VoltSetAlertFilterCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+            </completers>
+        </command>
+        <command>
+            <action class="org.onosproject.drivers.fujitsu.cli.VoltSubscribeCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+            </completers>
+        </command>
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
-
 </blueprint>
diff --git a/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
index 46cb697..3547394 100644
--- a/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
+++ b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
@@ -30,5 +30,7 @@
                    impl="org.onosproject.drivers.fujitsu.FujitsuVoltOnuConfig"/>
         <behaviour api="org.onosproject.drivers.fujitsu.behaviour.VoltOnuOperConfig"
                    impl="org.onosproject.drivers.fujitsu.FujitsuVoltOnuOperConfig"/>
+        <behaviour api="org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig"
+                   impl="org.onosproject.drivers.fujitsu.FujitsuVoltAlertConfig"/>
     </driver>
-</drivers>
\ No newline at end of file
+</drivers>