blob: 394a4c9da2f692c9bec4183cf78fa07072b7f6d0 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.cli.net.DeviceIdCompleter;
26import org.onosproject.cli.net.PortNumberCompleter;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.config.NetworkConfigService;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.provider.lldp.impl.LinkDiscoveryFromDevice;
33import org.onosproject.provider.lldp.impl.LinkDiscoveryFromPort;
34
35/**
36 *
37 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070038@Service
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -080039@Command(scope = "onos", name = "config-link-discovery",
40 description = "Adds configuration to disable LLDP link discovery")
41public class ConfigLinkDiscoveryCommand extends AbstractShellCommand {
42
43 // OSGi workaround to introduce package dependency
44 DeviceIdCompleter deviceIdCompleter;
45 @Argument(index = 0, name = "device",
46 description = "DeviceID",
47 required = true)
48 String device = null;
49
50
51 // OSGi workaround to introduce package dependency
52 PortNumberCompleter portNumberCompleter;
53 @Argument(index = 1, name = "port",
54 description = "Port number",
55 required = false)
56 String port = null;
57
58 @Option(name = "--remove", aliases = "-r",
59 description = "remove configuration",
60 required = false)
61 boolean remove = false;
62
63 @Option(name = "--enable",
64 description = "add configuration to enable LinkDiscovery",
65 required = false)
66 boolean enable = false;
67
68
69 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070070 protected void doExecute() {
Yuta HIGUCHIea7efa42017-02-16 20:34:25 -080071 DeviceService deviceService = get(DeviceService.class);
72 NetworkConfigService netcfgService = get(NetworkConfigService.class);
73
74 DeviceId did = DeviceId.deviceId(device);
75
76 ConnectPoint cp = Optional.ofNullable(port)
77 .map(PortNumber::fromString)
78 .map(pn -> new ConnectPoint(did, pn))
79 .orElse(null);
80
81 if (cp == null) {
82 // device config
83 if (!remove) {
84 if (deviceService.getDevice(did) == null) {
85 print("[WARN] configuring about unknown device %s", did);
86 }
87 LinkDiscoveryFromDevice cfg;
88 cfg = netcfgService.addConfig(did, LinkDiscoveryFromDevice.class);
89 cfg.enabled(enable);
90 cfg.apply();
91 } else {
92 netcfgService.removeConfig(did, LinkDiscoveryFromDevice.class);
93 }
94 } else {
95 // port config
96 if (!remove) {
97 if (deviceService.getPort(cp) == null) {
98 print("[WARN] configuring about unknown port %s", cp);
99 }
100
101 LinkDiscoveryFromPort cfg;
102 cfg = netcfgService.addConfig(cp, LinkDiscoveryFromPort.class);
103 cfg.enabled(enable);
104 cfg.apply();
105 } else {
106 netcfgService.removeConfig(cp, LinkDiscoveryFromPort.class);
107 }
108 }
109 }
110
111}