blob: 5ce0b8dd93baaad64fff65636242c3fcbfc0e6de [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;
Luca Prete8f2a3ce2016-03-29 16:13:01 -070023import org.onlab.packet.VlanId;
papazois7d39a742015-10-14 10:15:56 +030024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.routing.RoutingService;
30import org.onosproject.routing.config.BgpConfig;
31
32import java.util.HashSet;
33import java.util.Optional;
34
35/**
36 * Command to add a new internal BGP speaker.
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010039@Command(scope = "onos", name = "bgp-speaker-add",
papazois7d39a742015-10-14 10:15:56 +030040 description = "Adds an internal BGP speaker")
41public class AddSpeakerCommand extends AbstractShellCommand {
42
43 @Argument(index = 0, name = "name",
44 description = "Name of the internal BGP speaker",
45 required = true, multiValued = false)
Luca Prete8f2a3ce2016-03-29 16:13:01 -070046 private String name = null;
papazois7d39a742015-10-14 10:15:56 +030047
48 @Argument(index = 1, name = "connectionPoint",
49 description = "Interface to the BGP speaker",
50 required = true, multiValued = false)
Luca Prete8f2a3ce2016-03-29 16:13:01 -070051 private String connectionPoint = null;
52
53 @Argument(index = 2, name = "vlanId",
54 description = "VLAN Id of the internal BGP speaker",
55 required = false, multiValued = false)
56 private String vlanId = null;
57 private VlanId vlanIdObj = null;
papazois7d39a742015-10-14 10:15:56 +030058
59 private static final String SPEAKER_ADD_SUCCESS = "Speaker Successfully Added.";
60
61 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070062 protected void doExecute() {
papazois7d39a742015-10-14 10:15:56 +030063 NetworkConfigService configService = get(NetworkConfigService.class);
64 CoreService coreService = get(CoreService.class);
65 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
66
67 BgpConfig config = configService.addConfig(appId, BgpConfig.class);
68
Luca Prete8f2a3ce2016-03-29 16:13:01 -070069 if (name != null) {
70 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
71 if (speaker != null) {
72 log.debug("Speaker already exists: {}", name);
73 return;
74 }
75 }
76
77 if (vlanId == null || vlanId.isEmpty()) {
78 vlanIdObj = VlanId.NONE;
79 } else {
80 vlanIdObj = VlanId.vlanId(Short.valueOf(vlanId));
papazois7d39a742015-10-14 10:15:56 +030081 }
82
83 addSpeakerToConf(config);
84 configService.applyConfig(appId, BgpConfig.class, config.node());
85
86 print(SPEAKER_ADD_SUCCESS);
87 }
88
89 /**
90 * Adds the speaker to the BgpConfig service.
91 *
92 * @param config the BGP configuration
93 */
94 private void addSpeakerToConf(BgpConfig config) {
95 log.debug("Adding new speaker to configuration: {}", name);
96 BgpConfig.BgpSpeakerConfig speaker = getSpeaker();
97
98 config.addSpeaker(speaker);
99 }
100
101 private BgpConfig.BgpSpeakerConfig getSpeaker() {
102 ConnectPoint connectPoint = ConnectPoint.
103 deviceConnectPoint(connectionPoint);
104 return new BgpConfig.BgpSpeakerConfig(Optional.ofNullable(name),
Luca Prete8f2a3ce2016-03-29 16:13:01 -0700105 vlanIdObj,
106 connectPoint, new HashSet<IpAddress>());
papazois7d39a742015-10-14 10:15:56 +0300107 }
108}