blob: 32a72e0c3560eb86b35eeb4dcab0ce63c868fe50 [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;
Ray Milkey8a636bb2018-10-09 14:35:13 -070019import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanella545edb42018-03-20 16:37:29 -070022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
Ray Milkey8a636bb2018-10-09 14:35:13 -070024import org.onosproject.cli.net.HostIdCompleter;
Andrea Campanella545edb42018-03-20 16:37:29 -070025import org.onosproject.mcast.api.McastRoute;
Andrea Campanella545edb42018-03-20 16:37:29 -070026import org.onosproject.mcast.api.MulticastRouteService;
Andrea Campanella545edb42018-03-20 16:37:29 -070027import org.onosproject.net.HostId;
28
Andrea Campanella545edb42018-03-20 16:37:29 -070029/**
30 * Installs a source, multicast group flow.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070033@Command(scope = "onos", name = "mcast-host-join",
34 description = "Installs a source, multicast group flow")
35public class McastHostJoinCommand extends AbstractShellCommand {
36
37 // Format for group line
38 private static final String FORMAT_MAPPING = "Added the mcast route: " +
39 "origin=%s, group=%s, source=%s";
40
41 @Option(name = "-sAddr", aliases = "--sourceAddress",
42 description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
43 valueToShowInHelp = "1.1.1.1",
44 required = true, multiValued = false)
45 String sAddr = null;
46
47 @Option(name = "-gAddr", aliases = "--groupAddress",
48 description = "IP Address of the multicast group",
49 valueToShowInHelp = "224.0.0.0",
50 required = true, multiValued = false)
Ray Milkey8a636bb2018-10-09 14:35:13 -070051 @Completion(McastGroupCompleter.class)
Andrea Campanella545edb42018-03-20 16:37:29 -070052 String gAddr = null;
53
54 @Option(name = "-srcs", aliases = "--sources",
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020055 description = "Host sink format: MAC/VLAN",
56 valueToShowInHelp = "00:00:00:00:00:00/None",
Andrea Campanella545edb42018-03-20 16:37:29 -070057 multiValued = true)
Ray Milkey8a636bb2018-10-09 14:35:13 -070058 @Completion(HostIdCompleter.class)
Andrea Campanella545edb42018-03-20 16:37:29 -070059 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)
Ray Milkey8a636bb2018-10-09 14:35:13 -070066 @Completion(HostIdCompleter.class)
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020067 String[] sinks = null;
Andrea Campanella545edb42018-03-20 16:37:29 -070068
69 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070070 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070071 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
72
73 IpAddress sAddrIp = null;
74 //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
75 if (!sAddr.equals("*")) {
76 sAddrIp = IpAddress.valueOf(sAddr);
77 }
78
79 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC);
Andrea Campanella545edb42018-03-20 16:37:29 -070080 mcastRouteManager.add(mRoute);
81
82 if (sources != null) {
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020083 for (String hostId : sources) {
84 mcastRouteManager.addSource(mRoute, HostId.hostId(hostId));
85 }
Andrea Campanella545edb42018-03-20 16:37:29 -070086 }
87
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020088 if (sinks != null) {
89 for (String hostId : sinks) {
Andrea Campanella545edb42018-03-20 16:37:29 -070090 mcastRouteManager.addSink(mRoute, HostId.hostId(hostId));
Andrea Campanella545edb42018-03-20 16:37:29 -070091 }
92 }
93 printMcastRoute(mRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070094 }
95
96 private void printMcastRoute(McastRoute mcastRoute) {
Pier139babb2018-03-23 14:59:49 -070097 // If the source is present let's use it, otherwise we need to print *
98 print(FORMAT_MAPPING, mcastRoute.type(), mcastRoute.group(),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020099 mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*");
Andrea Campanella545edb42018-03-20 16:37:29 -0700100 }
101
Andrea Campanella545edb42018-03-20 16:37:29 -0700102}