blob: 3d94f5d6b9291eb403b7034d4d462ee485aa7917 [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;
Andrea Campanella545edb42018-03-20 16:37:29 -070020import org.onlab.packet.IpAddress;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.mcast.api.McastRoute;
Andrea Campanella545edb42018-03-20 16:37:29 -070023import org.onosproject.mcast.api.MulticastRouteService;
Andrea Campanella545edb42018-03-20 16:37:29 -070024import org.onosproject.net.HostId;
25
Andrea Campanella545edb42018-03-20 16:37:29 -070026/**
27 * Installs a source, multicast group flow.
28 */
29@Command(scope = "onos", name = "mcast-host-join",
30 description = "Installs a source, multicast group flow")
31public class McastHostJoinCommand extends AbstractShellCommand {
32
33 // Format for group line
34 private static final String FORMAT_MAPPING = "Added the mcast route: " +
35 "origin=%s, group=%s, source=%s";
36
37 @Option(name = "-sAddr", aliases = "--sourceAddress",
38 description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
39 valueToShowInHelp = "1.1.1.1",
40 required = true, multiValued = false)
41 String sAddr = null;
42
43 @Option(name = "-gAddr", aliases = "--groupAddress",
44 description = "IP Address of the multicast group",
45 valueToShowInHelp = "224.0.0.0",
46 required = true, multiValued = false)
47 String gAddr = null;
48
49 @Option(name = "-srcs", aliases = "--sources",
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020050 description = "Host sink format: MAC/VLAN",
51 valueToShowInHelp = "00:00:00:00:00:00/None",
Andrea Campanella545edb42018-03-20 16:37:29 -070052 multiValued = true)
53 String[] sources = null;
54
55 @Option(name = "-sinks",
56 aliases = "--hostsinks",
57 description = "Host sink format: MAC/VLAN",
58 valueToShowInHelp = "00:00:00:00:00:00/None",
59 multiValued = true)
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020060 String[] sinks = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070061
62 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070063 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070064 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
65
66 IpAddress sAddrIp = null;
67 //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
68 if (!sAddr.equals("*")) {
69 sAddrIp = IpAddress.valueOf(sAddr);
70 }
71
72 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
Andrea Campanella545edb42018-03-20 16:37:29 -070073 mcastRouteManager.add(mRoute);
74
75 if (sources != null) {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020076 for (String hostId : sources) {
77 mcastRouteManager.addSource(mRoute, HostId.hostId(hostId));
78 }
Andrea Campanella545edb42018-03-20 16:37:29 -070079 }
80
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020081 if (sinks != null) {
82 for (String hostId : sinks) {
Andrea Campanella545edb42018-03-20 16:37:29 -070083 mcastRouteManager.addSink(mRoute, HostId.hostId(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -070084 }
85 }
86 printMcastRoute(mRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070087 }
88
89 private void printMcastRoute(McastRoute mcastRoute) {
Pier139babb2018-03-23 14:59:49 -070090 // If the source is present let's use it, otherwise we need to print *
91 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020092 mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*");
Andrea Campanella545edb42018-03-20 16:37:29 -070093 }
94
Andrea Campanella545edb42018-03-20 16:37:29 -070095}