papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
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 | |
| 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.incubator.net.intf.InterfaceService; |
| 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.routing.RoutingService; |
| 28 | import org.onosproject.routing.config.BgpConfig; |
| 29 | |
| 30 | /** |
| 31 | * Command to add new BGP peer to existing internal speaker. |
| 32 | */ |
Andreas Pantelopoulos | b6a2f78 | 2016-11-12 17:29:42 +0100 | [diff] [blame] | 33 | @Command(scope = "onos", name = "bgp-peer-add", |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 34 | description = "Adds an external BGP router as peer to an existing BGP speaker") |
| 35 | public class AddPeerCommand 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 | @Argument(index = 1, name = "ip", |
| 43 | description = "IP address of the BGP peer", |
| 44 | required = true, multiValued = false) |
| 45 | String ip = null; |
| 46 | |
| 47 | private static final String PEER_ADD_SUCCESS = "Peer Successfully Added."; |
| 48 | private static final String NO_CONFIGURATION = "No speakers configured"; |
| 49 | private static final String SPEAKER_NOT_FOUND = |
| 50 | "Speaker with name \'%s\' not found"; |
| 51 | private static final String NO_INTERFACE = |
| 52 | "No matching interface found for IP \'%s\'"; |
| 53 | |
| 54 | private IpAddress peerAddress = null; |
| 55 | |
| 56 | @Override |
| 57 | protected void execute() { |
| 58 | peerAddress = IpAddress.valueOf(ip); |
| 59 | |
| 60 | NetworkConfigService configService = get(NetworkConfigService.class); |
| 61 | CoreService coreService = get(CoreService.class); |
| 62 | ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 63 | |
| 64 | BgpConfig config = configService.getConfig(appId, BgpConfig.class); |
| 65 | if (config == null || config.bgpSpeakers().isEmpty()) { |
| 66 | print(NO_CONFIGURATION); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name); |
| 71 | if (speaker == null) { |
| 72 | print(SPEAKER_NOT_FOUND, name); |
| 73 | return; |
| 74 | } else { |
| 75 | if (speaker.isConnectedToPeer(peerAddress)) { |
| 76 | return; // Peering already exists. |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | InterfaceService interfaceService = get(InterfaceService.class); |
| 81 | if (interfaceService.getMatchingInterface(peerAddress) == null) { |
| 82 | print(NO_INTERFACE, ip); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | addPeerToSpeakerConf(config); |
| 87 | configService.applyConfig(appId, BgpConfig.class, config.node()); |
| 88 | |
| 89 | print(PEER_ADD_SUCCESS); |
| 90 | } |
| 91 | |
| 92 | private void addPeerToSpeakerConf(BgpConfig config) { |
| 93 | log.debug("Creating BGP configuration for new peer: {}", ip); |
| 94 | config.addPeerToSpeaker(name, peerAddress); |
| 95 | } |
| 96 | } |