[ONOS-4939] NETCONF function for FUJITSU OLT #7

- Add "ONU firmware upgrade" command for FUJITSU OLT
   volt-ondemandfwdl <netconf:target> <Firmware Image name:ONU-ID list[:reboot-mode]>
- Update fujitsu-drivers.xml and shell-config.xml in FUJITSU directory

Change-Id: Id90c4a59046e0a0f695ddcd40ea3ab905d1fa253
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltFwdlConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltFwdlConfig.java
new file mode 100644
index 0000000..51cc1ee
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/FujitsuVoltFwdlConfig.java
@@ -0,0 +1,129 @@
+/*
+ * 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.drivers.fujitsu.behaviour.VoltFwdlConfig;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.netconf.NetconfController;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.drivers.fujitsu.FujitsuVoltXmlUtility.*;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation to upgrade firmware in ONUs manually
+ * through the Netconf protocol.
+ */
+public class FujitsuVoltFwdlConfig extends AbstractHandlerBehaviour
+        implements VoltFwdlConfig {
+
+    private final Logger log = getLogger(FujitsuVoltFwdlConfig.class);
+    private static final String ONDEMAND_FIRMWARE_UPGRADE = "ondemand-firmware-upgrade";
+    private static final String PARTICIPANT_LIST = "participant-list";
+    private static final String MEMBER = "member";
+    private static final String IMAGE_NAME = "image-name";
+    private static final String REBOOT_MODE = "reboot-mode";
+    private int pon;
+    private int onu;
+
+
+    @Override
+    public String upgradeFirmwareOndemand(String target) {
+        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;
+        int count;
+
+        if (!mastershipService.isLocalMaster(ncDeviceId)) {
+            log.warn("Not master for {} Use {} to execute command",
+                     ncDeviceId,
+                     mastershipService.getMasterFor(ncDeviceId));
+            return reply;
+        }
+
+        String[] data = target.split(":");
+        if ((data.length < 2) || (data.length > 3)) {
+            log.error("Invalid number of arguments");
+            return reply;
+        }
+
+        String[] onuList = data[1].split(",");
+        if (onuList.length == 0) {
+            log.error("No ONU listed");
+            return reply;
+        }
+
+        try {
+            StringBuilder request = new StringBuilder();
+            request.append(ANGLE_LEFT).append(ONDEMAND_FIRMWARE_UPGRADE).append(SPACE);
+            request.append(VOLT_NE_NAMESPACE).append(ANGLE_RIGHT).append(NEW_LINE);
+            request.append(buildStartTag(PARTICIPANT_LIST));
+
+            for (count = 0; count < onuList.length; count++) {
+                String[] onuId = onuList[count].split("-");
+                if (onuId.length != 2) {
+                    log.error("Invalid ONU identifier");
+                    return reply;
+                }
+
+                try {
+                    pon = Integer.parseInt(onuId[0]);
+                    onu = Integer.parseInt(onuId[1]);
+                } catch (NumberFormatException e) {
+                    log.error("Non-number input");
+                    return reply;
+                }
+
+                request.append(buildStartTag(MEMBER));
+                request.append(buildStartTag(PONLINK_ID));
+                request.append(onuId[0]);
+                request.append(buildEndTag(PONLINK_ID));
+                request.append(buildStartTag(ONU_ID));
+                request.append(onuId[1]);
+                request.append(buildEndTag(ONU_ID));
+                request.append(buildEndTag(MEMBER));
+            }
+            request.append(buildEndTag(PARTICIPANT_LIST));
+            request.append(buildStartTag(IMAGE_NAME));
+            request.append(data[0]);
+            request.append(buildEndTag(IMAGE_NAME));
+            if (data.length == 3) {
+                request.append(buildStartTag(REBOOT_MODE));
+                request.append(data[2]);
+                request.append(buildEndTag(REBOOT_MODE));
+            }
+            request.append(buildEndTag(ONDEMAND_FIRMWARE_UPGRADE));
+
+            reply = controller.
+                    getDevicesMap().get(ncDeviceId).getSession().
+                    doWrappedRpc(request.toString());
+        } catch (IOException e) {
+            log.error("Cannot communicate to device {} exception ", ncDeviceId, e);
+        }
+        return reply;
+    }
+
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltFwdlConfig.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltFwdlConfig.java
new file mode 100644
index 0000000..221c5b1
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/behaviour/VoltFwdlConfig.java
@@ -0,0 +1,34 @@
+/*
+ * 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 upgrade ONUs to new firmware.
+ */
+@Beta
+public interface VoltFwdlConfig extends HandlerBehaviour {
+
+    /**
+     * Upgrade firmware manually in an ONU in the device.
+     *
+     * @param target input data in string
+     */
+    String upgradeFirmwareOndemand(String target);
+
+}
diff --git a/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltOndemandFwdlCommand.java b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltOndemandFwdlCommand.java
new file mode 100644
index 0000000..1996fef
--- /dev/null
+++ b/drivers/fujitsu/src/main/java/org/onosproject/drivers/fujitsu/cli/VoltOndemandFwdlCommand.java
@@ -0,0 +1,57 @@
+/*
+ * 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.drivers.fujitsu.behaviour.VoltFwdlConfig;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+
+/**
+ * Requests manual firmware upgrade on a list of ONUs in vOLT.
+ */
+@Command(scope = "onos", name = "volt-ondemandfwdl",
+        description = "Requests manual firmware upgrade on a list of ONUs in vOLT")
+public class VoltOndemandFwdlCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = true, multiValued = false)
+    String uri = null;
+
+    @Argument(index = 1, name = "target",
+            description = "image name:PON link ID-ONU ID[,PON link ID-ONU ID,..:reboot-mode]",
+            required = true, multiValued = false)
+    String target = null;
+
+    private DeviceId deviceId;
+
+    @Override
+    protected void execute() {
+        DriverService service = get(DriverService.class);
+        deviceId = DeviceId.deviceId(uri);
+        DriverHandler h = service.createHandler(deviceId);
+        VoltFwdlConfig volt = h.behaviour(VoltFwdlConfig.class);
+        String reply = volt.upgradeFirmwareOndemand(target);
+        if (reply != null) {
+            print("%s", reply);
+        } else {
+            print("ONU firmware-upgrade failure %s", deviceId.toString());
+        }
+    }
+}
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 a361f7d..e8103cc 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
@@ -77,6 +77,12 @@
                 <ref component-id="deviceIdCompleter"/>
             </completers>
         </command>
+        <command>
+            <action class="org.onosproject.drivers.fujitsu.cli.VoltOndemandFwdlCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+            </completers>
+        </command>
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
diff --git a/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
index 3547394..a1374b9 100644
--- a/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
+++ b/drivers/fujitsu/src/main/resources/fujitsu-drivers.xml
@@ -32,5 +32,7 @@
                    impl="org.onosproject.drivers.fujitsu.FujitsuVoltOnuOperConfig"/>
         <behaviour api="org.onosproject.drivers.fujitsu.behaviour.VoltAlertConfig"
                    impl="org.onosproject.drivers.fujitsu.FujitsuVoltAlertConfig"/>
+        <behaviour api="org.onosproject.drivers.fujitsu.behaviour.VoltFwdlConfig"
+                   impl="org.onosproject.drivers.fujitsu.FujitsuVoltFwdlConfig"/>
     </driver>
 </drivers>