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