Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 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 | * Installs a source, multicast group flow. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "mcast-host-join", |
| 34 | description = "Installs a source, multicast group flow") |
| 35 | public class McastHostJoinCommand extends AbstractShellCommand { |
| 36 | |
| 37 | // Format for group line |
| 38 | private static final String FORMAT_MAPPING = "Added 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) |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 51 | @Completion(McastGroupCompleter.class) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 52 | String gAddr = null; |
| 53 | |
| 54 | @Option(name = "-srcs", aliases = "--sources", |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 55 | description = "Host sink format: MAC/VLAN", |
| 56 | valueToShowInHelp = "00:00:00:00:00:00/None", |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 57 | multiValued = true) |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 58 | @Completion(HostIdCompleter.class) |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 59 | String[] sources = null; |
| 60 | |
| 61 | @Option(name = "-sinks", |
| 62 | aliases = "--hostsinks", |
| 63 | description = "Host sink format: MAC/VLAN", |
| 64 | valueToShowInHelp = "00:00:00:00:00:00/None", |
| 65 | multiValued = true) |
Ray Milkey | 8a636bb | 2018-10-09 14:35:13 -0700 | [diff] [blame] | 66 | @Completion(HostIdCompleter.class) |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 67 | String[] sinks = null; |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 68 | |
| 69 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 70 | protected void doExecute() { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 71 | MulticastRouteService mcastRouteManager = get(MulticastRouteService.class); |
| 72 | |
| 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 | |
| 79 | McastRoute mRoute = new McastRoute(sAddrIp, IpAddress.valueOf(gAddr), McastRoute.Type.STATIC); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 80 | mcastRouteManager.add(mRoute); |
| 81 | |
| 82 | if (sources != null) { |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 83 | for (String hostId : sources) { |
| 84 | mcastRouteManager.addSource(mRoute, HostId.hostId(hostId)); |
| 85 | } |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame] | 88 | if (sinks != null) { |
| 89 | for (String hostId : sinks) { |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 90 | mcastRouteManager.addSink(mRoute, HostId.hostId(hostId)); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | printMcastRoute(mRoute); |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private void printMcastRoute(McastRoute mcastRoute) { |
Pier | 139babb | 2018-03-23 14:59:49 -0700 | [diff] [blame] | 97 | // If the source is present let's use it, otherwise we need to print * |
| 98 | print(FORMAT_MAPPING, 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 | |
Andrea Campanella | 545edb4 | 2018-03-20 16:37:29 -0700 | [diff] [blame] | 102 | } |