blob: 2b22c6786f486b6cc378a64feed518f40be2d946 [file] [log] [blame]
Piere99511d2018-04-19 16:47:06 +02001/*
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
17package org.onosproject.segmentrouting.cli;
18
19import com.google.common.collect.ArrayListMultimap;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Multimap;
Ray Milkeydb38bc82018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Command;
Ray Milkey5105ec32018-10-05 10:17:34 -070023import org.apache.karaf.shell.api.action.Completion;
Ray Milkeydb38bc82018-09-27 12:32:28 -070024import org.apache.karaf.shell.api.action.Option;
Ray Milkey52ca4e92018-09-28 10:58:28 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Piere99511d2018-04-19 16:47:06 +020026import org.onlab.packet.IpAddress;
27import org.onosproject.cli.AbstractShellCommand;
Ray Milkey5105ec32018-10-05 10:17:34 -070028import org.onosproject.cli.net.ConnectPointCompleter;
Piere99511d2018-04-19 16:47:06 +020029import org.onosproject.mcast.cli.McastGroupCompleter;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.DeviceId;
32import org.onosproject.segmentrouting.SegmentRoutingService;
33import org.onosproject.segmentrouting.mcast.McastRole;
34import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
35
36import java.util.Collection;
37import java.util.Map;
38import java.util.Set;
39import java.util.stream.Collectors;
40
41import static com.google.common.base.Strings.isNullOrEmpty;
42
43/**
44 * Command to show the list of mcast roles.
45 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070046@Service
Piere99511d2018-04-19 16:47:06 +020047@Command(scope = "onos", name = "sr-mcast-role",
48 description = "Lists all mcast roles")
49public class McastRoleListCommand extends AbstractShellCommand {
50
51 // OSGi workaround to introduce package dependency
52 McastGroupCompleter completer;
53
54 // Format for group line
55 private static final String FORMAT_MAPPING = "%s,%s ingress=%s\ttransit=%s\tegress=%s";
56
57 @Option(name = "-gAddr", aliases = "--groupAddress",
58 description = "IP Address of the multicast group",
59 valueToShowInHelp = "224.0.0.0",
60 required = false, multiValued = false)
Ray Milkey5105ec32018-10-05 10:17:34 -070061 @Completion(McastGroupCompleter.class)
Piere99511d2018-04-19 16:47:06 +020062 String gAddr = null;
63
64 @Option(name = "-src", aliases = "--connectPoint",
65 description = "Source port of:XXXXXXXXXX/XX",
66 valueToShowInHelp = "of:0000000000000001/1",
67 required = false, multiValued = false)
Ray Milkey5105ec32018-10-05 10:17:34 -070068 @Completion(ConnectPointCompleter.class)
Piere99511d2018-04-19 16:47:06 +020069 String source = null;
70
71 @Override
Ray Milkeydb38bc82018-09-27 12:32:28 -070072 protected void doExecute() {
Piere99511d2018-04-19 16:47:06 +020073 // Verify mcast group
74 IpAddress mcastGroup = null;
75 // We want to use source cp only for a specific group
76 ConnectPoint sourcecp = null;
77 if (!isNullOrEmpty(gAddr)) {
78 mcastGroup = IpAddress.valueOf(gAddr);
79 if (!isNullOrEmpty(source)) {
80 sourcecp = ConnectPoint.deviceConnectPoint(source);
81 }
82 }
83 // Get SR service, the roles and the groups
84 SegmentRoutingService srService = get(SegmentRoutingService.class);
85 Map<McastRoleStoreKey, McastRole> keyToRole = srService.getMcastRoles(mcastGroup, sourcecp);
86 Set<IpAddress> mcastGroups = keyToRole.keySet().stream()
87 .map(McastRoleStoreKey::mcastIp)
88 .collect(Collectors.toSet());
89 // Print the trees for each group
90 mcastGroups.forEach(group -> {
91 // Create a new map for the group
92 Map<ConnectPoint, Multimap<McastRole, DeviceId>> roleDeviceIdMap = Maps.newHashMap();
93 keyToRole.entrySet()
94 .stream()
95 .filter(entry -> entry.getKey().mcastIp().equals(group))
96 .forEach(entry -> roleDeviceIdMap.compute(entry.getKey().source(), (gsource, map) -> {
97 map = map == null ? ArrayListMultimap.create() : map;
98 map.put(entry.getValue(), entry.getKey().deviceId());
99 return map;
100 }));
101 roleDeviceIdMap.forEach((gsource, map) -> {
102 // Print the map
103 printMcastRole(group, gsource,
104 map.get(McastRole.INGRESS),
105 map.get(McastRole.TRANSIT),
106 map.get(McastRole.EGRESS));
107 });
108 });
109 }
110
111 private void printMcastRole(IpAddress mcastGroup, ConnectPoint source,
112 Collection<DeviceId> ingress,
113 Collection<DeviceId> transit,
114 Collection<DeviceId> egress) {
115 print(FORMAT_MAPPING, mcastGroup, source, ingress, transit, egress);
116 }
117}