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; |
| 19 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.mcast.api.McastRoute; |
| 24 | import org.onosproject.mcast.api.MulticastRouteService; |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 25 | import org.onosproject.net.HostId; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Deletes a multicast route. |
| 29 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 30 | @Service |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 31 | @Command(scope = "onos", name = "mcast-source-delete", |
| 32 | description = "Delete a multicast route flow") |
| 33 | public class McastSourceDeleteCommand extends AbstractShellCommand { |
| 34 | |
| 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 | |
| 55 | @Option(name = "-src", aliases = "--connectPoint", |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 56 | description = "Host sink format: MAC/VLAN", |
| 57 | valueToShowInHelp = "00:00:00:00:00:00/None", |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 58 | multiValued = true) |
| 59 | String[] sourceList = null; |
| 60 | |
| 61 | |
| 62 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 63 | protected void doExecute() { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 64 | MulticastRouteService mcastRouteManager = get(MulticastRouteService.class); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 65 | // Clear all routes |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 66 | if ("*".equals(sAddr) && "*".equals(gAddr)) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 67 | mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove); |
| 68 | return; |
| 69 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 70 | // Removing/updating a specific entry |
| 71 | IpAddress sAddrIp = null; |
| 72 | // If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty() |
| 73 | if (!sAddr.equals("*")) { |
| 74 | sAddrIp = IpAddress.valueOf(sAddr); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 75 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 76 | McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 77 | McastRoute.Type.STATIC); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 78 | // No specific connect points, we have to remove everything |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 79 | if (sourceList == null) { |
| 80 | mcastRouteManager.remove(mRoute); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 81 | printMcastRoute(D_FORMAT_MAPPING, mRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 84 | // Otherwise we need to remove specific connect points |
| 85 | if (!mcastRouteManager.getRoutes().contains(mRoute)) { |
| 86 | print("Route is not present, store it first"); |
| 87 | return; |
| 88 | } |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 89 | for (String hostId : sourceList) { |
| 90 | mcastRouteManager.removeSource(mRoute, HostId.hostId(hostId)); |
| 91 | |
| 92 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 93 | printMcastRoute(U_FORMAT_MAPPING, mRoute); |
| 94 | } |
| 95 | |
| 96 | private void printMcastRoute(String format, McastRoute mcastRoute) { |
| 97 | // If the source is present let's use it, otherwise we need to print * |
| 98 | print(format, mcastRoute.type(), mcastRoute.group(), |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 99 | mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*"); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 100 | } |
| 101 | } |