blob: e6da695cdd9f696e2322bad4c102fe71a5937ab6 [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
Ray Milkeydb38bc82018-09-27 12:32:28 -070024import org.apache.karaf.shell.api.action.Command;
Ray Milkey52ca4e92018-09-28 10:58:28 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Saurav Das261c3002017-06-13 15:35:54 -070026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.segmentrouting.SegmentRoutingService;
28import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
29import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
30
31/**
32 * Command to read the current state of the DestinationSetNextObjectiveStore.
Saurav Das261c3002017-06-13 15:35:54 -070033 */
Ray Milkey52ca4e92018-09-28 10:58:28 -070034@Service
Charles Chan0b1dd7e2018-08-19 19:21:46 -070035@Command(scope = "onos", name = "sr-next-dst",
Saurav Das261c3002017-06-13 15:35:54 -070036 description = "Displays the current next-hops seen by each switch "
37 + "towards a set of destinations and the next-id it maps to")
Charles Chan0b1dd7e2018-08-19 19:21:46 -070038public class NextDstCommand extends AbstractShellCommand {
Saurav Das261c3002017-06-13 15:35:54 -070039
40 private static final String FORMAT_MAPPING = " %s";
41
42 @Override
Ray Milkeydb38bc82018-09-27 12:32:28 -070043 protected void doExecute() {
Saurav Das261c3002017-06-13 15:35:54 -070044 SegmentRoutingService srService =
45 AbstractShellCommand.get(SegmentRoutingService.class);
Charles Chan0b1dd7e2018-08-19 19:21:46 -070046 printDestinationSet(srService.getDstNextObjStore());
Saurav Das261c3002017-06-13 15:35:54 -070047 }
48
49 private void printDestinationSet(Map<DestinationSetNextObjectiveStoreKey,
50 NextNeighbors> ds) {
51 ArrayList<DestinationSetNextObjectiveStoreKey> a = new ArrayList<>();
52 ds.keySet().forEach(key -> a.add(key));
53 a.sort(new Comp());
54
55 StringBuilder dsbldr = new StringBuilder();
56 for (int i = 0; i < a.size(); i++) {
57 dsbldr.append("\n " + a.get(i));
58 dsbldr.append(" --> via: " + ds.get(a.get(i)));
59 }
60 print(FORMAT_MAPPING, dsbldr.toString());
61 }
62
63 static class Comp implements Comparator<DestinationSetNextObjectiveStoreKey> {
64
65 @Override
66 public int compare(DestinationSetNextObjectiveStoreKey o1,
67 DestinationSetNextObjectiveStoreKey o2) {
68 int res = o1.deviceId().toString().compareTo(o2.deviceId().toString());
69 if (res < 0) {
70 return -1;
71 } else if (res > 0) {
72 return +1;
73 }
74 return 0;
75 }
76
77 }
78}