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