annotate-link command

- Utility to add annotations to existing links.
  for ONOS-5810, etc.

Change-Id: I0a10e5cffe376fe935552dc9c1812741522746ee
diff --git a/cli/src/main/java/org/onosproject/cli/net/AnnotateLinkCommand.java b/cli/src/main/java/org/onosproject/cli/net/AnnotateLinkCommand.java
new file mode 100644
index 0000000..94f3076
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/AnnotateLinkCommand.java
@@ -0,0 +1,113 @@
+/*
+ * 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 com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
+
+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.DefaultAnnotations;
+import org.onosproject.net.Link;
+import org.onosproject.net.link.DefaultLinkDescription;
+import org.onosproject.net.link.LinkDescription;
+import org.onosproject.net.link.LinkProvider;
+import org.onosproject.net.link.LinkProviderRegistry;
+import org.onosproject.net.link.LinkProviderService;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.net.provider.ProviderId;
+
+/**
+ * Annotates network link model.
+ */
+@Command(scope = "onos", name = "annotate-link",
+         description = "Annotates network model entities")
+public class AnnotateLinkCommand extends AbstractShellCommand {
+
+    static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
+
+    @Option(name = "--both",
+            description = "Add to both direction")
+    private boolean both = false;
+
+    @Argument(index = 0, name = "srcConnectPoint", description = "source Connect Point",
+            required = true, multiValued = false)
+    private String srcCp = null;
+
+    @Argument(index = 1, name = "dstConnectPoint", description = "destination Connect Point",
+            required = true, multiValued = false)
+    private String dstCp = null;
+
+
+
+    @Argument(index = 2, name = "key", description = "Annotation key",
+            required = true, multiValued = false)
+    private String key = null;
+
+    @Argument(index = 3, name = "value",
+            description = "Annotation value (null to remove)",
+            required = false, multiValued = false)
+    private String value = null;
+
+
+    @Override
+    protected void execute() {
+        LinkService service = get(LinkService.class);
+        ConnectPoint src = deviceConnectPoint(srcCp);
+        ConnectPoint dst = deviceConnectPoint(dstCp);
+
+        LinkProviderRegistry registry = get(LinkProviderRegistry.class);
+        CliLinkProvider provider = new CliLinkProvider();
+        LinkProviderService providerService = registry.register(provider);
+        try {
+            providerService.linkDetected(description(service.getLink(src, dst),
+                                                     key, value));
+            if (both) {
+                providerService.linkDetected(description(service.getLink(dst, src),
+                                                         key, value));
+            }
+        } finally {
+            registry.unregister(provider);
+        }
+    }
+
+
+    private LinkDescription description(Link link, String key, String value) {
+        checkNotNull(key, "Key cannot be null");
+        DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
+        if (value != null) {
+            builder.set(key, value);
+        } else {
+            builder.remove(key);
+        }
+        return new DefaultLinkDescription(link.src(),
+                                          link.dst(),
+                                          link.type(),
+                                          link.isExpected(),
+                                          builder.build());
+    }
+
+    private static final class CliLinkProvider implements LinkProvider {
+        @Override
+        public ProviderId id() {
+            return PID;
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/completer/AnnotationKeysCompleter.java b/cli/src/main/java/org/onosproject/cli/net/completer/AnnotationKeysCompleter.java
new file mode 100644
index 0000000..c061bb3
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/completer/AnnotationKeysCompleter.java
@@ -0,0 +1,51 @@
+/*
+ * 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.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.net.AnnotationKeys;
+
+/**
+ * Completer for annotation keys declared in {@link AnnotationKeys}.
+ */
+public class AnnotationKeysCompleter extends AbstractChoicesCompleter {
+
+
+    @Override
+    protected List<String> choices() {
+
+        return Arrays.asList(AnnotationKeys.class.getFields())
+            .stream()
+            .filter(f -> f.getType() == String.class)
+            .filter(f -> Modifier.isStatic(f.getModifiers()))
+            .map(f -> {
+                try {
+                    return (String) f.get(null);
+                } catch (IllegalArgumentException | IllegalAccessException e) {
+                    return null;
+                }
+            })
+            .filter(Objects::nonNull)
+            .collect(Collectors.toList());
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/completer/PeerConnectPointCompleter.java b/cli/src/main/java/org/onosproject/cli/net/completer/PeerConnectPointCompleter.java
new file mode 100644
index 0000000..1095f6c
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/completer/PeerConnectPointCompleter.java
@@ -0,0 +1,78 @@
+/*
+ * 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 static org.onlab.osgi.DefaultServiceDirectory.getService;
+import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.link.LinkService;
+
+/**
+ * Completer, which proposes remote end of existing Link in the system.
+ * <p>
+ * This completer will look for (device id)/(port number) in the
+ * existing argument and propose list of remote ports.
+ */
+public class PeerConnectPointCompleter extends AbstractChoicesCompleter {
+
+    @Override
+    protected List<String> choices() {
+        ArgumentList args = getArgumentList();
+
+        DeviceService deviceService = getService(DeviceService.class);
+        LinkService linkService = getService(LinkService.class);
+
+        Optional<ConnectPoint> port = Arrays.asList(args.getArguments()).stream()
+            .filter(s -> s.contains(":") && s.contains("/"))
+            .map(s -> {
+                try {
+                    return deviceConnectPoint(s);
+                } catch (IllegalArgumentException e) {
+                    // silently ill-formed String
+                    return null;
+                }
+            })
+            .filter(Objects::nonNull)
+            .filter(cp -> deviceService.getPort(cp) != null)
+            .findFirst();
+
+        if (!port.isPresent()) {
+            // no candidate
+            return Collections.emptyList();
+        }
+        final ConnectPoint cp = port.get();
+
+        return linkService.getLinks(cp).stream()
+                .flatMap(l -> Stream.of(l.src(), l.dst()))
+                .filter(peer -> !cp.equals(peer))
+                .distinct()
+                .map(ConnectPoint::toString)
+                .collect(Collectors.toList());
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/completer/package-info.java b/cli/src/main/java/org/onosproject/cli/net/completer/package-info.java
new file mode 100644
index 0000000..c622d27
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/completer/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.
+ */
+/**
+ * Completers for karaf CLI.
+ */
+package org.onosproject.cli.net.completer;
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 f3c660f..da7c4af 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -209,11 +209,22 @@
                 <ref component-id="roleCompleter"/>
             </completers>
         </command>
+
         <command>
             <action class="org.onosproject.cli.net.AnnotateDeviceCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
+                <ref component-id="annotationKeysCompleter"/>
                 <null/>
+            </completers>
+        </command>
+
+        <command>
+            <action class="org.onosproject.cli.net.AnnotateLinkCommand"/>
+            <completers>
+                <ref component-id="connectPointCompleter"/>
+                <ref component-id="peerConnectPointCompleter"/>
+                <ref component-id="annotationKeysCompleter"/>
                 <null/>
             </completers>
         </command>
@@ -759,4 +770,8 @@
 
     <bean id="regionIdCompleter" class="org.onosproject.cli.net.RegionIdCompleter"/>
     <bean id="regionTypeCompleter" class="org.onosproject.cli.net.RegionTypeCompleter"/>
+
+    <bean id="annotationKeysCompleter" class="org.onosproject.cli.net.completer.AnnotationKeysCompleter"/>
+
+    <bean id="peerConnectPointCompleter" class="org.onosproject.cli.net.completer.PeerConnectPointCompleter"/>
 </blueprint>