papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 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 | |
Jonathan Hart | 2bac2f2 | 2016-01-05 21:16:30 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.cli; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.config.NetworkConfigService; |
| 28 | import org.onosproject.routing.RoutingService; |
| 29 | import org.onosproject.routing.config.BgpConfig; |
| 30 | |
| 31 | import java.util.HashSet; |
| 32 | import java.util.Optional; |
| 33 | |
| 34 | /** |
| 35 | * Command to add a new internal BGP speaker. |
| 36 | */ |
Andreas Pantelopoulos | b6a2f78 | 2016-11-12 17:29:42 +0100 | [diff] [blame] | 37 | @Command(scope = "onos", name = "bgp-speaker-add", |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 38 | description = "Adds an internal BGP speaker") |
| 39 | public 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 44 | private String name = null; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 45 | |
| 46 | @Argument(index = 1, name = "connectionPoint", |
| 47 | description = "Interface to the BGP speaker", |
| 48 | required = true, multiValued = false) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 49 | 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; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 56 | |
| 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 67 | 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)); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 79 | } |
| 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 103 | vlanIdObj, |
| 104 | connectPoint, new HashSet<IpAddress>()); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 105 | } |
| 106 | } |