papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 3 | * |
| 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 Hart | 2bac2f2 | 2016-01-05 21:16:30 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.cli; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 18 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.config.NetworkConfigService; |
| 26 | import org.onosproject.routing.RoutingService; |
| 27 | import org.onosproject.routing.config.BgpConfig; |
| 28 | |
| 29 | /** |
| 30 | * Command to remove a internal BGP speaker. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 32 | @Service |
Andreas Pantelopoulos | b6a2f78 | 2016-11-12 17:29:42 +0100 | [diff] [blame] | 33 | @Command(scope = "onos", name = "bgp-speaker-remove", |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 34 | description = "Removes an internal BGP speaker") |
| 35 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 51 | 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 | } |