blob: 2ce6da20c71a5657aa9dd0d65c707349761827da [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Jonathan Hart2bac2f22016-01-05 21:16:30 -08002 * Copyright 2016 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.onosproject.cli.AbstractShellCommand;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.routing.RoutingService;
26import org.onosproject.routing.config.BgpConfig;
27
28/**
29 * Command to remove a internal BGP speaker.
30 */
31@Command(scope = "onos", name = "remove-bgp-speaker",
32 description = "Removes an internal BGP speaker")
33public class RemoveSpeakerCommand extends AbstractShellCommand {
34
35 @Argument(index = 0, name = "name",
36 description = "Name of the internal BGP speaker",
37 required = true, multiValued = false)
38 String name = null;
39
40 private static final String SPEAKER_REMOVE_SUCCESS = "Speaker Successfully Removed.";
41 private static final String NO_CONFIGURATION = "No speakers configured";
42 private static final String PEERS_EXIST =
43 "Speaker with name \'%s\' has peer connections";
44 private static final String SPEAKER_NOT_FOUND =
45 "Speaker with name \'%s\' not found";
46
47 @Override
48 protected void execute() {
49 NetworkConfigService configService = get(NetworkConfigService.class);
50 CoreService coreService = get(CoreService.class);
51 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
52
53 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
54 if (config == null || config.bgpSpeakers().isEmpty()) {
55 print(NO_CONFIGURATION);
56 return;
57 }
58
59 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
60 if (speaker == null) {
61 print(SPEAKER_NOT_FOUND, name);
62 return;
63 } else {
64 if (!speaker.peers().isEmpty()) {
65 // Removal not allowed when peer connections exist.
66 print(PEERS_EXIST, name);
67 return;
68 }
69 }
70
71 removeSpeakerFromConf(config);
72 configService.applyConfig(appId, BgpConfig.class, config.node());
73
74 print(SPEAKER_REMOVE_SUCCESS);
75 }
76
77 /**
78 * Removes the speaker from the BgpConfig service.
79 *
80 * @param bgpConfig the BGP configuration
81 */
82 private void removeSpeakerFromConf(BgpConfig bgpConfig) {
83 log.debug("Removing speaker from configuration: {}", name);
84
85 bgpConfig.removeSpeaker(name);
86 }
87}