papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.sdnip.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 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 existing BGP peer. |
| 31 | */ |
| 32 | @Command(scope = "onos", name = "remove-bgp-peer", |
| 33 | description = "Removes a BGP peer") |
| 34 | public 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 | } |