Yuta HIGUCHI | ea7efa4 | 2017-02-16 20:34:25 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | ea7efa4 | 2017-02-16 20:34:25 -0800 | [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 | package org.onosproject.provider.lldp.cli; |
| 17 | |
| 18 | import java.util.Optional; |
| 19 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | ea7efa4 | 2017-02-16 20:34:25 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 25 | import org.onosproject.cli.net.PortNumberCompleter; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.provider.lldp.impl.LinkDiscoveryFromDevice; |
| 32 | import org.onosproject.provider.lldp.impl.LinkDiscoveryFromPort; |
| 33 | |
| 34 | /** |
| 35 | * |
| 36 | */ |
| 37 | @Command(scope = "onos", name = "config-link-discovery", |
| 38 | description = "Adds configuration to disable LLDP link discovery") |
| 39 | public class ConfigLinkDiscoveryCommand extends AbstractShellCommand { |
| 40 | |
| 41 | // OSGi workaround to introduce package dependency |
| 42 | DeviceIdCompleter deviceIdCompleter; |
| 43 | @Argument(index = 0, name = "device", |
| 44 | description = "DeviceID", |
| 45 | required = true) |
| 46 | String device = null; |
| 47 | |
| 48 | |
| 49 | // OSGi workaround to introduce package dependency |
| 50 | PortNumberCompleter portNumberCompleter; |
| 51 | @Argument(index = 1, name = "port", |
| 52 | description = "Port number", |
| 53 | required = false) |
| 54 | String port = null; |
| 55 | |
| 56 | @Option(name = "--remove", aliases = "-r", |
| 57 | description = "remove configuration", |
| 58 | required = false) |
| 59 | boolean remove = false; |
| 60 | |
| 61 | @Option(name = "--enable", |
| 62 | description = "add configuration to enable LinkDiscovery", |
| 63 | required = false) |
| 64 | boolean enable = false; |
| 65 | |
| 66 | |
| 67 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 68 | protected void doExecute() { |
Yuta HIGUCHI | ea7efa4 | 2017-02-16 20:34:25 -0800 | [diff] [blame] | 69 | DeviceService deviceService = get(DeviceService.class); |
| 70 | NetworkConfigService netcfgService = get(NetworkConfigService.class); |
| 71 | |
| 72 | DeviceId did = DeviceId.deviceId(device); |
| 73 | |
| 74 | ConnectPoint cp = Optional.ofNullable(port) |
| 75 | .map(PortNumber::fromString) |
| 76 | .map(pn -> new ConnectPoint(did, pn)) |
| 77 | .orElse(null); |
| 78 | |
| 79 | if (cp == null) { |
| 80 | // device config |
| 81 | if (!remove) { |
| 82 | if (deviceService.getDevice(did) == null) { |
| 83 | print("[WARN] configuring about unknown device %s", did); |
| 84 | } |
| 85 | LinkDiscoveryFromDevice cfg; |
| 86 | cfg = netcfgService.addConfig(did, LinkDiscoveryFromDevice.class); |
| 87 | cfg.enabled(enable); |
| 88 | cfg.apply(); |
| 89 | } else { |
| 90 | netcfgService.removeConfig(did, LinkDiscoveryFromDevice.class); |
| 91 | } |
| 92 | } else { |
| 93 | // port config |
| 94 | if (!remove) { |
| 95 | if (deviceService.getPort(cp) == null) { |
| 96 | print("[WARN] configuring about unknown port %s", cp); |
| 97 | } |
| 98 | |
| 99 | LinkDiscoveryFromPort cfg; |
| 100 | cfg = netcfgService.addConfig(cp, LinkDiscoveryFromPort.class); |
| 101 | cfg.enabled(enable); |
| 102 | cfg.apply(); |
| 103 | } else { |
| 104 | netcfgService.removeConfig(cp, LinkDiscoveryFromPort.class); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } |