Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [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 | |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 22 | import org.onosproject.segmentrouting.xconnect.api.XconnectKey; |
| 23 | import org.onosproject.segmentrouting.xconnect.api.XconnectService; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Comparator; |
| 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
| 30 | * Command to read the current state of the xconnect next stores. |
| 31 | */ |
Ray Milkey | 52ca4e9 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "sr-next-xconnect", |
| 34 | description = "Displays the current next-id for xconnect") |
| 35 | public class XconnectNextListCommand extends AbstractShellCommand { |
| 36 | @Override |
Ray Milkey | db38bc8 | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 37 | protected void doExecute() { |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 38 | XconnectService xconnectService = |
| 39 | AbstractShellCommand.get(XconnectService.class); |
| 40 | print(xconnectService.getNext()); |
| 41 | } |
| 42 | |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 43 | private void print(Map<XconnectKey, Integer> nextStore) { |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 44 | ArrayList<XconnectKey> a = new ArrayList<>(nextStore.keySet()); |
| 45 | a.sort(Comparator |
| 46 | .comparing((XconnectKey o) -> o.deviceId().toString()) |
| 47 | .thenComparing((XconnectKey o) -> o.vlanId().toShort())); |
| 48 | |
| 49 | StringBuilder builder = new StringBuilder(); |
| 50 | a.forEach(k -> |
| 51 | builder.append("\n") |
| 52 | .append(k) |
| 53 | .append(" --> ") |
Charles Chan | 1fb6513 | 2018-09-21 11:29:12 -0700 | [diff] [blame] | 54 | .append(nextStore.get(k)) |
Charles Chan | 0b1dd7e | 2018-08-19 19:21:46 -0700 | [diff] [blame] | 55 | ); |
| 56 | print(builder.toString()); |
| 57 | } |
| 58 | } |