[AETHER-1299] Implement SPINE pinning policy in SR.

Additionally introduces a new CLI command. Also this review addresses
comments coming from the previous patch [24393] and fixes some issue
seen in the previous patch.

Change-Id: I5362d95ebe1c237eb5bdb13ec34ab109d25f9f7a
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/DropPolicyAddCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyDropAddCommand.java
similarity index 92%
rename from impl/src/main/java/org/onosproject/segmentrouting/cli/DropPolicyAddCommand.java
rename to impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyDropAddCommand.java
index 9c3cb51..778e4c4 100644
--- a/impl/src/main/java/org/onosproject/segmentrouting/cli/DropPolicyAddCommand.java
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyDropAddCommand.java
@@ -27,9 +27,9 @@
  * Command to add a new drop policy.
  */
 @Service
-@Command(scope = "onos", name = "sr-drop-policy-add",
+@Command(scope = "onos", name = "sr-policy-drop-add",
         description = "Create a new drop policy")
-public class DropPolicyAddCommand extends AbstractShellCommand {
+public class PolicyDropAddCommand extends AbstractShellCommand {
 
     @Override
     protected void doExecute() {
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyListCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyListCommand.java
index 1c61912..c110358 100644
--- a/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyListCommand.java
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyListCommand.java
@@ -31,7 +31,7 @@
  * Command to show the list of policies.
  */
 @Service
-@Command(scope = "onos", name = "sr-policy-list",
+@Command(scope = "onos", name = "sr-policy",
         description = "Lists all policies")
 public class PolicyListCommand extends AbstractShellCommand {
 
@@ -40,11 +40,11 @@
     private static final String FORMAT_MAPPING_OPERATION =
             "    op=%s";
 
-    @Option(name = "-filt", aliases = "--filter",
+    @Option(name = "-t", aliases = "--type",
             description = "Filter based on policy type",
             valueToShowInHelp = "DROP",
             multiValued = true)
-    String[] filters = null;
+    String[] types = null;
 
     @Override
     protected void doExecute() {
@@ -55,8 +55,8 @@
 
     private Set<Policy.PolicyType> policyTypes() {
         Set<Policy.PolicyType> policyTypes = Sets.newHashSet();
-        if (filters != null) {
-            for (String filter : filters) {
+        if (types != null) {
+            for (String filter : types) {
                 policyTypes.add(Policy.PolicyType.valueOf(filter));
             }
         }
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyRedirectAddCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyRedirectAddCommand.java
new file mode 100644
index 0000000..e9f0279
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/PolicyRedirectAddCommand.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.segmentrouting.cli;
+
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.Option;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.glassfish.jersey.internal.guava.Sets;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.cli.net.DeviceIdCompleter;
+import org.onosproject.net.DeviceId;
+import org.onosproject.segmentrouting.policy.api.PolicyId;
+import org.onosproject.segmentrouting.policy.api.PolicyService;
+import org.onosproject.segmentrouting.policy.api.RedirectPolicy;
+
+import java.util.Set;
+
+/**
+ * Command to add a new redirect policy.
+ */
+@Service
+@Command(scope = "onos", name = "sr-policy-redirect-add",
+        description = "Create a new redirect policy")
+public class PolicyRedirectAddCommand extends AbstractShellCommand {
+
+    @Option(name = "-s", aliases = "--spine",
+            description = "Pin to spine",
+            valueToShowInHelp = "device:spine1",
+            multiValued = true)
+    @Completion(DeviceIdCompleter.class)
+    String[] spines = null;
+
+    @Override
+    protected void doExecute() {
+        Set<DeviceId> spinesToEnforce = spinesToEnforce();
+        if (spinesToEnforce.isEmpty()) {
+            print("Unable to submit redirect policy");
+            return;
+        }
+        PolicyService policyService = AbstractShellCommand.get(PolicyService.class);
+        PolicyId policyId = policyService.addOrUpdatePolicy(new RedirectPolicy(spinesToEnforce));
+        print("Policy %s has been submitted", policyId);
+    }
+
+    private Set<DeviceId> spinesToEnforce() {
+        Set<DeviceId> spinesToEnforce = Sets.newHashSet();
+        if (spines != null) {
+            for (String spine : spines) {
+                spinesToEnforce.add(DeviceId.deviceId(spine));
+            }
+        }
+        return spinesToEnforce;
+    }
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchAddCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchAddCommand.java
index d8b7a0a..ad9a432 100644
--- a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchAddCommand.java
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchAddCommand.java
@@ -17,6 +17,7 @@
 
 import org.apache.karaf.shell.api.action.Argument;
 import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 
 import org.onlab.packet.IPv4;
@@ -37,7 +38,7 @@
  * Command to add a traffic match.
  */
 @Service
-@Command(scope = "onos", name = "sr-tmatch-add",
+@Command(scope = "onos", name = "sr-tm-add",
         description = "Create a new traffic match")
 public class TrafficMatchAddCommand extends AbstractShellCommand {
 
@@ -46,44 +47,53 @@
             required = true, multiValued = false)
     String policyId;
 
-    @Argument(index = 1, name = "srcIp",
+    @Option(name = "-sip", aliases = "--srcIp",
             description = "src IP",
-            required = false, multiValued = false)
+            valueToShowInHelp = "10.0.0.1",
+            multiValued = false)
     String srcIp;
 
-    @Argument(index = 2, name = "srcPort",
+    @Option(name = "-sp", aliases = "--srcPort",
             description = "src port",
-            required = false, multiValued = false)
+            valueToShowInHelp = "1001",
+            multiValued = false)
     short srcPort;
 
-    @Argument(index = 3, name = "dstIp",
+    @Option(name = "-dip", aliases = "--dstIp",
             description = "dst IP",
-            required = false, multiValued = false)
+            valueToShowInHelp = "10.0.0.2",
+            multiValued = false)
     String dstIp;
 
-    @Argument(index = 4, name = "dstPort",
+    @Option(name = "-dp", aliases = "--dstPort",
             description = "dst port",
-            required = false, multiValued = false)
+            valueToShowInHelp = "1002",
+            multiValued = false)
     short dstPort;
 
-    @Argument(index = 5, name = "proto",
+    @Option(name = "-p", aliases = "--proto",
             description = "IP protocol",
-            required = false, multiValued = false)
+            valueToShowInHelp = "0x11",
+            multiValued = false)
     String proto;
 
-    @Argument(index = 6, name = "srcMac",
+    // TODO Consider to filter out the following fields for red policies
+    @Option(name = "-smac", aliases = "--srcMac",
             description = "src MAC",
-            required = false, multiValued = false)
+            valueToShowInHelp = "00:00:00:00:00:01",
+            multiValued = false)
     String srcMac;
 
-    @Argument(index = 7, name = "dstMac",
+    @Option(name = "-dmac", aliases = "--dstMac",
             description = "dst MAC",
-            required = false, multiValued = false)
+            valueToShowInHelp = "00:00:00:00:00:02",
+            multiValued = false)
     String dstMac;
 
-    @Argument(index = 8, name = "vlanId",
-            description = "VLAN id",
-            required = false, multiValued = false)
+    @Option(name = "-vid", aliases = "--VlanId",
+            description = "vlan ID",
+            valueToShowInHelp = "10",
+            multiValued = false)
     short vlanId = -1;
 
     @Override
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchListCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchListCommand.java
index c00d1fa..cb5008a 100644
--- a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchListCommand.java
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchListCommand.java
@@ -26,12 +26,12 @@
  * Command to show the list of traffic matches.
  */
 @Service
-@Command(scope = "onos", name = "sr-tmatch-list",
+@Command(scope = "onos", name = "sr-tm",
         description = "Lists all traffic matches")
 public class TrafficMatchListCommand extends AbstractShellCommand {
 
     private static final String FORMAT_MAPPING_TRAFFIC_MATCH =
-            "  id=%s, state=%s";
+            "  id=%s, state=%s, policyId=%s";
     private static final String FORMAT_MAPPING_OPERATION =
             "    op=%s";
 
@@ -43,7 +43,7 @@
 
     private void printTrafficMatch(TrafficMatchData trafficMatchData) {
         print(FORMAT_MAPPING_TRAFFIC_MATCH, trafficMatchData.trafficMatch().trafficMatchId(),
-                trafficMatchData.trafficMatchState());
+                trafficMatchData.trafficMatchState(), trafficMatchData.trafficMatch().policyId());
         trafficMatchData.operations().forEach(operation -> print(FORMAT_MAPPING_OPERATION, operation));
     }
 }
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchRemoveCommand.java b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchRemoveCommand.java
index 763e9c8..f6f0212 100644
--- a/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchRemoveCommand.java
+++ b/impl/src/main/java/org/onosproject/segmentrouting/cli/TrafficMatchRemoveCommand.java
@@ -28,7 +28,7 @@
  * Command to remove a traffic match.
  */
 @Service
-@Command(scope = "onos", name = "sr-tmatch-remove",
+@Command(scope = "onos", name = "sr-tm-remove",
         description = "Remove a traffic match")
 public class TrafficMatchRemoveCommand extends AbstractShellCommand {