blob: 8d89c45e943b3ae002b67d9a4c9b4b50247300e4 [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-present Open Networking Laboratory
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
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onlab.packet.IpAddress;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.incubator.net.intf.InterfaceService;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.routing.RoutingService;
28import org.onosproject.routing.config.BgpConfig;
29
30/**
31 * Command to add new BGP peer to existing internal speaker.
32 */
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010033@Command(scope = "onos", name = "bgp-peer-add",
papazois7d39a742015-10-14 10:15:56 +030034 description = "Adds an external BGP router as peer to an existing BGP speaker")
35public 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}