Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | import java.util.Comparator; |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 30 | import com.google.common.collect.ImmutableMap; |
| 31 | import com.google.common.collect.Lists; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Command to read the current state of the DestinationSetNextObjectiveStore. |
| 36 | * |
| 37 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 38 | @Service |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 39 | @Command(scope = "onos", name = "sr-link-state", description = "Displays the current internal link state " |
| 40 | + "noted by this instance of the controller") |
| 41 | public class LinkStateCommand extends AbstractShellCommand { |
| 42 | private static final String FORMAT_MAPPING = " %s"; |
| 43 | |
| 44 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 45 | protected void doExecute() { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 46 | SegmentRoutingService srService = AbstractShellCommand |
| 47 | .get(SegmentRoutingService.class); |
| 48 | printLinkState(srService.getSeenLinks(), |
| 49 | srService.getDownedPortState()); |
| 50 | } |
| 51 | |
| 52 | private void printLinkState(ImmutableMap<Link, Boolean> seenLinks, |
| 53 | ImmutableMap<DeviceId, Set<PortNumber>> downedPortState) { |
| 54 | List<Link> a = Lists.newArrayList(); |
| 55 | a.addAll(seenLinks.keySet()); |
| 56 | a.sort(new CompLinks()); |
| 57 | |
| 58 | StringBuilder slbldr = new StringBuilder(); |
| 59 | slbldr.append("\n Seen Links: "); |
| 60 | for (int i = 0; i < a.size(); i++) { |
| 61 | slbldr.append("\n " |
| 62 | + (seenLinks.get(a.get(i)) == Boolean.TRUE ? " up : " |
| 63 | : "down : ")); |
| 64 | slbldr.append(a.get(i).src() + " --> " + a.get(i).dst()); |
| 65 | } |
| 66 | print(FORMAT_MAPPING, slbldr.toString()); |
| 67 | |
| 68 | StringBuilder dpbldr = new StringBuilder(); |
| 69 | dpbldr.append("\n\n Administratively Disabled Ports: "); |
| 70 | downedPortState.entrySet().forEach(entry -> dpbldr |
| 71 | .append("\n " + entry.getKey() + entry.getValue())); |
| 72 | print(FORMAT_MAPPING, dpbldr.toString()); |
| 73 | } |
| 74 | |
| 75 | static class CompLinks implements Comparator<Link> { |
| 76 | |
| 77 | @Override |
| 78 | public int compare(Link o1, Link o2) { |
| 79 | int res = o1.src().deviceId().toString() |
| 80 | .compareTo(o2.src().deviceId().toString()); |
| 81 | if (res < 0) { |
| 82 | return -1; |
| 83 | } else if (res > 0) { |
| 84 | return +1; |
| 85 | } |
| 86 | if (o1.src().port().toLong() < o2.src().port().toLong()) { |
| 87 | return -1; |
| 88 | } |
| 89 | return +1; |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | |
| 94 | } |