T3: Adding a test all multicast routes command

Change-Id: I8fe144cc8f5c4c592f2ac78bdc9d0417c0ab842d
(cherry picked from commit d3130e8efa30c8d5ff3d1baa890e94ba3fab04d9)
diff --git a/BUCK b/BUCK
index f28792c..bb18951 100644
--- a/BUCK
+++ b/BUCK
@@ -9,6 +9,7 @@
     '//drivers/default:onos-drivers-default',
     '//apps/segmentrouting/app:onos-apps-segmentrouting-app',
     '//apps/route-service/api:onos-apps-route-service-api',
+    '//apps/mcast/api:onos-apps-mcast-api',
 ]
 
 TEST_DEPS = [
@@ -28,5 +29,9 @@
     url = 'https://wiki.opencord.org/pages/viewpage.action?pageId=4456974',
     description = 'Provides static analysis of flows and groups ' +
     'to determine the possible paths a packet may take.',
-    required_apps = [ 'org.onosproject.segmentrouting', 'org.onosproject.route-service' ],
+    required_apps = [
+        'org.onosproject.segmentrouting',
+        'org.onosproject.route-service',
+        'org.onosproject.mcast',
+    ],
 )
diff --git a/src/main/java/org/onosproject/t3/api/TroubleshootService.java b/src/main/java/org/onosproject/t3/api/TroubleshootService.java
index ab7ff29..5b19cc2 100644
--- a/src/main/java/org/onosproject/t3/api/TroubleshootService.java
+++ b/src/main/java/org/onosproject/t3/api/TroubleshootService.java
@@ -17,6 +17,7 @@
 package org.onosproject.t3.api;
 
 import org.onlab.packet.EthType;
+import org.onlab.packet.VlanId;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.HostId;
 import org.onosproject.net.flow.TrafficSelector;
@@ -48,6 +49,14 @@
     Generator<Set<StaticPacketTrace>> pingAllGenerator(EthType.EtherType type);
 
     /**
+     * Requests a static trace be performed for all mcast Routes in the network.
+     *
+     * @param vlanId the vlanId configured for multicast.
+     * @return a set of trace result yielded one by one.
+     */
+    Generator<Set<StaticPacketTrace>> traceMcast(VlanId vlanId);
+
+    /**
      * Requests a static trace be performed between the two hosts in the network, given a type of traffic.
      *
      * @param sourceHost      source host
diff --git a/src/main/java/org/onosproject/t3/cli/TroubleshootMcastCommand.java b/src/main/java/org/onosproject/t3/cli/TroubleshootMcastCommand.java
new file mode 100644
index 0000000..1591758
--- /dev/null
+++ b/src/main/java/org/onosproject/t3/cli/TroubleshootMcastCommand.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2018-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.t3.cli;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.VlanId;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.t3.api.StaticPacketTrace;
+import org.onosproject.t3.api.TroubleshootService;
+import org.onosproject.t3.impl.Generator;
+
+import java.util.Set;
+
+/**
+ * Starts a Static Packet Trace for all the multicast routes in the system and prints the result.
+ */
+@Command(scope = "onos", name = "t3-troubleshoot-mcast",
+        description = "Traces all the mcast routes present in the system")
+public class TroubleshootMcastCommand extends AbstractShellCommand {
+
+
+    @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each host to host combination")
+    private boolean verbosity1 = false;
+
+    @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs details of every trace")
+    private boolean verbosity2 = false;
+
+    @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
+    String vlan = "None";
+
+
+    @Override
+    protected void execute() {
+        TroubleshootService service = get(TroubleshootService.class);
+        print("Tracing all Multicast routes in the System");
+        print("%s", StringUtils.rightPad("", 125, '-'));
+
+            //Create the generator for the list of traces.
+        VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan);
+        Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId);
+        while (generator.iterator().hasNext()) {
+            //Print also Route if possible or packet
+            Set<StaticPacketTrace> traces = generator.iterator().next();
+            traces.forEach(trace -> {
+                print("Tracing packet: %s", trace.getInitialPacket());
+                print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2));
+                print("%s", StringUtils.rightPad("", 125, '-'));
+            });
+        }
+
+    }
+}
diff --git a/src/main/java/org/onosproject/t3/impl/McastGenerator.java b/src/main/java/org/onosproject/t3/impl/McastGenerator.java
new file mode 100644
index 0000000..59d5ba6
--- /dev/null
+++ b/src/main/java/org/onosproject/t3/impl/McastGenerator.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2018-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.t3.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.packet.EthType;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.mcast.api.McastRouteData;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.t3.api.StaticPacketTrace;
+import org.slf4j.Logger;
+
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the generator class that yields a set of Packet Traces.
+ */
+public class McastGenerator extends Generator<Set<StaticPacketTrace>> {
+
+    private static final Logger log = getLogger(McastGenerator.class);
+    protected static final MacAddress IPV4_ADDRESS = MacAddress.valueOf("01:00:5E:00:00:00");
+    protected static final MacAddress IPV6_ADDRESS = MacAddress.valueOf("33:33:00:00:00:00");
+
+    private final MulticastRouteService mcastService;
+    private final TroubleshootManager manager;
+    private final VlanId vlanId;
+
+    /**
+     * Creates a generator for obtaining traces of all configured multicast routes.
+     *
+     * @param service the host service
+     * @param manager the troubleshoot manager issuing the request.
+     * @param vlanId  the multicast configured VlanId.
+     */
+    McastGenerator(MulticastRouteService service, TroubleshootManager manager, VlanId vlanId) {
+        this.mcastService = service;
+        this.manager = manager;
+        this.vlanId = vlanId;
+    }
+
+    @Override
+    protected void run() {
+        mcastService.getRoutes().forEach(route -> {
+            McastRouteData routeData = mcastService.routeData(route);
+            IpAddress group = route.group();
+            routeData.sources().forEach(source -> {
+                TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
+                        .matchVlanId(vlanId)
+                        .matchInPort(source.port());
+                if (group.isIp4()) {
+                    selector.matchEthDst(IPV4_ADDRESS)
+                            .matchIPDst(group.toIpPrefix())
+                            .matchEthType(EthType.EtherType.IPV4.ethType().toShort());
+                } else {
+                    selector.matchEthDst(IPV6_ADDRESS)
+                            .matchIPv6Dst(group.toIpPrefix())
+                            .matchEthType(EthType.EtherType.IPV6.ethType().toShort());
+                }
+                try {
+                    yield(ImmutableSet.of(manager.trace(selector.build(), source)));
+                } catch (InterruptedException e) {
+                    log.warn("Interrupted generator", e.getMessage());
+                    log.debug("exception", e);
+                }
+            });
+
+        });
+
+    }
+}
diff --git a/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java b/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
index 4f22f97..5b245b2 100644
--- a/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
+++ b/src/main/java/org/onosproject/t3/impl/TroubleshootManager.java
@@ -30,6 +30,7 @@
 import org.onlab.packet.VlanId;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.mcast.api.MulticastRouteService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
@@ -139,6 +140,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RouteService routeService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MulticastRouteService mcastService;
+
     @Override
     public List<StaticPacketTrace> pingAll(EtherType type) {
         ImmutableList.Builder<StaticPacketTrace> tracesBuilder = ImmutableList.builder();
@@ -171,6 +175,11 @@
     }
 
     @Override
+    public Generator<Set<StaticPacketTrace>> traceMcast(VlanId vlanId) {
+        return new McastGenerator(mcastService, this, vlanId);
+    }
+
+    @Override
     public Set<StaticPacketTrace> trace(HostId sourceHost, HostId destinationHost, EtherType etherType) {
         Host source = hostService.getHost(sourceHost);
         Host destination = hostService.getHost(destinationHost);
diff --git a/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 658b912..48df042 100644
--- a/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -34,6 +34,9 @@
                 <entry key="-et" value-ref="ethTypeCompleter"/>
             </optional-completers>
         </command>
+        <command>
+            <action class="org.onosproject.t3.cli.TroubleshootMcastCommand"/>
+        </command>
     </command-bundle>
 
     <bean id="hostIdCompleter" class="org.onosproject.cli.net.HostIdCompleter"/>