blob: a4b2912ca165069acddd95d03a18c0075c84a238 [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
papazois7d39a742015-10-14 10:15:56 +030022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.routing.RoutingService;
28import org.onosproject.routing.config.BgpConfig;
29
30/**
31 * Command to remove existing BGP peer.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010034@Command(scope = "onos", name = "bgp-peer-remove",
papazois7d39a742015-10-14 10:15:56 +030035 description = "Removes a BGP peer")
36public class RemovePeerCommand extends AbstractShellCommand {
37
38 @Argument(index = 0, name = "ip",
39 description = "IP address of the BGP peer",
40 required = true, multiValued = false)
41 String ip = null;
42
43 private static final String PEER_REMOVE_SUCCESS = "Peer Successfully Removed.";
44 private static final String NO_CONFIGURATION = "No speakers configured";
45 private static final String PEER_NOT_FOUND =
46 "Peer with IP \'%s\' not found";
47
48 private IpAddress peerAddress = null;
49
50 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 protected void doExecute() {
papazois7d39a742015-10-14 10:15:56 +030052 peerAddress = IpAddress.valueOf(ip);
53
54 NetworkConfigService configService = get(NetworkConfigService.class);
55 CoreService coreService = get(CoreService.class);
56 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
57
58 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
59 if (config == null || config.bgpSpeakers().isEmpty()) {
60 print(NO_CONFIGURATION);
61 return;
62 }
63
64 peerAddress = IpAddress.valueOf(ip);
65
66 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerFromPeer(peerAddress);
67 if (speaker == null) {
68 print(PEER_NOT_FOUND, ip);
69 return;
70 }
71
72 removePeerFromSpeakerConf(speaker, config);
73 configService.applyConfig(appId, BgpConfig.class, config.node());
74
75 print(PEER_REMOVE_SUCCESS);
76 }
77
78 private void removePeerFromSpeakerConf(BgpConfig.BgpSpeakerConfig speaker,
79 BgpConfig config) {
80 log.debug("Removing BGP configuration for peer: {}", ip);
81 config.removePeerFromSpeaker(speaker, peerAddress);
82 }
83}