ONOS-5503 Added CLIs for administering OFAgent

Change-Id: I58256316e2054952da9dce04bf927901761807e5
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentAddControllerCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentAddControllerCommand.java
new file mode 100644
index 0000000..354197b
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentAddControllerCommand.java
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgent;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+import org.onosproject.ofagent.api.OFAgentService;
+import org.onosproject.ofagent.impl.DefaultOFAgent;
+import org.onosproject.ofagent.impl.DefaultOFController;
+
+/**
+ * Adds a controller to the OFAgent.
+ */
+@Command(scope = "onos", name = "ofagent-controller-add",
+        description = "Add a controller to the ofagent")
+public class OFAgentAddControllerCommand extends AbstractShellCommand {
+
+    private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Argument(index = 1, name = "controller",
+            description = "External controller with IP:PORT format",
+            required = true, multiValued = false)
+    private String strCtrl;
+
+    @Override
+    protected void execute() {
+        if (!isValidController(strCtrl)) {
+            error("Invalid controller string %s, must be IP:PORT", strCtrl);
+            return;
+        }
+
+        OFAgentService service = get(OFAgentService.class);
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+
+        OFAgent existing = service.agent(NetworkId.networkId(networkId));
+        if (existing == null) {
+            error("OFAgent for network %s does not exist", networkId);
+            return;
+        }
+
+        String[] temp = strCtrl.split(":");
+        OFAgent updated = DefaultOFAgent.builder()
+                .from(existing)
+                .addController(DefaultOFController.of(
+                        IpAddress.valueOf(temp[0]),
+                        TpPort.tpPort(Integer.valueOf(temp[1]))))
+                .build();
+        adminService.updateAgent(updated);
+    }
+
+    private boolean isValidController(String ctrl) {
+        if (!ctrl.matches(PATTERN_IP_PORT)) {
+            return false;
+        }
+
+        String[] temp = ctrl.split(":");
+        try {
+            IpAddress.valueOf(temp[0]);
+            TpPort.tpPort(Integer.valueOf(temp[1]));
+            return true;
+        } catch (IllegalArgumentException e) {
+            return false;
+        }
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentCreateCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentCreateCommand.java
new file mode 100644
index 0000000..239c5ef
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentCreateCommand.java
@@ -0,0 +1,87 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import com.google.common.collect.Sets;
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgent;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+import org.onosproject.ofagent.api.OFController;
+import org.onosproject.ofagent.impl.DefaultOFAgent;
+import org.onosproject.ofagent.impl.DefaultOFController;
+
+import java.util.Set;
+
+/**
+ * Creates a new OFAagent.
+ */
+@Command(scope = "onos", name = "ofagent-create", description = "Add a new ofagent")
+public class OFAgentCreateCommand extends AbstractShellCommand {
+
+    private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Argument(index = 1, name = "controllers",
+            description = "List of external controllers with IP:PORT format",
+            required = false, multiValued = true)
+    private String[] strCtrls = {};
+
+    @Override
+    protected void execute() {
+        Set<OFController> ctrls = Sets.newHashSet();
+        for (String strCtrl : strCtrls) {
+            if (!isValidController(strCtrl)) {
+                print("Invalid controller %s, ignores it.", strCtrl);
+                continue;
+            }
+            String[] temp = strCtrl.split(":");
+            ctrls.add(DefaultOFController.of(IpAddress.valueOf(temp[0]),
+                    TpPort.tpPort(Integer.valueOf(temp[1]))));
+        }
+
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+        OFAgent ofAgent = DefaultOFAgent.builder()
+                .networkId(NetworkId.networkId(networkId))
+                .controllers(ctrls)
+                .state(OFAgent.State.STOPPED)
+                .build();
+        adminService.createAgent(ofAgent);
+        print("Successfully created OFAgent for network %s", networkId);
+    }
+
+    private boolean isValidController(String ctrl) {
+        if (!ctrl.matches(PATTERN_IP_PORT)) {
+            return false;
+        }
+
+        String[] temp = ctrl.split(":");
+        try {
+            IpAddress.valueOf(temp[0]);
+            TpPort.tpPort(Integer.valueOf(temp[1]));
+            return true;
+        } catch (IllegalArgumentException e) {
+            return false;
+        }
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentDeleteControllerCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentDeleteControllerCommand.java
new file mode 100644
index 0000000..e401798
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentDeleteControllerCommand.java
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgent;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+import org.onosproject.ofagent.api.OFAgentService;
+import org.onosproject.ofagent.impl.DefaultOFAgent;
+import org.onosproject.ofagent.impl.DefaultOFController;
+
+/**
+ * Removes the controller from the OFAgent.
+ */
+@Command(scope = "onos", name = "ofagent-controller-delete",
+        description = "Deletes a controller from the ofagent")
+public class OFAgentDeleteControllerCommand extends AbstractShellCommand {
+
+    private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Argument(index = 1, name = "controller",
+            description = "External controller with IP:PORT format",
+            required = true, multiValued = false)
+    private String strCtrl;
+
+    @Override
+    protected void execute() {
+        if (!isValidController(strCtrl)) {
+            error("Invalid controller string %s, must be IP:PORT", strCtrl);
+            return;
+        }
+
+        OFAgentService service = get(OFAgentService.class);
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+
+        OFAgent existing = service.agent(NetworkId.networkId(networkId));
+        if (existing == null) {
+            error("OFAgent for network %s does not exist", networkId);
+            return;
+        }
+
+        String[] temp = strCtrl.split(":");
+        OFAgent updated = DefaultOFAgent.builder()
+                .from(existing)
+                .deleteController(DefaultOFController.of(
+                        IpAddress.valueOf(temp[0]),
+                        TpPort.tpPort(Integer.valueOf(temp[1]))))
+                .build();
+        adminService.updateAgent(updated);
+    }
+
+    private boolean isValidController(String ctrl) {
+        if (!ctrl.matches(PATTERN_IP_PORT)) {
+            return false;
+        }
+
+        String[] temp = ctrl.split(":");
+        try {
+            IpAddress.valueOf(temp[0]);
+            TpPort.tpPort(Integer.valueOf(temp[1]));
+            return true;
+        } catch (IllegalArgumentException e) {
+            return false;
+        }
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentListCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentListCommand.java
new file mode 100644
index 0000000..8cceadc
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentListCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.ofagent.api.OFAgentService;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Lists the existing OFAgents.
+ */
+@Command(scope = "onos", name = "ofagents", description = "Lists all ofagents")
+public class OFAgentListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%-10s%-10s%-8s";
+    private static final String CTRL = "%s:%s";
+
+    @Override
+    protected void execute() {
+        OFAgentService service = get(OFAgentService.class);
+        print(FORMAT, "Network", "Status", "Controllers");
+
+        service.agents().forEach(agent -> {
+            Set<String> ctrls = agent.controllers().stream()
+                    .map(ctrl -> String.format(CTRL, ctrl.ip(), ctrl.port()))
+                    .collect(Collectors.toSet());
+            print(FORMAT, agent.networkId(), agent.state().name(), ctrls);
+        });
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentRemoveCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentRemoveCommand.java
new file mode 100644
index 0000000..5e588f8
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentRemoveCommand.java
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgent;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+
+/**
+ * Removes the existing OFAgent.
+ */
+@Command(scope = "onos", name = "ofagent-remove", description = "Removes the ofagent")
+public class OFAgentRemoveCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Override
+    protected void execute() {
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+        OFAgent removed = adminService.removeAgent(NetworkId.networkId(networkId));
+        if (removed != null) {
+            print("Successfully removed OFAgent for network %s", networkId);
+        } else {
+            print("Failed to remove OFAgent for network %s", networkId);
+        }
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStartCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStartCommand.java
new file mode 100644
index 0000000..ba9228d
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStartCommand.java
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+
+/**
+ * Starts the OFAgent.
+ */
+@Command(scope = "onos", name = "ofagent-start", description = "Starts the ofagent")
+public class OFAgentStartCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Override
+    protected void execute() {
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+        adminService.startAgent(NetworkId.networkId(networkId));
+        print("Successfully started OFAgent for network %s", networkId);
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStopCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStopCommand.java
new file mode 100644
index 0000000..d780bed
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFAgentStopCommand.java
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFAgentAdminService;
+
+/**
+ * Stops the OFAgent.
+ */
+@Command(scope = "onos", name = "ofagent-stop", description = "Stops the ofagent")
+public class OFAgentStopCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = true, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Override
+    protected void execute() {
+        OFAgentAdminService adminService = get(OFAgentAdminService.class);
+        adminService.stopAgent(NetworkId.networkId(networkId));
+        print("Successfully stopped OFAgent for network %s", networkId);
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFSwitchListCommand.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFSwitchListCommand.java
new file mode 100644
index 0000000..c3e5714
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/OFSwitchListCommand.java
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+package org.onosproject.ofagent.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.ofagent.api.OFSwitch;
+import org.onosproject.ofagent.api.OFSwitchService;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Lists virtual OF switches.
+ */
+@Command(scope = "onos", name = "ofagent-switches", description = "Lists all OF switches")
+public class OFSwitchListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%-35s%-8s";
+
+    @Argument(index = 0, name = "network", description = "Virtual network ID",
+            required = false, multiValued = false)
+    private long networkId = NetworkId.NONE.id();
+
+    @Override
+    protected void execute() {
+        OFSwitchService service = get(OFSwitchService.class);
+
+        Set<OFSwitch> ofSwitches;
+        if (networkId != NetworkId.NONE.id()) {
+            ofSwitches = service.ofSwitches(NetworkId.networkId(networkId));
+        } else {
+            ofSwitches = service.ofSwitches();
+        }
+
+        print(FORMAT, "DPID", "Connected controllers");
+        ofSwitches.forEach(ofSwitch -> {
+            Set<String> channels = ofSwitch.controllerChannels().stream()
+                    .map(channel -> channel.remoteAddress().toString())
+                    .collect(Collectors.toSet());
+            print(FORMAT, ofSwitch.dpid(), channels);
+        });
+    }
+}
diff --git a/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/package-info.java b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/package-info.java
new file mode 100644
index 0000000..b9761ce
--- /dev/null
+++ b/apps/ofagent/src/main/java/org/onosproject/ofagent/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * CLIs for administering OFAgents.
+ */
+package org.onosproject.ofagent.cli;
\ No newline at end of file