Moving Multicast subsystem into app and refactoring APIs to support multihomed sources and sinks

Change-Id: I95d07b163c619b018ff106159b134ff214aa2ffd
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostDeleteCommand.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostDeleteCommand.java
new file mode 100644
index 0000000..6d872e8
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostDeleteCommand.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2016-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.mcast.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Deletes a multicast route.
+ */
+@Command(scope = "onos", name = "mcast-host-delete",
+        description = "Delete a multicast route flow")
+public class McastHostDeleteCommand extends AbstractShellCommand {
+
+    // Delete format for group line
+    private static final String D_FORMAT_MAPPING = "Deleted the mcast route: " +
+            "origin=%s, group=%s, source=%s";
+
+    // Update format for group line
+    private static final String U_FORMAT_MAPPING = "Updated the mcast route: " +
+            "origin=%s, group=%s, source=%s";
+
+    @Option(name = "-sAddr", aliases = "--sourceAddress",
+            description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
+            valueToShowInHelp = "1.1.1.1",
+            required = true, multiValued = false)
+    String sAddr = null;
+
+    @Option(name = "-gAddr", aliases = "--groupAddress",
+            description = "IP Address of the multicast group",
+            valueToShowInHelp = "224.0.0.0",
+            required = true, multiValued = false)
+    String gAddr = null;
+
+    @Option(name = "-h", aliases = "--host",
+            description = "Host sink format: MAC/VLAN",
+            valueToShowInHelp = "00:00:00:00:00:00/None")
+    String host = null;
+
+    @Option(name = "-cps", aliases = "--connectPoint",
+            description = "Egress port of:XXXXXXXXXX/XX",
+            valueToShowInHelp = "of:0000000000000001/1",
+            multiValued = true)
+    String[] egressList = null;
+
+
+    @Override
+    protected void execute() {
+        MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
+
+        if ("*".equals(sAddr) && "*".equals(gAddr)) {
+            // Clear all routes
+            mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove);
+            return;
+        }
+        if (host != null && host.isEmpty()) {
+            print("Please provide a non-empty host Id");
+            return;
+        }
+        print("%s", host);
+        HostId hostId = HostId.hostId(host);
+        McastRoute mRoute = new McastRoute(IpAddress.valueOf(sAddr),
+                IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
+        if (!mcastRouteManager.getRoutes().contains(mRoute)) {
+            print("Route is not present, store it first");
+            print(U_FORMAT_MAPPING, mRoute.type(), mRoute.group(), mRoute.source());
+            return;
+        }
+        if (host != null && egressList != null) {
+            Set<ConnectPoint> sinksSet = Arrays.stream(egressList)
+                    .map(ConnectPoint::deviceConnectPoint)
+                    .collect(Collectors.toSet());
+            mcastRouteManager.removeSinks(mRoute, hostId, sinksSet);
+        } else if (host != null) {
+            mcastRouteManager.removeSink(mRoute, hostId);
+        }
+        print(U_FORMAT_MAPPING, mRoute.type(), mRoute.group(), mRoute.source());
+        print("%s", mcastRouteManager.routeData(mRoute));
+    }
+}
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostJoinCommand.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostJoinCommand.java
new file mode 100644
index 0000000..1391822
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastHostJoinCommand.java
@@ -0,0 +1,107 @@
+/*
+ * 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.mcast.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.McastRouteData;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.HostId;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Installs a source, multicast group flow.
+ */
+@Command(scope = "onos", name = "mcast-host-join",
+        description = "Installs a source, multicast group flow")
+public class McastHostJoinCommand extends AbstractShellCommand {
+
+    // Format for group line
+    private static final String FORMAT_MAPPING = "Added the mcast route: " +
+            "origin=%s, group=%s, source=%s";
+
+    @Option(name = "-sAddr", aliases = "--sourceAddress",
+            description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
+            valueToShowInHelp = "1.1.1.1",
+            required = true, multiValued = false)
+    String sAddr = null;
+
+    @Option(name = "-gAddr", aliases = "--groupAddress",
+            description = "IP Address of the multicast group",
+            valueToShowInHelp = "224.0.0.0",
+            required = true, multiValued = false)
+    String gAddr = null;
+
+    @Option(name = "-srcs", aliases = "--sources",
+            description = "Ingress port of:XXXXXXXXXX/XX",
+            valueToShowInHelp = "of:0000000000000001/1",
+            multiValued = true)
+    String[] sources = null;
+
+    @Option(name = "-sinks",
+            aliases = "--hostsinks",
+            description = "Host sink format: MAC/VLAN",
+            valueToShowInHelp = "00:00:00:00:00:00/None",
+            multiValued = true)
+    String[] hosts = null;
+
+    @Override
+    protected void execute() {
+        MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
+
+        IpAddress sAddrIp = null;
+        //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
+        if (!sAddr.equals("*")) {
+            sAddrIp = IpAddress.valueOf(sAddr);
+        }
+
+        McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
+        log.info("Route {}", mRoute);
+        mcastRouteManager.add(mRoute);
+
+        if (sources != null) {
+            Set<ConnectPoint> sourcesSet = Arrays.stream(sources)
+                    .map(ConnectPoint::deviceConnectPoint)
+                    .collect(Collectors.toSet());
+            log.info("{}", sourcesSet);
+            mcastRouteManager.addSources(mRoute, sourcesSet);
+        }
+
+        if (hosts != null) {
+            for (String hostId : hosts) {
+                mcastRouteManager.addSink(mRoute, HostId.hostId(hostId));
+
+            }
+        }
+        printMcastRoute(mRoute);
+        printMcastRouteData(mcastRouteManager.routeData(mRoute));
+    }
+
+    private void printMcastRoute(McastRoute mcastRoute) {
+        print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(), mcastRoute.source());
+    }
+
+    private void printMcastRouteData(McastRouteData mcastRouteData) {
+        print("%s", mcastRouteData);
+    }
+}
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastShowHostCommand.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastShowHostCommand.java
new file mode 100644
index 0000000..562afad
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastShowHostCommand.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2016-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.mcast.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Comparator;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+
+/**
+ * Displays the source, multicast group flows entries.
+ */
+@Command(scope = "onos", name = "mcast-host-show", description = "Displays the source, multicast group flows")
+public class McastShowHostCommand extends AbstractShellCommand {
+
+    // Format for group line
+    private static final String FORMAT_MAPPING = "origin=%s, group=%s, source=%s, sinks=%s";
+
+    @Argument(index = 0, name = "mcastIp", description = "mcast Ip",
+            required = false, multiValued = false)
+    String mcastIp;
+
+    @Override
+    protected void execute() {
+        // Get the service
+        MulticastRouteService mcastService = get(MulticastRouteService.class);
+        // Get the routes
+        Set<McastRoute> routes = mcastService.getRoutes();
+        // Verify mcast group
+        if (!isNullOrEmpty(mcastIp)) {
+            // Let's find the group
+            IpAddress mcastGroup = IpAddress.valueOf(mcastIp);
+            McastRoute mcastRoute = routes.stream()
+                    .filter(route -> route.group().equals(mcastGroup))
+                    .findAny().orElse(null);
+            // If it exists
+            if (mcastRoute != null) {
+                // Get the sinks and print info
+                Set<ConnectPoint> sinks = mcastService.sinks(mcastRoute);
+                Set<ConnectPoint> sources = mcastService.sources(mcastRoute);
+                print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
+                        sources, sinks);
+            }
+            return;
+        }
+        // Filter ipv4
+        Set<McastRoute> ipv4Routes = routes.stream()
+                .filter(mcastRoute -> mcastRoute.group().isIp4())
+                .collect(Collectors.toSet());
+        // Print ipv4 first
+        ipv4Routes.stream()
+                .sorted(Comparator.comparing(McastRoute::group))
+                .forEach(route -> {
+                    // Get sinks
+                    Set<ConnectPoint> sinks = mcastService.sinks(route);
+                    Set<ConnectPoint> sources = mcastService.sources(route);
+                    print(FORMAT_MAPPING, route.type(), route.group(),
+                            sources, sinks);
+                });
+        // Filter ipv6
+        Set<McastRoute> ipv6Routes = routes.stream()
+                .filter(mcastRoute -> mcastRoute.group().isIp6())
+                .collect(Collectors.toSet());
+        // Then print ipv6
+        ipv6Routes.stream()
+                .sorted(Comparator.comparing(McastRoute::group))
+                .forEach(route -> {
+                    // Get sinks
+                    Set<ConnectPoint> sinks = mcastService.sinks(route);
+                    Set<ConnectPoint> sources = mcastService.sources(route);
+                    print(FORMAT_MAPPING, route.type(), route.group(),
+                            sources, sinks);
+                });
+    }
+
+}
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastSourceDeleteCommand.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastSourceDeleteCommand.java
new file mode 100644
index 0000000..d85e155
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/McastSourceDeleteCommand.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2016-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.mcast.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.mcast.api.McastRoute;
+import org.onosproject.mcast.api.MulticastRouteService;
+import org.onosproject.net.ConnectPoint;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Deletes a multicast route.
+ */
+@Command(scope = "onos", name = "mcast-source-delete",
+        description = "Delete a multicast route flow")
+public class McastSourceDeleteCommand extends AbstractShellCommand {
+
+    // Delete format for group line
+    private static final String D_FORMAT_MAPPING = "Deleted the mcast route: " +
+            "origin=%s, group=%s, source=%s";
+
+    // Update format for group line
+    private static final String U_FORMAT_MAPPING = "Updated the mcast route: " +
+            "origin=%s, group=%s, source=%s";
+
+    @Option(name = "-sAddr", aliases = "--sourceAddress",
+            description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
+            valueToShowInHelp = "1.1.1.1",
+            required = true, multiValued = false)
+    String sAddr = null;
+
+    @Option(name = "-gAddr", aliases = "--groupAddress",
+            description = "IP Address of the multicast group",
+            valueToShowInHelp = "224.0.0.0",
+            required = true, multiValued = false)
+    String gAddr = null;
+
+    @Option(name = "-src", aliases = "--connectPoint",
+            description = "Source port of:XXXXXXXXXX/XX",
+            valueToShowInHelp = "of:0000000000000001/1",
+            multiValued = true)
+    String[] sourceList = null;
+
+
+    @Override
+    protected void execute() {
+        MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
+
+        if ("*".equals(sAddr) && "*".equals(gAddr)) {
+            // Clear all routes
+            mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove);
+            return;
+        }
+
+        McastRoute mRoute = new McastRoute(IpAddress.valueOf(sAddr),
+                IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
+        if (!mcastRouteManager.getRoutes().contains(mRoute)) {
+            print("Route is not present, store it first");
+            print("origin=%s, group=%s, source=%s", mRoute.type(), mRoute.group(), mRoute.source());
+            return;
+        }
+        if (sourceList == null) {
+            mcastRouteManager.remove(mRoute);
+            print(D_FORMAT_MAPPING, mRoute.type(), mRoute.group(), mRoute.source());
+            return;
+        }
+
+        Set<ConnectPoint> sourcesSet = Arrays.stream(sourceList)
+                .map(ConnectPoint::deviceConnectPoint)
+                .collect(Collectors.toSet());
+        mcastRouteManager.removeSources(mRoute, sourcesSet);
+        print(U_FORMAT_MAPPING, mRoute.type(), mRoute.group(), mRoute.source());
+        print("%s", mcastRouteManager.routeData(mRoute));
+    }
+}
diff --git a/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/package-info.java b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/package-info.java
new file mode 100644
index 0000000..7cedb77
--- /dev/null
+++ b/apps/mcast/cli/src/main/java/org/onosproject/mcast/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * CLI implementation for mcast.
+ */
+package org.onosproject.mcast.cli;
diff --git a/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..7ab4273
--- /dev/null
+++ b/apps/mcast/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,49 @@
+<!--
+~ 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.
+-->
+<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.mcast.cli.McastHostJoinCommand"/>
+            <optional-completers>
+                <entry key="-srcs" value-ref="deviceIdCompleter"/>
+                <entry key="-sinks" value-ref="hostIdCompleter"/>
+            </optional-completers>
+        </command>
+        <command>
+            <action class="org.onosproject.mcast.cli.McastShowHostCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.mcast.cli.McastHostDeleteCommand"/>
+            <optional-completers>
+                <entry key="-cps" value-ref="deviceIdCompleter"/>
+                <entry key="-h" value-ref="hostIdCompleter"/>
+            </optional-completers>
+        </command>
+        <command>
+            <action class="org.onosproject.mcast.cli.McastSourceDeleteCommand"/>
+            <optional-completers>
+                <entry key="-src" value-ref="deviceIdCompleter"/>
+            </optional-completers>
+        </command>
+
+    </command-bundle>
+
+    <bean id="hostIdCompleter" class="org.onosproject.cli.net.HostIdCompleter"/>
+    <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
+
+</blueprint>