blob: bab4f167128e1f4954e2a0483f2bffe038da9495 [file] [log] [blame]
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -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.cli.net;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080023import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.onosproject.cli.net.completer.AnnotationKeysCompleter;
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.net.config.basics.PortAnnotationConfig;
28import org.onosproject.net.device.DeviceService;
29
30/**
31 * Annotates network device port model.
32 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Service
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080034@Command(scope = "onos", name = "annotate-port",
35 description = "Annotates port entities")
36public class AnnotatePortCommand extends AbstractShellCommand {
37
38 @Argument(index = 0, name = "port", description = "Device Port",
39 required = true)
Ray Milkey0068fd02018-10-11 15:45:39 -070040 @Completion(ConnectPointCompleter.class)
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080041 String port = null;
42
43 @Argument(index = 1, name = "key", description = "Annotation key",
44 required = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070045 @Completion(AnnotationKeysCompleter.class)
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080046 String key = null;
47
48 @Argument(index = 2, name = "value",
49 description = "Annotation value (null to remove)",
50 required = false)
51 String value = null;
52
53 @Option(name = "--remove-config",
54 description = "Remove annotation config")
55 private boolean removeCfg = false;
56
57 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 protected void doExecute() {
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080059 DeviceService deviceService = get(DeviceService.class);
60 NetworkConfigService netcfgService = get(NetworkConfigService.class);
61
62
63 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(port);
64 if (deviceService.getPort(connectPoint) == null) {
65 print("Port %s does not exist.", port);
66 return;
67 }
68
69 if (removeCfg && key == null) {
70 // remove whole port annotation config
71 netcfgService.removeConfig(connectPoint, PortAnnotationConfig.class);
72 print("Annotation Config about %s removed", connectPoint);
73 return;
74 }
75
76 if (key == null) {
77 print("[ERROR] Annotation key not specified.");
78 return;
79 }
80
81 PortAnnotationConfig cfg = netcfgService.addConfig(connectPoint, PortAnnotationConfig.class);
82 if (removeCfg) {
83 // remove config about entry
84 cfg.annotation(key);
85 } else {
86 // add remove request config
87 cfg.annotation(key, value);
88 }
89 cfg.apply();
90 }
91
92}