blob: 757829c99ef3f1da8712450ba77c1c2e15c21788 [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;
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;
26import org.onosproject.mcast.api.MulticastRouteService;
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020027import org.onosproject.net.HostId;
Andrea Campanella545edb42018-03-20 16:37:29 -070028
29/**
30 * Deletes a multicast route.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Andrea Campanella545edb42018-03-20 16:37:29 -070033@Command(scope = "onos", name = "mcast-source-delete",
34 description = "Delete a multicast route flow")
35public class McastSourceDeleteCommand extends AbstractShellCommand {
36
37 // Delete format for group line
38 private static final String D_FORMAT_MAPPING = "Deleted the mcast route: " +
39 "origin=%s, group=%s, source=%s";
40
41 // Update format for group line
42 private static final String U_FORMAT_MAPPING = "Updated the mcast route: " +
43 "origin=%s, group=%s, source=%s";
44
45 @Option(name = "-sAddr", aliases = "--sourceAddress",
46 description = "IP Address of the multicast source. '*' can be used for any source (*, G) entry",
47 valueToShowInHelp = "1.1.1.1",
48 required = true, multiValued = false)
49 String sAddr = null;
50
51 @Option(name = "-gAddr", aliases = "--groupAddress",
52 description = "IP Address of the multicast group",
53 valueToShowInHelp = "224.0.0.0",
54 required = true, multiValued = false)
Ray Milkey8a636bb2018-10-09 14:35:13 -070055 @Completion(McastGroupCompleter.class)
Andrea Campanella545edb42018-03-20 16:37:29 -070056 String gAddr = null;
57
58 @Option(name = "-src", aliases = "--connectPoint",
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020059 description = "Host sink format: MAC/VLAN",
60 valueToShowInHelp = "00:00:00:00:00:00/None",
Andrea Campanella545edb42018-03-20 16:37:29 -070061 multiValued = true)
Ray Milkey8a636bb2018-10-09 14:35:13 -070062 @Completion(HostIdCompleter.class)
Andrea Campanella545edb42018-03-20 16:37:29 -070063 String[] sourceList = null;
64
65
66 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070067 protected void doExecute() {
Andrea Campanella545edb42018-03-20 16:37:29 -070068 MulticastRouteService mcastRouteManager = get(MulticastRouteService.class);
Pier139babb2018-03-23 14:59:49 -070069 // Clear all routes
Andrea Campanella545edb42018-03-20 16:37:29 -070070 if ("*".equals(sAddr) && "*".equals(gAddr)) {
Andrea Campanella545edb42018-03-20 16:37:29 -070071 mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove);
72 return;
73 }
Pier139babb2018-03-23 14:59:49 -070074 // Removing/updating a specific entry
75 IpAddress sAddrIp = null;
76 // If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty()
77 if (!sAddr.equals("*")) {
78 sAddrIp = IpAddress.valueOf(sAddr);
Andrea Campanella545edb42018-03-20 16:37:29 -070079 }
Pier139babb2018-03-23 14:59:49 -070080 McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020081 McastRoute.Type.STATIC);
Pier139babb2018-03-23 14:59:49 -070082 // No specific connect points, we have to remove everything
Andrea Campanella545edb42018-03-20 16:37:29 -070083 if (sourceList == null) {
84 mcastRouteManager.remove(mRoute);
Pier139babb2018-03-23 14:59:49 -070085 printMcastRoute(D_FORMAT_MAPPING, mRoute);
Andrea Campanella545edb42018-03-20 16:37:29 -070086 return;
87 }
Pier139babb2018-03-23 14:59:49 -070088 // Otherwise we need to remove specific connect points
89 if (!mcastRouteManager.getRoutes().contains(mRoute)) {
90 print("Route is not present, store it first");
91 return;
92 }
Andrea Campanella0ddf9b82018-04-27 15:54:42 +020093 for (String hostId : sourceList) {
94 mcastRouteManager.removeSource(mRoute, HostId.hostId(hostId));
95
96 }
Pier139babb2018-03-23 14:59:49 -070097 printMcastRoute(U_FORMAT_MAPPING, mRoute);
98 }
99
100 private void printMcastRoute(String format, McastRoute mcastRoute) {
101 // If the source is present let's use it, otherwise we need to print *
102 print(format, mcastRoute.type(), mcastRoute.group(),
Andrea Campanella0ddf9b82018-04-27 15:54:42 +0200103 mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*");
Andrea Campanella545edb42018-03-20 16:37:29 -0700104 }
105}