blob: a6700e759545e3c42bedce56f166c21d4601f1df [file] [log] [blame]
Andrea Campanella545edb42018-03-20 16:37:29 -07001/*
2 * Copyright 2016-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;
24import 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 * Deletes a multicast route.
29 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070030@Service
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020031@Command(scope = "onos", name = "mcast-sink-delete",
32 description = "Delete a sink from multicast route flow. If no sin is specified removes the whole route.")
33public class McastSinkDeleteCommand extends AbstractShellCommand {
Andrea Campanella545edb42018-03-20 16:37:29 -070034
35 // Delete format for group line
36 private static final String D_FORMAT_MAPPING = "Deleted the mcast route: " +
37 "origin=%s, group=%s, source=%s";
38
39 // Update format for group line
40 private static final String U_FORMAT_MAPPING = "Updated 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
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020055 @Option(name = "-s", aliases = "--sinks",
Andrea Campanella545edb42018-03-20 16:37:29 -070056 description = "Host sink format: MAC/VLAN",
57 valueToShowInHelp = "00:00:00:00:00:00/None")
58 String host = null;
59
Andrea Campanella545edb42018-03-20 16:37:29 -070060 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070061 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070062 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
Pier139babb2018-03-23 14:59:49 -070063 // Clear all routes
Andrea Campanella545edb42018-03-20 16:37:29 -070064 if ("*".equals(sAddr) && "*".equals(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070065 mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove);
66 return;
67 }
Pier139babb2018-03-23 14:59:49 -070068 // Removing/updating a specific entry
69 IpAddress sAddrIp = null;
70 //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
71 if (!sAddr.equals("*")) {
72 sAddrIp = IpAddress.valueOf(sAddr);
73 }
74 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020075 McastRoute.Type.STATIC);
Pier139babb2018-03-23 14:59:49 -070076 // If the user provides only sAddr and gAddr, we have to remove the route
77 if (host == null || host.isEmpty()) {
78 mcastRouteManager.remove(mRoute);
79 printMcastRoute(D_FORMAT_MAPPING, mRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070080 return;
81 }
Pier139babb2018-03-23 14:59:49 -070082 // Otherwise we need to remove a specific sink
Andrea Campanella545edb42018-03-20 16:37:29 -070083 HostId hostId = HostId.hostId(host);
Andrea Campanella545edb42018-03-20 16:37:29 -070084 if (!mcastRouteManager.getRoutes().contains(mRoute)) {
85 print("Route is not present, store it first");
Andrea Campanella545edb42018-03-20 16:37:29 -070086 return;
87 }
Ray Milkey6fc64c12018-04-12 09:35:33 -070088 // Remove the entire host id
89 mcastRouteManager.removeSink(mRoute, hostId);
90
Pier139babb2018-03-23 14:59:49 -070091 // We have done
92 printMcastRoute(U_FORMAT_MAPPING, mRoute);
93 }
94
95 private void printMcastRoute(String format, McastRoute mcastRoute) {
96 // If the source is present let's use it, otherwise we need to print *
97 print(format, mcastRoute.type(), mcastRoute.group(),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020098 mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*");
Andrea Campanella545edb42018-03-20 16:37:29 -070099 }
100}