blob: 9d52e711f55c7ee014adbef81230173624846cd6 [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.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 a internal BGP speaker.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010033@Command(scope = "onos", name = "bgp-speaker-remove",
papazois7d39a742015-10-14 10:15:56 +030034 description = "Removes an internal BGP speaker")
35public class RemoveSpeakerCommand extends AbstractShellCommand {
36
37 @Argument(index = 0, name = "name",
38 description = "Name of the internal BGP speaker",
39 required = true, multiValued = false)
40 String name = null;
41
42 private static final String SPEAKER_REMOVE_SUCCESS = "Speaker Successfully Removed.";
43 private static final String NO_CONFIGURATION = "No speakers configured";
44 private static final String PEERS_EXIST =
45 "Speaker with name \'%s\' has peer connections";
46 private static final String SPEAKER_NOT_FOUND =
47 "Speaker with name \'%s\' not found";
48
49 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 protected void doExecute() {
papazois7d39a742015-10-14 10:15:56 +030051 NetworkConfigService configService = get(NetworkConfigService.class);
52 CoreService coreService = get(CoreService.class);
53 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
54
55 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
56 if (config == null || config.bgpSpeakers().isEmpty()) {
57 print(NO_CONFIGURATION);
58 return;
59 }
60
61 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
62 if (speaker == null) {
63 print(SPEAKER_NOT_FOUND, name);
64 return;
65 } else {
66 if (!speaker.peers().isEmpty()) {
67 // Removal not allowed when peer connections exist.
68 print(PEERS_EXIST, name);
69 return;
70 }
71 }
72
73 removeSpeakerFromConf(config);
74 configService.applyConfig(appId, BgpConfig.class, config.node());
75
76 print(SPEAKER_REMOVE_SUCCESS);
77 }
78
79 /**
80 * Removes the speaker from the BgpConfig service.
81 *
82 * @param bgpConfig the BGP configuration
83 */
84 private void removeSpeakerFromConf(BgpConfig bgpConfig) {
85 log.debug("Removing speaker from configuration: {}", name);
86
87 bgpConfig.removeSpeaker(name);
88 }
89}