blob: b50c2e26383428e931b55df7595f8c13e57b78ef [file] [log] [blame]
Andreas Papazoisa9964ea2016-01-08 15:58:22 +02001/*
Andreas Papazoise6aebaa2016-05-26 15:25:51 +03002 * Copyright 2016-present Open Networking Laboratory
Andreas Papazoisa9964ea2016-01-08 15:58:22 +02003 *
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
17package org.onosproject.sdxl3.cli;
18
19import com.google.common.collect.Lists;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.routing.RoutingService;
27import org.onosproject.routing.config.BgpConfig;
28import org.onosproject.sdxl3.SdxL3;
Andreas Papazoisc2c45012016-01-20 14:26:11 +020029import org.onosproject.sdxl3.SdxL3PeerService;
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030030import org.onosproject.sdxl3.config.SdxParticipantsConfig;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020031
32import java.util.Comparator;
33import java.util.List;
34import java.util.Optional;
35
36/**
37 * Lists the BGP peers configured in the system.
38 */
39@Command(scope = "onos", name = "bgp-peers",
40 description = "Lists all BGP peers")
41public class BgpPeersListCommand extends AbstractShellCommand {
42
Andreas Papazoisc2c45012016-01-20 14:26:11 +020043 private static final String BASIC_FORMAT = "ip=%s";
44 private static final String DETAILS_FORMAT =
45 BASIC_FORMAT + ", port=%s/%s, intfName=%s";
46 private static final String NAME_FORMAT = "%s: " + DETAILS_FORMAT;
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020047 public static final String NO_PEERS = "No peers configured";
48
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030049 private static final Comparator<SdxParticipantsConfig.PeerConfig> PEER_COMPARATOR =
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020050 Comparator.comparing(p -> p.ip());
51 public static final String EMPTY = "";
52
53 @Override
54 protected void execute() {
55 NetworkConfigService configService = get(NetworkConfigService.class);
56 CoreService coreService = get(CoreService.class);
Andreas Papazoisc2c45012016-01-20 14:26:11 +020057 SdxL3PeerService peerService = get(SdxL3PeerService.class);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020058
59 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
60 BgpConfig bgpConfig = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
61
62 ApplicationId sdxL3AppId = coreService.getAppId(SdxL3.SDX_L3_APP);
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030063 SdxParticipantsConfig peersConfig = configService.
64 getConfig(sdxL3AppId, SdxParticipantsConfig.class);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020065
66 if (bgpConfig == null && peersConfig == null) {
67 print(NO_PEERS);
68 return;
69 }
70
71 List<IpAddress> peeringAddresses = Lists.newArrayList();
72 if (bgpConfig != null) {
Andreas Papazoisc2c45012016-01-20 14:26:11 +020073 // Get all peering addresses from BGP configuration
74 peeringAddresses = peerService.getPeerAddresses(bgpConfig);
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020075 }
76
Andreas Papazoise6aebaa2016-05-26 15:25:51 +030077 List<SdxParticipantsConfig.PeerConfig> bgpPeers =
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020078 Lists.newArrayList();
79 if (peersConfig != null) {
Andreas Papazoisc2c45012016-01-20 14:26:11 +020080 // Get all peers having details specified
Andreas Papazoisa9964ea2016-01-08 15:58:22 +020081 bgpPeers.addAll(peersConfig.bgpPeers());
82 }
83
84 bgpPeers = mergePeers(peeringAddresses, bgpPeers);
85
86 if (bgpPeers.isEmpty()) {
87 print(NO_PEERS);
88 return;
89 }
90
91 bgpPeers.sort(PEER_COMPARATOR);
92 bgpPeers.forEach(p -> {
93 if (p.name().isPresent()) {
Andreas Papazoisc2c45012016-01-20 14:26:11 +020094 print(NAME_FORMAT, p.name().get(), p.ip(),
95 p.connectPoint().deviceId(), p.connectPoint().port(),
96 p.interfaceName());
97 } else if (p.connectPoint() != null) {
98 print(DETAILS_FORMAT, p.ip(), p.connectPoint().deviceId(),
99 p.connectPoint().port(), p.interfaceName());
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200100 } else {
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200101 print(BASIC_FORMAT, p.ip());
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200102 }
103 });
104 }
105
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300106 private List<SdxParticipantsConfig.PeerConfig> mergePeers(
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200107 List<IpAddress> peeringAddresses,
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300108 List<SdxParticipantsConfig.PeerConfig> bgpPeers) {
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200109 peeringAddresses.forEach(a -> {
110 boolean exists = bgpPeers.stream()
111 .filter(p -> p.ip().equals(a))
112 .findAny().isPresent();
113 if (!exists) {
Andreas Papazoise6aebaa2016-05-26 15:25:51 +0300114 bgpPeers.add(new SdxParticipantsConfig
Andreas Papazoisc2c45012016-01-20 14:26:11 +0200115 .PeerConfig(Optional.<String>empty(), a, null, EMPTY));
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200116 }
117 });
Andreas Papazoisa9964ea2016-01-08 15:58:22 +0200118 return bgpPeers;
119 }
120}