blob: 84353852dd4630c9565388b2b6fedcaaf8c499ef [file] [log] [blame]
papazois7d39a742015-10-14 10:15:56 +03001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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
17package org.onosproject.sdnip.cli;
18
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.net.ConnectPoint;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.routing.RoutingService;
28import org.onosproject.routing.config.BgpConfig;
29
30import java.util.HashSet;
31import java.util.Optional;
32
33/**
34 * Command to add a new internal BGP speaker.
35 */
36@Command(scope = "onos", name = "add-bgp-speaker",
37 description = "Adds an internal BGP speaker")
38public class AddSpeakerCommand extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "name",
41 description = "Name of the internal BGP speaker",
42 required = true, multiValued = false)
43 String name = null;
44
45 @Argument(index = 1, name = "connectionPoint",
46 description = "Interface to the BGP speaker",
47 required = true, multiValued = false)
48 String connectionPoint = null;
49
50 private static final String SPEAKER_ADD_SUCCESS = "Speaker Successfully Added.";
51
52 @Override
53 protected void execute() {
54 NetworkConfigService configService = get(NetworkConfigService.class);
55 CoreService coreService = get(CoreService.class);
56 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
57
58 BgpConfig config = configService.addConfig(appId, BgpConfig.class);
59
60 BgpConfig.BgpSpeakerConfig speaker = config.getSpeakerWithName(name);
61 if (speaker != null) {
62 log.debug("Speaker already exists: {}", name);
63 return;
64 }
65
66 addSpeakerToConf(config);
67 configService.applyConfig(appId, BgpConfig.class, config.node());
68
69 print(SPEAKER_ADD_SUCCESS);
70 }
71
72 /**
73 * Adds the speaker to the BgpConfig service.
74 *
75 * @param config the BGP configuration
76 */
77 private void addSpeakerToConf(BgpConfig config) {
78 log.debug("Adding new speaker to configuration: {}", name);
79 BgpConfig.BgpSpeakerConfig speaker = getSpeaker();
80
81 config.addSpeaker(speaker);
82 }
83
84 private BgpConfig.BgpSpeakerConfig getSpeaker() {
85 ConnectPoint connectPoint = ConnectPoint.
86 deviceConnectPoint(connectionPoint);
87 return new BgpConfig.BgpSpeakerConfig(Optional.ofNullable(name),
88 connectPoint, new HashSet<IpAddress>());
89 }
90}