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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.config.NetworkConfigService; |
| 29 | import org.onosproject.routing.RoutingService; |
| 30 | import org.onosproject.routing.config.BgpConfig; |
| 31 | |
| 32 | import java.util.HashSet; |
| 33 | import java.util.Optional; |
| 34 | |
| 35 | /** |
| 36 | * Command to add a new internal BGP speaker. |
| 37 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 38 | @Service |
Andreas Pantelopoulos | b6a2f78 | 2016-11-12 17:29:42 +0100 | [diff] [blame] | 39 | @Command(scope = "onos", name = "bgp-speaker-add", |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 40 | description = "Adds an internal BGP speaker") |
| 41 | public 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 46 | private String name = null; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 47 | |
| 48 | @Argument(index = 1, name = "connectionPoint", |
| 49 | description = "Interface to the BGP speaker", |
| 50 | required = true, multiValued = false) |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 51 | 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; |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 58 | |
| 59 | private static final String SPEAKER_ADD_SUCCESS = "Speaker Successfully Added."; |
| 60 | |
| 61 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 63 | 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 69 | 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)); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 81 | } |
| 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 Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 105 | vlanIdObj, |
| 106 | connectPoint, new HashSet<IpAddress>()); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 107 | } |
| 108 | } |