blob: 139182222d54c8b4c9ca5ca0c1eb78234075a28c [file] [log] [blame]
Andrea Campanella545edb42018-03-20 16:37:29 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.mcast.cli;
17
18import org.apache.karaf.shell.commands.Command;
19import org.apache.karaf.shell.commands.Option;
20import org.onlab.packet.IpAddress;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.mcast.api.McastRoute;
23import org.onosproject.mcast.api.McastRouteData;
24import org.onosproject.mcast.api.MulticastRouteService;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.HostId;
27
28import java.util.Arrays;
29import java.util.Set;
30import java.util.stream.Collectors;
31
32/**
33 * Installs a source, multicast group flow.
34 */
35@Command(scope = "onos", name = "mcast-host-join",
36 description = "Installs a source, multicast group flow")
37public class McastHostJoinCommand extends AbstractShellCommand {
38
39 // Format for group line
40 private static final String FORMAT_MAPPING = "Added the mcast route: " +
41 "origin=%s, group=%s, source=%s";
42
43 @Option(name = "-sAddr", aliases = "--sourceAddress",
44 description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
45 valueToShowInHelp = "1.1.1.1",
46 required = true, multiValued = false)
47 String sAddr = null;
48
49 @Option(name = "-gAddr", aliases = "--groupAddress",
50 description = "IP Address of the multicast group",
51 valueToShowInHelp = "224.0.0.0",
52 required = true, multiValued = false)
53 String gAddr = null;
54
55 @Option(name = "-srcs", aliases = "--sources",
56 description = "Ingress port of:XXXXXXXXXX/XX",
57 valueToShowInHelp = "of:0000000000000001/1",
58 multiValued = true)
59 String[] sources = null;
60
61 @Option(name = "-sinks",
62 aliases = "--hostsinks",
63 description = "Host sink format: MAC/VLAN",
64 valueToShowInHelp = "00:00:00:00:00:00/None",
65 multiValued = true)
66 String[] hosts = null;
67
68 @Override
69 protected void execute() {
70 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
71
72 IpAddress sAddrIp = null;
73 //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
74 if (!sAddr.equals("*")) {
75 sAddrIp = IpAddress.valueOf(sAddr);
76 }
77
78 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
79 log.info("Route {}", mRoute);
80 mcastRouteManager.add(mRoute);
81
82 if (sources != null) {
83 Set<ConnectPoint> sourcesSet = Arrays.stream(sources)
84 .map(ConnectPoint::deviceConnectPoint)
85 .collect(Collectors.toSet());
86 log.info("{}", sourcesSet);
87 mcastRouteManager.addSources(mRoute, sourcesSet);
88 }
89
90 if (hosts != null) {
91 for (String hostId : hosts) {
92 mcastRouteManager.addSink(mRoute, HostId.hostId(hostId));
93
94 }
95 }
96 printMcastRoute(mRoute);
97 printMcastRouteData(mcastRouteManager.routeData(mRoute));
98 }
99
100 private void printMcastRoute(McastRoute mcastRoute) {
101 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(), mcastRoute.source());
102 }
103
104 private void printMcastRouteData(McastRouteData mcastRouteData) {
105 print("%s", mcastRouteData);
106 }
107}