Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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.onlab.onos.sdnip.cli; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.apache.karaf.shell.commands.Option; |
| 26 | import org.onlab.onos.cli.AbstractShellCommand; |
| 27 | import org.onlab.onos.sdnip.SdnIpService; |
| 28 | import org.onlab.onos.sdnip.bgp.BgpSession; |
| 29 | |
| 30 | /** |
| 31 | * Command to show the BGP neighbors. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "bgp-neighbors", |
| 34 | description = "Lists the BGP neighbors") |
| 35 | public class BgpNeighborsListCommand extends AbstractShellCommand { |
| 36 | @Option(name = "-n", aliases = "--neighbor", |
| 37 | description = "BGP neighbor to display information about", |
| 38 | required = false, multiValued = false) |
| 39 | private String bgpNeighbor; |
| 40 | |
| 41 | private static final String FORMAT_NEIGHBOR_LINE1 = |
| 42 | "BGP neighbor is %s, remote AS %d, local AS %d"; |
| 43 | private static final String FORMAT_NEIGHBOR_LINE2 = |
| 44 | " Remote router ID %s, IP %s, BGP version %d, Hold time %d"; |
| 45 | private static final String FORMAT_NEIGHBOR_LINE3 = |
| 46 | " Local router ID %s, IP %s, BGP version %d, Hold time %d"; |
| 47 | |
| 48 | @Override |
| 49 | protected void execute() { |
| 50 | SdnIpService service = get(SdnIpService.class); |
| 51 | Collection<BgpSession> bgpSessions = service.getBgpSessions(); |
| 52 | |
| 53 | if (bgpNeighbor != null) { |
| 54 | // Print a single neighbor (if found) |
| 55 | BgpSession foundBgpSession = null; |
| 56 | for (BgpSession bgpSession : bgpSessions) { |
| 57 | if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) { |
| 58 | foundBgpSession = bgpSession; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | if (foundBgpSession != null) { |
| 63 | printNeighbor(foundBgpSession); |
| 64 | } else { |
| 65 | print("BGP neighbor %s not found", bgpNeighbor); |
| 66 | } |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // Print all neighbors |
| 71 | printNeighbors(bgpSessions); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Prints all BGP neighbors. |
| 76 | * |
| 77 | * @param bgpSessions the BGP sessions for the neighbors to print |
| 78 | */ |
| 79 | private void printNeighbors(Collection<BgpSession> bgpSessions) { |
| 80 | if (outputJson()) { |
| 81 | print("%s", json(bgpSessions)); |
| 82 | } else { |
| 83 | for (BgpSession bgpSession : bgpSessions) { |
| 84 | printNeighbor(bgpSession); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Prints a BGP neighbor. |
| 91 | * |
| 92 | * @param bgpSession the BGP session for the neighbor to print |
| 93 | */ |
| 94 | private void printNeighbor(BgpSession bgpSession) { |
| 95 | print(FORMAT_NEIGHBOR_LINE1, |
| 96 | bgpSession.getRemoteBgpId().toString(), |
| 97 | bgpSession.getRemoteAs(), |
| 98 | bgpSession.getLocalAs()); |
| 99 | print(FORMAT_NEIGHBOR_LINE2, |
| 100 | bgpSession.getRemoteBgpId().toString(), |
| 101 | bgpSession.getRemoteAddress().toString(), |
| 102 | bgpSession.getRemoteBgpVersion(), |
| 103 | bgpSession.getRemoteHoldtime()); |
| 104 | print(FORMAT_NEIGHBOR_LINE3, |
| 105 | bgpSession.getLocalBgpId().toString(), |
| 106 | bgpSession.getLocalAddress().toString(), |
| 107 | bgpSession.getLocalBgpVersion(), |
| 108 | bgpSession.getLocalHoldtime()); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Produces a JSON array of BGP neighbors. |
| 113 | * |
| 114 | * @param bgpSessions the BGP sessions with the data |
| 115 | * @return JSON array with the neighbors |
| 116 | */ |
| 117 | private JsonNode json(Collection<BgpSession> bgpSessions) { |
| 118 | ObjectMapper mapper = new ObjectMapper(); |
| 119 | ArrayNode result = mapper.createArrayNode(); |
| 120 | |
| 121 | for (BgpSession bgpSession : bgpSessions) { |
| 122 | result.add(json(mapper, bgpSession)); |
| 123 | } |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Produces JSON object for a BGP neighbor. |
| 129 | * |
| 130 | * @param mapper the JSON object mapper to use |
| 131 | * @param bgpSession the BGP session with the data |
| 132 | * @return JSON object for the route |
| 133 | */ |
| 134 | private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) { |
| 135 | ObjectNode result = mapper.createObjectNode(); |
| 136 | |
| 137 | result.put("remoteAddress", bgpSession.getRemoteAddress().toString()); |
| 138 | result.put("remoteBgpVersion", bgpSession.getRemoteBgpVersion()); |
| 139 | result.put("remoteAs", bgpSession.getRemoteAs()); |
| 140 | result.put("remoteHoldtime", bgpSession.getRemoteHoldtime()); |
| 141 | result.put("remoteBgpId", bgpSession.getRemoteBgpId().toString()); |
| 142 | // |
| 143 | result.put("localAddress", bgpSession.getLocalAddress().toString()); |
| 144 | result.put("localBgpVersion", bgpSession.getLocalBgpVersion()); |
| 145 | result.put("localAs", bgpSession.getLocalAs()); |
| 146 | result.put("localHoldtime", bgpSession.getLocalHoldtime()); |
| 147 | result.put("localBgpId", bgpSession.getLocalBgpId().toString()); |
| 148 | |
| 149 | return result; |
| 150 | } |
| 151 | } |