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 | |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 28 | import org.onosproject.segmentrouting.grouphandler.NextNeighbors; |
| 29 | import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey; |
| 30 | |
| 31 | /** |
| 32 | * Command to read the current state of the DestinationSetNextObjectiveStore. |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 33 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 34 | @Service |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "sr-next-dst", |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 36 | description = "Displays the current next-hops seen by each switch " |
| 37 | + "towards a set of destinations and the next-id it maps to") |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 38 | public class NextDstCommand extends AbstractShellCommand { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 39 | |
| 40 | private static final String FORMAT_MAPPING = " %s"; |
| 41 | |
| 42 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 44 | SegmentRoutingService srService = |
| 45 | AbstractShellCommand.get(SegmentRoutingService.class); |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 46 | printDestinationSet(srService.getDstNextObjStore()); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 47 | } |
| 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 | } |