CLI to configure extra link attributes

Change-Id: I841857dc1f281a69565a68d38e99621ed15b62c9
diff --git a/cli/src/main/java/org/onosproject/cli/net/ConfigureLinkCommand.java b/cli/src/main/java/org/onosproject/cli/net/ConfigureLinkCommand.java
new file mode 100644
index 0000000..be310e8
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/ConfigureLinkCommand.java
@@ -0,0 +1,107 @@
+/*
+ * 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.cli.net;
+
+import static org.onosproject.net.LinkKey.linkKey;
+
+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.net.ConnectPoint;
+import org.onosproject.net.Link;
+import org.onosproject.net.LinkKey;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.net.config.basics.BasicLinkConfig;
+import org.onosproject.net.device.DeviceService;
+
+/**
+ * Add Link configuration.
+ */
+@Command(scope = "onos", name = "config-link",
+         description = "Configure link.")
+public class ConfigureLinkCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "src", description = "src port",
+            required = true, multiValued = false)
+    String src = null;
+
+    @Argument(index = 1, name = "dst", description = "dst port",
+            required = true, multiValued = false)
+    String dst = null;
+
+    @Option(name = "--type",
+            description = "specify link type",
+            valueToShowInHelp = "DIRECT")
+    String type = Link.Type.DIRECT.name();
+
+    // TODO add metric, latency, durable
+
+    @Option(name = "--bandwidth",
+            description = "bandwidth in Mbps (integer)")
+    String bandwidth = null;
+
+    @Option(name = "--disallow",
+            description = "disallow link")
+    boolean disallow = false;
+
+    @Option(name = "--remove-config",
+            description = "remove link configuration")
+    boolean remove = false;
+
+
+    @Override
+    protected void execute() {
+        DeviceService deviceService = get(DeviceService.class);
+        NetworkConfigService netCfgService = get(NetworkConfigService.class);
+
+        ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(src);
+        if (deviceService.getPort(srcCp) == null) {
+            print("[ERROR] %s does not exist", srcCp);
+            return;
+        }
+
+        ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(dst);
+        if (deviceService.getPort(dstCp) == null) {
+            print("[ERROR] %s does not exist", dstCp);
+            return;
+        }
+
+        LinkKey link = linkKey(srcCp, dstCp);
+        if (remove) {
+            netCfgService.removeConfig(link, BasicLinkConfig.class);
+            return;
+        }
+
+        Long bw = Optional.ofNullable(bandwidth)
+                        .map(Long::valueOf)
+                        .orElse(null);
+
+        Link.Type linkType = Link.Type.valueOf(type);
+
+        BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class);
+        cfg.isAllowed(!disallow);
+        cfg.type(linkType);
+        if (bw != null) {
+            cfg.bandwidth(bw);
+        }
+
+        cfg.apply();
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/completer/LinkTypeCompleter.java b/cli/src/main/java/org/onosproject/cli/net/completer/LinkTypeCompleter.java
new file mode 100644
index 0000000..1c41bb6
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/completer/LinkTypeCompleter.java
@@ -0,0 +1,39 @@
+/*
+ * 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.cli.net.completer;
+
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.net.Link;
+
+/**
+ * Completer for {@link Link.Type}.
+ */
+public class LinkTypeCompleter extends AbstractChoicesCompleter {
+
+    @Override
+    protected List<String> choices() {
+        return EnumSet.allOf(Link.Type.class).stream()
+                .sorted(Comparator.comparingInt(Enum::ordinal))
+                .map(Enum::name)
+                .collect(Collectors.toList());
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 537c110..c0329c1 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -777,6 +777,19 @@
             </optional-completers>
         </command>
 
+        <command>
+            <action class="org.onosproject.cli.net.ConfigureLinkCommand"/>
+            <completers>
+                <ref component-id="connectPointCompleter"/>
+                <ref component-id="peerConnectPointCompleter"/>
+                <null/>
+            </completers>
+            <optional-completers>
+                <entry key="--type" value-ref="linkTypeCompleter"/>
+            </optional-completers>
+        </command>
+
+
     </command-bundle>
 
     <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/>
@@ -825,4 +838,7 @@
 
     <bean id="peerConnectPointCompleter" class="org.onosproject.cli.net.completer.PeerConnectPointCompleter"/>
     <bean id="interfaceNameCompleter" class="org.onosproject.cli.net.completer.InterfaceNameCompleter"/>
+
+    <bean id="linkTypeCompleter" class="org.onosproject.cli.net.completer.LinkTypeCompleter"/>
+
 </blueprint>
diff --git a/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java b/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
index 64bf8b3..cfed826 100644
--- a/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
+++ b/core/net/src/main/java/org/onosproject/net/link/impl/LinkManager.java
@@ -52,6 +52,7 @@
 import org.onosproject.net.provider.AbstractProviderService;
 import org.slf4j.Logger;
 
+import java.util.Optional;
 import java.util.Set;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -357,9 +358,12 @@
                 rldesc = BasicLinkOperator.combine(cfg,
                             BasicLinkOperator.descriptionOf(lk.dst(), lk.src(), link));
             }
-            // XXX think of sane way to fetch the LinkProvider
-            store.createOrUpdateLink(ProviderId.NONE, fldesc);
-            store.createOrUpdateLink(ProviderId.NONE, rldesc);
+
+            ProviderId pid = Optional.ofNullable(link)
+                                .map(Link::providerId)
+                                .orElse(ProviderId.NONE);
+            store.createOrUpdateLink(pid, fldesc);
+            store.createOrUpdateLink(pid, rldesc);
         }
 
     }