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