blob: 2b43de5720c51c7e1fc580def6ffe52b12900b97 [file] [log] [blame]
Saurav Das261c3002017-06-13 15:35:54 -07001/*
2 * Copyright 2016-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
19
20import java.util.ArrayList;
21import java.util.Comparator;
22import java.util.Map;
23
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.segmentrouting.SegmentRoutingService;
27import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
28import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
29
30/**
31 * Command to read the current state of the DestinationSetNextObjectiveStore.
32 *
33 */
34@Command(scope = "onos", name = "sr-next-hops",
35 description = "Displays the current next-hops seen by each switch "
36 + "towards a set of destinations and the next-id it maps to")
37public class NextHopCommand extends AbstractShellCommand {
38
39 private static final String FORMAT_MAPPING = " %s";
40
41 @Override
42 protected void execute() {
43 SegmentRoutingService srService =
44 AbstractShellCommand.get(SegmentRoutingService.class);
45 printDestinationSet(srService.getDestinationSet());
46 }
47
48 private void printDestinationSet(Map<DestinationSetNextObjectiveStoreKey,
49 NextNeighbors> ds) {
50 ArrayList<DestinationSetNextObjectiveStoreKey> a = new ArrayList<>();
51 ds.keySet().forEach(key -> a.add(key));
52 a.sort(new Comp());
53
54 StringBuilder dsbldr = new StringBuilder();
55 for (int i = 0; i < a.size(); i++) {
56 dsbldr.append("\n " + a.get(i));
57 dsbldr.append(" --> via: " + ds.get(a.get(i)));
58 }
59 print(FORMAT_MAPPING, dsbldr.toString());
60 }
61
62 static class Comp implements Comparator<DestinationSetNextObjectiveStoreKey> {
63
64 @Override
65 public int compare(DestinationSetNextObjectiveStoreKey o1,
66 DestinationSetNextObjectiveStoreKey o2) {
67 int res = o1.deviceId().toString().compareTo(o2.deviceId().toString());
68 if (res < 0) {
69 return -1;
70 } else if (res > 0) {
71 return +1;
72 }
73 return 0;
74 }
75
76 }
77}