blob: 1e257c8e7925e0b38b00572ebd2747d8d3589280 [file] [log] [blame]
Saurav Das7bcbe702017-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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070024import org.apache.karaf.shell.api.action.Command;
Saurav Das7bcbe702017-06-13 15:35:54 -070025import 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.
Saurav Das7bcbe702017-06-13 15:35:54 -070032 */
Charles Chand5814aa2018-08-19 19:21:46 -070033@Command(scope = "onos", name = "sr-next-dst",
Saurav Das7bcbe702017-06-13 15:35:54 -070034 description = "Displays the current next-hops seen by each switch "
35 + "towards a set of destinations and the next-id it maps to")
Charles Chand5814aa2018-08-19 19:21:46 -070036public class NextDstCommand extends AbstractShellCommand {
Saurav Das7bcbe702017-06-13 15:35:54 -070037
38 private static final String FORMAT_MAPPING = " %s";
39
40 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070041 protected void doExecute() {
Saurav Das7bcbe702017-06-13 15:35:54 -070042 SegmentRoutingService srService =
43 AbstractShellCommand.get(SegmentRoutingService.class);
Charles Chand5814aa2018-08-19 19:21:46 -070044 printDestinationSet(srService.getDstNextObjStore());
Saurav Das7bcbe702017-06-13 15:35:54 -070045 }
46
47 private void printDestinationSet(Map<DestinationSetNextObjectiveStoreKey,
48 NextNeighbors> ds) {
49 ArrayList<DestinationSetNextObjectiveStoreKey> a = new ArrayList<>();
50 ds.keySet().forEach(key -> a.add(key));
51 a.sort(new Comp());
52
53 StringBuilder dsbldr = new StringBuilder();
54 for (int i = 0; i < a.size(); i++) {
55 dsbldr.append("\n " + a.get(i));
56 dsbldr.append(" --> via: " + ds.get(a.get(i)));
57 }
58 print(FORMAT_MAPPING, dsbldr.toString());
59 }
60
61 static class Comp implements Comparator<DestinationSetNextObjectiveStoreKey> {
62
63 @Override
64 public int compare(DestinationSetNextObjectiveStoreKey o1,
65 DestinationSetNextObjectiveStoreKey o2) {
66 int res = o1.deviceId().toString().compareTo(o2.deviceId().toString());
67 if (res < 0) {
68 return -1;
69 } else if (res > 0) {
70 return +1;
71 }
72 return 0;
73 }
74
75 }
76}