Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting.cli; |
| 18 | |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Comparator; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 27 | import org.onosproject.segmentrouting.grouphandler.NextNeighbors; |
| 28 | import 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") |
| 37 | public 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 | } |