blob: 1fee3d84b94e1515b1f8d8c39123d1ebbf3ec196 [file] [log] [blame]
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -08003 *
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 */
16package org.onosproject.provider.lldp.cli;
17
18import java.util.Optional;
19
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.net.DeviceIdCompleter;
25import org.onosproject.cli.net.PortNumberCompleter;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.provider.lldp.impl.LinkDiscoveryFromDevice;
32import 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")
39public 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 Milkey86ad7bb2018-09-27 12:32:28 -070068 protected void doExecute() {
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -080069 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}