blob: 750fac99526055281fd87c7c85d681708a63a815 [file] [log] [blame]
alshabibeff00542015-09-23 13:22:33 -07001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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 */
16package org.onosproject.mfwd.cli;
17
18import org.apache.karaf.shell.commands.Command;
19import org.onosproject.cli.AbstractShellCommand;
20import org.onosproject.net.ConnectPoint;
21import org.onlab.packet.IpPrefix;
22
23import java.util.Map;
24import java.util.Set;
25
26import org.slf4j.Logger;
27import org.onosproject.mfwd.impl.McastRouteTable;
28import org.onosproject.mfwd.impl.McastRouteGroup;
29
30import com.fasterxml.jackson.databind.JsonNode;
31import com.fasterxml.jackson.databind.ObjectMapper;
32import com.fasterxml.jackson.databind.node.ArrayNode;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Displays the source, multicast group flows entries.
38 */
39@Command(scope = "onos", name = "mcast-show", description = "Displays the source, multicast group flows")
40public class McastShowCommand extends AbstractShellCommand {
41
42 private final Logger log = getLogger(getClass());
43
44 @Override
45 protected void execute() {
46 McastRouteTable mrt = McastRouteTable.getInstance();
47 if (outputJson()) {
48 print("%s", json(mrt));
49 } else {
50 printMrib4(mrt);
51 }
52 }
53
54 public JsonNode json(McastRouteTable mrt) {
55 ObjectMapper mapper = new ObjectMapper();
56 ArrayNode result = mapper.createArrayNode();
57 Map<IpPrefix, McastRouteGroup> mrib4 = mrt.getMrib4();
58 for (McastRouteGroup mg : mrib4.values()) {
59 String sAddr = "";
60 String gAddr = "";
61 String inPort = "";
62 String outPorts = "";
63 if (mg.getSaddr() != null) {
64 sAddr = mg.getSaddr().toString();
65 log.info("Multicast Source: " + sAddr);
66 }
67 if (mg.getGaddr() != null) {
68 gAddr = mg.getGaddr().toString();
69 log.info("Multicast Group: " + gAddr);
70 }
71 if (mg.getIngressPoint() != null) {
72 inPort = mg.getIngressPoint().toString();
73 log.info("Multicast Ingress: " + inPort);
74 }
75 Set<ConnectPoint> eps = mg.getEgressPoints();
76 if (eps != null && !eps.isEmpty()) {
77 outPorts = eps.toString();
78 }
79 result.add(mapper.createObjectNode()
80 .put("src", sAddr)
81 .put("grp", gAddr)
82 .put("inPort", inPort)
83 .put("outPorts", outPorts));
84 }
85 return result;
86 }
87
88 /**
89 * Displays multicast route table entries.
90 *
91 * @param mrt route table
92 */
93 protected void printMrib4(McastRouteTable mrt) {
94 print(mrt.printMcastRouteTable());
95 }
96}