William Davies | d368750 | 2019-06-19 20:23:17 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.routing.fpm.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.routing.fpm.FpmInfoService; |
| 24 | import org.onosproject.routing.fpm.FpmPeer; |
| 25 | import org.onosproject.routing.fpm.FpmPeerInfo; |
| 26 | |
| 27 | import java.util.Comparator; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * Displays the acceptRoute flag value for given peer. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "fpm-get-accept-route", |
| 34 | description = "Displays the acceptRoute flag value for given peer") |
| 35 | public class FpmAcceptRoutesInfoCommand extends AbstractShellCommand { |
| 36 | |
| 37 | private static final String FORMAT = "peer %s port %s acceptRoutes %s"; |
| 38 | |
| 39 | @Argument(index = 0, name = "peerAddress", description = "Peer Ip address", |
| 40 | required = false, multiValued = false) |
| 41 | String peerAddress = null; |
| 42 | |
| 43 | |
| 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | FpmInfoService fpmInfo = get(FpmInfoService.class); |
| 48 | if (peerAddress != null) { |
| 49 | IpAddress address = IpAddress.valueOf(peerAddress); |
| 50 | fpmInfo.peers().entrySet().stream() |
| 51 | .filter(peer -> peer.getKey().address().equals(address)) |
| 52 | .map(Map.Entry::getValue) |
| 53 | .forEach(this::print); |
| 54 | } else { |
| 55 | fpmInfo.peers().entrySet().stream() |
| 56 | .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address()) |
| 57 | .thenComparing(e -> e.getKey().port())) |
| 58 | .map(Map.Entry::getValue) |
| 59 | .forEach(this::print); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private void print(FpmPeerInfo info) { |
| 64 | info.connections().forEach(cinfo -> |
| 65 | print(FORMAT, cinfo.peer().address(), |
| 66 | cinfo.peer().port(), |
| 67 | cinfo.isAcceptRoutes()) |
| 68 | ); |
| 69 | } |
| 70 | } |