ONOS-6022 CLI to disable LLDP discovery

Change-Id: Ic080e8eefcc96a4dc143166346fe6b0b19316b11
diff --git a/providers/lldp/BUCK b/providers/lldp/BUCK
index c5d0d3c..7e897a6 100644
--- a/providers/lldp/BUCK
+++ b/providers/lldp/BUCK
@@ -1,6 +1,8 @@
 COMPILE_DEPS = [
     '//lib:CORE_DEPS',
     '//providers/lldpcommon:onos-providers-lldpcommon',
+    '//lib:org.apache.karaf.shell.console',
+    '//cli:onos-cli',
 ]
 
 TEST_DEPS = [
diff --git a/providers/lldp/pom.xml b/providers/lldp/pom.xml
index 30fd242..dbaca7d 100644
--- a/providers/lldp/pom.xml
+++ b/providers/lldp/pom.xml
@@ -47,6 +47,17 @@
         </dependency>
 
         <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-cli</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
             <groupId>org.onosproject</groupId>
             <artifactId>onos-lldp-provider-common</artifactId>
             <version>1.10.0-SNAPSHOT</version>
diff --git a/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/ConfigLinkDiscoveryCommand.java b/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/ConfigLinkDiscoveryCommand.java
new file mode 100644
index 0000000..9dee8d0
--- /dev/null
+++ b/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/ConfigLinkDiscoveryCommand.java
@@ -0,0 +1,109 @@
+/*
+ * 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.provider.lldp.cli;
+
+import java.util.Optional;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cli.net.DeviceIdCompleter;
+import org.onosproject.cli.net.PortNumberCompleter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.provider.lldp.impl.LinkDiscoveryFromDevice;
+import org.onosproject.provider.lldp.impl.LinkDiscoveryFromPort;
+
+/**
+ *
+ */
+@Command(scope = "onos", name = "config-link-discovery",
+         description = "Adds configuration to disable LLDP link discovery")
+public class ConfigLinkDiscoveryCommand extends AbstractShellCommand {
+
+    // OSGi workaround to introduce package dependency
+    DeviceIdCompleter deviceIdCompleter;
+    @Argument(index = 0, name = "device",
+            description = "DeviceID",
+            required = true)
+    String device = null;
+
+
+    // OSGi workaround to introduce package dependency
+    PortNumberCompleter portNumberCompleter;
+    @Argument(index = 1, name = "port",
+            description = "Port number",
+            required = false)
+    String port = null;
+
+    @Option(name = "--remove", aliases = "-r",
+            description = "remove configuration",
+            required = false)
+    boolean remove = false;
+
+    @Option(name = "--enable",
+            description = "add configuration to enable LinkDiscovery",
+            required = false)
+    boolean enable = false;
+
+
+    @Override
+    protected void execute() {
+        DeviceService deviceService = get(DeviceService.class);
+        NetworkConfigService netcfgService = get(NetworkConfigService.class);
+
+        DeviceId did = DeviceId.deviceId(device);
+
+        ConnectPoint cp = Optional.ofNullable(port)
+                .map(PortNumber::fromString)
+                .map(pn -> new ConnectPoint(did, pn))
+                .orElse(null);
+
+        if (cp == null) {
+            // device config
+            if (!remove) {
+                if (deviceService.getDevice(did) == null) {
+                    print("[WARN] configuring about unknown device %s", did);
+                }
+                LinkDiscoveryFromDevice cfg;
+                cfg = netcfgService.addConfig(did, LinkDiscoveryFromDevice.class);
+                cfg.enabled(enable);
+                cfg.apply();
+            } else {
+                netcfgService.removeConfig(did, LinkDiscoveryFromDevice.class);
+            }
+        } else {
+            // port config
+            if (!remove) {
+                if (deviceService.getPort(cp) == null) {
+                    print("[WARN] configuring about unknown port %s", cp);
+                }
+
+                LinkDiscoveryFromPort cfg;
+                cfg = netcfgService.addConfig(cp, LinkDiscoveryFromPort.class);
+                cfg.enabled(enable);
+                cfg.apply();
+            } else {
+                netcfgService.removeConfig(cp, LinkDiscoveryFromPort.class);
+            }
+        }
+    }
+
+}
diff --git a/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/package-info.java b/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/package-info.java
new file mode 100644
index 0000000..037a992
--- /dev/null
+++ b/providers/lldp/src/main/java/org/onosproject/provider/lldp/cli/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * 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 LLDP Link Provider.
+ */
+package org.onosproject.provider.lldp.cli;
diff --git a/providers/lldp/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/providers/lldp/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..abb10ce
--- /dev/null
+++ b/providers/lldp/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,34 @@
+<!--
+  ~ 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.
+  -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+
+        <command>
+            <action class="org.onosproject.provider.lldp.cli.ConfigLinkDiscoveryCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+                <ref component-id="portNumberCompleter"/>
+                <null/>
+            </completers>
+        </command>
+
+    </command-bundle>
+
+    <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
+    <bean id="portNumberCompleter" class="org.onosproject.cli.net.PortNumberCompleter"/>
+
+</blueprint>