blob: eae5c4834f3293e4656674e9ae63e71873200354 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella545edb42018-03-20 16:37:29 -070021import org.onlab.packet.IpAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.mcast.api.McastRoute;
Andrea Campanella545edb42018-03-20 16:37:29 -070024import org.onosproject.mcast.api.MulticastRouteService;
Andrea Campanella545edb42018-03-20 16:37:29 -070025import org.onosproject.net.HostId;
26
Andrea Campanella545edb42018-03-20 16:37:29 -070027/**
28 * Installs a source, multicast group flow.
29 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070030@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070031@Command(scope = "onos", name = "mcast-host-join",
32 description = "Installs a source, multicast group flow")
33public class McastHostJoinCommand extends AbstractShellCommand {
34
35 // Format for group line
36 private static final String FORMAT_MAPPING = "Added the mcast route: " +
37 "origin=%s, group=%s, source=%s";
38
39 @Option(name = "-sAddr", aliases = "--sourceAddress",
40 description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
41 valueToShowInHelp = "1.1.1.1",
42 required = true, multiValued = false)
43 String sAddr = null;
44
45 @Option(name = "-gAddr", aliases = "--groupAddress",
46 description = "IP Address of the multicast group",
47 valueToShowInHelp = "224.0.0.0",
48 required = true, multiValued = false)
49 String gAddr = null;
50
51 @Option(name = "-srcs", aliases = "--sources",
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020052 description = "Host sink format: MAC/VLAN",
53 valueToShowInHelp = "00:00:00:00:00:00/None",
Andrea Campanella545edb42018-03-20 16:37:29 -070054 multiValued = true)
55 String[] sources = null;
56
57 @Option(name = "-sinks",
58 aliases = "--hostsinks",
59 description = "Host sink format: MAC/VLAN",
60 valueToShowInHelp = "00:00:00:00:00:00/None",
61 multiValued = true)
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020062 String[] sinks = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070063
64 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070066 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
67
68 IpAddress sAddrIp = null;
69 //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
70 if (!sAddr.equals("*")) {
71 sAddrIp = IpAddress.valueOf(sAddr);
72 }
73
74 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
Andrea Campanella545edb42018-03-20 16:37:29 -070075 mcastRouteManager.add(mRoute);
76
77 if (sources != null) {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020078 for (String hostId : sources) {
79 mcastRouteManager.addSource(mRoute, HostId.hostId(hostId));
80 }
Andrea Campanella545edb42018-03-20 16:37:29 -070081 }
82
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020083 if (sinks != null) {
84 for (String hostId : sinks) {
Andrea Campanella545edb42018-03-20 16:37:29 -070085 mcastRouteManager.addSink(mRoute, HostId.hostId(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -070086 }
87 }
88 printMcastRoute(mRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070089 }
90
91 private void printMcastRoute(McastRoute mcastRoute) {
Pier139babb2018-03-23 14:59:49 -070092 // If the source is present let's use it, otherwise we need to print *
93 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020094 mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*");
Andrea Campanella545edb42018-03-20 16:37:29 -070095 }
96
Andrea Campanella545edb42018-03-20 16:37:29 -070097}