mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | package org.onosproject.bgp.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.bgp.controller.BgpCfg; |
| 21 | import org.onosproject.bgp.controller.BgpConnectPeer; |
| 22 | import org.onosproject.bgp.controller.BgpController; |
| 23 | import org.onosproject.bgp.controller.BgpPeerCfg; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.util.Set; |
| 29 | import java.util.TreeMap; |
| 30 | |
| 31 | |
| 32 | @Command(scope = "onos", name = "bgp", description = "lists configuration") |
| 33 | public class BgpConfiguration extends AbstractShellCommand { |
| 34 | private static final Logger log = LoggerFactory.getLogger(BgpConfiguration.class); |
| 35 | private static final String CONFIGURATION = "configuration"; |
| 36 | private static final String PEER = "peer"; |
| 37 | protected BgpController bgpController; |
| 38 | protected BgpConnectPeer bgpConnectPeer; |
| 39 | protected BgpPeerCfg bgpPeerCfg; |
| 40 | protected BgpCfg bgpCfg; |
| 41 | @Argument(index = 0, name = "name", |
| 42 | description = "configuration" + "\n" + "peer", |
| 43 | required = true, multiValued = false) |
| 44 | String name = null; |
| 45 | @Argument(index = 1, name = "peer", |
| 46 | description = "peerIp", |
| 47 | required = false, multiValued = false) |
| 48 | String peer = null; |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | switch (name) { |
| 53 | case CONFIGURATION: |
| 54 | displayBgpConfiguration(); |
| 55 | break; |
| 56 | case PEER: |
| 57 | displayBgpPeerConfiguration(); |
| 58 | break; |
| 59 | default: |
| 60 | System.out.print("Unknown command...!!"); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private void displayBgpConfiguration() { |
| 66 | try { |
| 67 | |
| 68 | this.bgpController = get(BgpController.class); |
| 69 | bgpCfg = bgpController.getConfig(); |
| 70 | print("RouterID = %s, ASNumber = %s, MaxSession = %s, HoldingTime = %s, LsCapabality = %s," + |
| 71 | " LargeAsCapabality = %s, FlowSpecCapabality = %s", bgpCfg.getRouterId(), |
| 72 | bgpCfg.getAsNumber(), bgpCfg.getMaxSession(), bgpCfg.getHoldTime(), |
| 73 | bgpCfg.getLsCapability(), bgpCfg.getLargeASCapability(), bgpCfg.flowSpecCapability()); |
| 74 | |
| 75 | } catch (Exception e) { |
| 76 | log.debug("Error occurred while displaying BGP configuration: {}", e.getMessage()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private void displayBgpPeerConfiguration() { |
| 81 | try { |
| 82 | this.bgpController = get(BgpController.class); |
| 83 | BgpCfg bgpCfg = bgpController.getConfig(); |
| 84 | if (bgpCfg == null) { |
| 85 | return; |
| 86 | } |
| 87 | TreeMap<String, BgpPeerCfg> displayPeerTree = bgpCfg.getPeerTree(); |
| 88 | Set<String> peerKey = displayPeerTree.keySet(); |
| 89 | if (peer != null) { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 90 | if (!peerKey.isEmpty()) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 91 | for (String peerIdKey : peerKey) { |
| 92 | bgpPeerCfg = displayPeerTree.get(peerIdKey); |
| 93 | bgpConnectPeer = bgpPeerCfg.connectPeer(); |
| 94 | if (peerIdKey.equals(peer)) { |
| 95 | print("PeerRouterID = %s, PeerHoldingTime = %s, ASNumber = %s, PeerState = %s," + |
| 96 | " PeerPort = %s, ConnectRetryCounter = %s", |
| 97 | peer, bgpPeerCfg.getHoldtime(), bgpPeerCfg.getAsNumber(), |
| 98 | bgpPeerCfg.getState(), bgpConnectPeer.getPeerPort(), |
| 99 | bgpConnectPeer.getConnectRetryCounter()); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } else { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame^] | 104 | if (!peerKey.isEmpty()) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 105 | for (String peerIdKey : peerKey) { |
| 106 | bgpPeerCfg = displayPeerTree.get(peerIdKey); |
| 107 | bgpConnectPeer = bgpPeerCfg.connectPeer(); |
| 108 | print("PeerRouterID = %s, PeerHoldingTime = %s, ASNumber = %s, PeerState = %s, PeerPort = %s," + |
| 109 | " ConnectRetryCounter = %s", |
| 110 | bgpPeerCfg.getPeerRouterId(), bgpPeerCfg.getHoldtime(), bgpPeerCfg.getAsNumber(), |
| 111 | bgpPeerCfg.getState(), bgpConnectPeer.getPeerPort(), |
| 112 | bgpConnectPeer.getConnectRetryCounter()); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | } catch (Exception e) { |
| 118 | log.debug("Error occurred while displaying BGP peer configuration: {}", e.getMessage()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } |