blob: ded4ae6339adcefa89b4cf309b9a60f1f48e7050 [file] [log] [blame]
Saurav Das95047002018-01-25 09:49:01 -08001/*
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 java.util.Comparator;
20import java.util.List;
21import java.util.Set;
22
23import org.apache.karaf.shell.commands.Command;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Link;
27import org.onosproject.net.PortNumber;
28import org.onosproject.segmentrouting.SegmentRoutingService;
29import com.google.common.collect.ImmutableMap;
30import com.google.common.collect.Lists;
31
32
33/**
34 * Command to read the current state of the DestinationSetNextObjectiveStore.
35 *
36 */
37@Command(scope = "onos", name = "sr-link-state", description = "Displays the current internal link state "
38 + "noted by this instance of the controller")
39public class LinkStateCommand extends AbstractShellCommand {
40 private static final String FORMAT_MAPPING = " %s";
41
42 @Override
43 protected void execute() {
44 SegmentRoutingService srService = AbstractShellCommand
45 .get(SegmentRoutingService.class);
46 printLinkState(srService.getSeenLinks(),
47 srService.getDownedPortState());
48 }
49
50 private void printLinkState(ImmutableMap<Link, Boolean> seenLinks,
51 ImmutableMap<DeviceId, Set<PortNumber>> downedPortState) {
52 List<Link> a = Lists.newArrayList();
53 a.addAll(seenLinks.keySet());
54 a.sort(new CompLinks());
55
56 StringBuilder slbldr = new StringBuilder();
57 slbldr.append("\n Seen Links: ");
58 for (int i = 0; i < a.size(); i++) {
59 slbldr.append("\n "
60 + (seenLinks.get(a.get(i)) == Boolean.TRUE ? " up : "
61 : "down : "));
62 slbldr.append(a.get(i).src() + " --> " + a.get(i).dst());
63 }
64 print(FORMAT_MAPPING, slbldr.toString());
65
66 StringBuilder dpbldr = new StringBuilder();
67 dpbldr.append("\n\n Administratively Disabled Ports: ");
68 downedPortState.entrySet().forEach(entry -> dpbldr
69 .append("\n " + entry.getKey() + entry.getValue()));
70 print(FORMAT_MAPPING, dpbldr.toString());
71 }
72
73 static class CompLinks implements Comparator<Link> {
74
75 @Override
76 public int compare(Link o1, Link o2) {
77 int res = o1.src().deviceId().toString()
78 .compareTo(o2.src().deviceId().toString());
79 if (res < 0) {
80 return -1;
81 } else if (res > 0) {
82 return +1;
83 }
84 if (o1.src().port().toLong() < o2.src().port().toLong()) {
85 return -1;
86 }
87 return +1;
88 }
89
90 }
91
92}