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 | |
| 18 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.apache.karaf.shell.commands.Option; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.mcast.api.McastRoute; |
| 23 | import org.onosproject.mcast.api.MulticastRouteService; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 24 | import org.onosproject.net.HostId; |
| 25 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Deletes a multicast route. |
| 28 | */ |
| 29 | @Command(scope = "onos", name = "mcast-host-delete", |
| 30 | description = "Delete a multicast route flow") |
| 31 | public class McastHostDeleteCommand extends AbstractShellCommand { |
| 32 | |
| 33 | // Delete format for group line |
| 34 | private static final String D_FORMAT_MAPPING = "Deleted the mcast route: " + |
| 35 | "origin=%s, group=%s, source=%s"; |
| 36 | |
| 37 | // Update format for group line |
| 38 | private static final String U_FORMAT_MAPPING = "Updated 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) |
| 51 | String gAddr = null; |
| 52 | |
| 53 | @Option(name = "-h", aliases = "--host", |
| 54 | description = "Host sink format: MAC/VLAN", |
| 55 | valueToShowInHelp = "00:00:00:00:00:00/None") |
| 56 | String host = null; |
| 57 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 58 | @Override |
| 59 | protected void execute() { |
| 60 | MulticastRouteService mcastRouteManager = get(MulticastRouteService.class); |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 61 | // Clear all routes |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 62 | if ("*".equals(sAddr) && "*".equals(gAddr)) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 63 | mcastRouteManager.getRoutes().forEach(mcastRouteManager::remove); |
| 64 | return; |
| 65 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 66 | // Removing/updating a specific entry |
| 67 | IpAddress sAddrIp = null; |
| 68 | //If the source Ip is * we want ASM so we leave it as null and the route will have it as an optional.empty() |
| 69 | if (!sAddr.equals("*")) { |
| 70 | sAddrIp = IpAddress.valueOf(sAddr); |
| 71 | } |
| 72 | McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), |
| 73 | McastRoute.Type.STATIC); |
| 74 | // If the user provides only sAddr and gAddr, we have to remove the route |
| 75 | if (host == null || host.isEmpty()) { |
| 76 | mcastRouteManager.remove(mRoute); |
| 77 | printMcastRoute(D_FORMAT_MAPPING, mRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 78 | return; |
| 79 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 80 | // Otherwise we need to remove a specific sink |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 81 | HostId hostId = HostId.hostId(host); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 82 | if (!mcastRouteManager.getRoutes().contains(mRoute)) { |
| 83 | print("Route is not present, store it first"); |
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 remove the entire host id |
Andrea Campanella | 7c8bcdf | 2018-03-26 23:29:11 -0700 | [diff] [blame] | 87 | if (host != null) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 88 | mcastRouteManager.removeSink(mRoute, hostId); |
| 89 | } |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 90 | // We have done |
| 91 | printMcastRoute(U_FORMAT_MAPPING, mRoute); |
| 92 | } |
| 93 | |
| 94 | private void printMcastRoute(String format, McastRoute mcastRoute) { |
| 95 | // If the source is present let's use it, otherwise we need to print * |
| 96 | print(format, mcastRoute.type(), mcastRoute.group(), |
| 97 | mcastRoute.source().isPresent() ? mcastRoute.source().get() : "*"); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 98 | } |
| 99 | } |