Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.mcast.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.net.HostIdCompleter; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 25 | import org.onosproject.mcast.api.McastRoute; |
| 26 | import org.onosproject.mcast.api.MulticastRouteService; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 27 | import org.onosproject.net.HostId; |
| 28 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Deletes a multicast route. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 33 | @Command(scope = "onos", name = "mcast-sink-delete", |
| 34 | description = "Delete a sink from multicast route flow. If no sin is specified removes the whole route.") |
| 35 | public class McastSinkDeleteCommand extends AbstractShellCommand { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 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 Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 55 | @Completion(McastGroupCompleter.class) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 56 | String gAddr = null; |
| 57 | |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 58 | @Option(name = "-s", aliases = "--sinks", |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 59 | description = "Host sink format: MAC/VLAN", |
| 60 | valueToShowInHelp = "00:00:00:00:00:00/None") |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 61 | @Completion(HostIdCompleter.class) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 62 | String host = null; |
| 63 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 64 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 65 | protected void doExecute() { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 66 | MulticastRouteService mcastRouteManager = get(MulticastRouteService.class); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 67 | // Clear all routes |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 68 | if ("*".equals(sAddr) && "*".equals(gAddr)) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 69 | mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove); |
| 70 | return; |
| 71 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 72 | // Removing/updating a specific entry |
| 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 | McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 79 | McastRoute.Type.STATIC); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 80 | // If the user provides only sAddr and gAddr, we have to remove the route |
| 81 | if (host == null || host.isEmpty()) { |
| 82 | mcastRouteManager.remove(mRoute); |
| 83 | printMcastRoute(D_FORMAT_MAPPING, mRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 84 | return; |
| 85 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 86 | // Otherwise we need to remove a specific sink |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 87 | HostId hostId = HostId.hostId(host); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 88 | if (!mcastRouteManager.getRoutes().contains(mRoute)) { |
| 89 | print("Route is not present, store it first"); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
Ray Milkey | 6fc64c1 | 2018-04-12 09:35:33 -0700 | [diff] [blame] | 92 | // Remove the entire host id |
| 93 | mcastRouteManager.removeSink(mRoute, hostId); |
| 94 | |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 95 | // We have done |
| 96 | printMcastRoute(U_FORMAT_MAPPING, mRoute); |
| 97 | } |
| 98 | |
| 99 | private void printMcastRoute(String format, McastRoute mcastRoute) { |
| 100 | // If the source is present let's use it, otherwise we need to print * |
| 101 | print(format, mcastRoute.type(), mcastRoute.group(), |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 102 | mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*"); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 103 | } |
| 104 | } |