blob: 55dc16838bc114f0b3454f86610bc9de8255d91e [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-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;
Luca Prete8f2a3ce2016-03-29 16:13:01 -070022import org.onlab.packet.VlanId;
papazois7d39a742015-10-14 10:15:56 +030023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.routing.RoutingService;
29import org.onosproject.routing.config.BgpConfig;
30
31import java.util.HashSet;
32import java.util.Optional;
33
34/**
35 * Command to add a new internal BGP speaker.
36 */
Andreas Pantelopoulosb6a2f782016-11-12 17:29:42 +010037@Command(scope = "onos", name = "bgp-speaker-add",
papazois7d39a742015-10-14 10:15:56 +030038 description = "Adds an internal BGP speaker")
39public class AddSpeakerCommand extends AbstractShellCommand {
40
41 @Argument(index = 0, name = "name",
42 description = "Name of the internal BGP speaker",
43 required = true, multiValued = false)
Luca Prete8f2a3ce2016-03-29 16:13:01 -070044 private String name = null;
papazois7d39a742015-10-14 10:15:56 +030045
46 @Argument(index = 1, name = "connectionPoint",
47 description = "Interface to the BGP speaker",
48 required = true, multiValued = false)
Luca Prete8f2a3ce2016-03-29 16:13:01 -070049 private String connectionPoint = null;
50
51 @Argument(index = 2, name = "vlanId",
52 description = "VLAN Id of the internal BGP speaker",
53 required = false, multiValued = false)
54 private String vlanId = null;
55 private VlanId vlanIdObj = null;
papazois7d39a742015-10-14 10:15:56 +030056
57 private static final String SPEAKER_ADD_SUCCESS = "Speaker Successfully Added.";
58
59 @Override
60 protected void execute() {
61 NetworkConfigService configService = get(NetworkConfigService.class);
62 CoreService coreService = get(CoreService.class);
63 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
64
65 BgpConfig config = configService.addConfig(appId, BgpConfig.class);
66
Luca Prete8f2a3ce2016-03-29 16:13:01 -070067 if (name != null) {
68 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
69 if (speaker != null) {
70 log.debug("Speaker already exists: {}", name);
71 return;
72 }
73 }
74
75 if (vlanId == null || vlanId.isEmpty()) {
76 vlanIdObj = VlanId.NONE;
77 } else {
78 vlanIdObj = VlanId.vlanId(Short.valueOf(vlanId));
papazois7d39a742015-10-14 10:15:56 +030079 }
80
81 addSpeakerToConf(config);
82 configService.applyConfig(appId, BgpConfig.class, config.node());
83
84 print(SPEAKER_ADD_SUCCESS);
85 }
86
87 /**
88 * Adds the speaker to the BgpConfig service.
89 *
90 * @param config the BGP configuration
91 */
92 private void addSpeakerToConf(BgpConfig config) {
93 log.debug("Adding new speaker to configuration: {}", name);
94 BgpConfig.BgpSpeakerConfig speaker = getSpeaker();
95
96 config.addSpeaker(speaker);
97 }
98
99 private BgpConfig.BgpSpeakerConfig getSpeaker() {
100 ConnectPoint connectPoint = ConnectPoint.
101 deviceConnectPoint(connectionPoint);
102 return new BgpConfig.BgpSpeakerConfig(Optional.ofNullable(name),
Luca Prete8f2a3ce2016-03-29 16:13:01 -0700103 vlanIdObj,
104 connectPoint, new HashSet<IpAddress>());
papazois7d39a742015-10-14 10:15:56 +0300105 }
106}