blob: f0f2f76d1f564a8eddd06139f24b2080ed54a7fd [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
papazois7d39a742015-10-14 10:15:56 +030022import org.onlab.packet.IpAddress;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Ray Milkeyfacf2862017-08-03 11:58:29 -070026import org.onosproject.net.intf.InterfaceService;
papazois7d39a742015-10-14 10:15:56 +030027import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.routing.RoutingService;
29import org.onosproject.routing.config.BgpConfig;
30
31/**
32 * Command to add new BGP peer to existing internal speaker.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010035@Command(scope = "onos", name = "bgp-peer-add",
papazois7d39a742015-10-14 10:15:56 +030036 description = "Adds an external BGP router as peer to an existing BGP speaker")
37public 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 Milkey86ad7bb2018-09-27 12:32:28 -070059 protected void doExecute() {
papazois7d39a742015-10-14 10:15:56 +030060 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}