blob: 06293ef33b0ec1abe67d1020cf433a7e90c93c49 [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
papazois7d39a742015-10-14 10:15:56 +03003 *
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
Jonathan Hart2bac2f22016-01-05 21:16:30 -080017package org.onosproject.routing.cli;
papazois7d39a742015-10-14 10:15:56 +030018
19import org.apache.karaf.shell.commands.Argument;
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;
28
29/**
30 * Command to remove existing BGP peer.
31 */
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010032@Command(scope = "onos", name = "bgp-peer-remove",
papazois7d39a742015-10-14 10:15:56 +030033 description = "Removes a BGP peer")
34public class RemovePeerCommand extends AbstractShellCommand {
35
36 @Argument(index = 0, name = "ip",
37 description = "IP address of the BGP peer",
38 required = true, multiValued = false)
39 String ip = null;
40
41 private static final String PEER_REMOVE_SUCCESS = "Peer Successfully Removed.";
42 private static final String NO_CONFIGURATION = "No speakers configured";
43 private static final String PEER_NOT_FOUND =
44 "Peer with IP \'%s\' not found";
45
46 private IpAddress peerAddress = null;
47
48 @Override
49 protected void execute() {
50 peerAddress = IpAddress.valueOf(ip);
51
52 NetworkConfigService configService = get(NetworkConfigService.class);
53 CoreService coreService = get(CoreService.class);
54 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
55
56 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
57 if (config == null || config.bgpSpeakers().isEmpty()) {
58 print(NO_CONFIGURATION);
59 return;
60 }
61
62 peerAddress = IpAddress.valueOf(ip);
63
64 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerFromPeer(peerAddress);
65 if (speaker == null) {
66 print(PEER_NOT_FOUND, ip);
67 return;
68 }
69
70 removePeerFromSpeakerConf(speaker, config);
71 configService.applyConfig(appId, BgpConfig.class, config.node());
72
73 print(PEER_REMOVE_SUCCESS);
74 }
75
76 private void removePeerFromSpeakerConf(BgpConfig.BgpSpeakerConfig speaker,
77 BgpConfig config) {
78 log.debug("Removing BGP configuration for peer: {}", ip);
79 config.removePeerFromSpeaker(speaker, peerAddress);
80 }
81}