blob: 6550bdcd7e401b322d1f044c94e11ec0cdc678b6 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.config.NetworkConfigService;
24import org.onosproject.net.config.basics.PortAnnotationConfig;
25import org.onosproject.net.device.DeviceService;
26
27/**
28 * Annotates network device port model.
29 */
30@Command(scope = "onos", name = "annotate-port",
31 description = "Annotates port entities")
32public class AnnotatePortCommand extends AbstractShellCommand {
33
34 @Argument(index = 0, name = "port", description = "Device Port",
35 required = true)
36 String port = null;
37
38 @Argument(index = 1, name = "key", description = "Annotation key",
39 required = false)
40 String key = null;
41
42 @Argument(index = 2, name = "value",
43 description = "Annotation value (null to remove)",
44 required = false)
45 String value = null;
46
47 @Option(name = "--remove-config",
48 description = "Remove annotation config")
49 private boolean removeCfg = false;
50
51 @Override
52 protected void execute() {
53 DeviceService deviceService = get(DeviceService.class);
54 NetworkConfigService netcfgService = get(NetworkConfigService.class);
55
56
57 ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(port);
58 if (deviceService.getPort(connectPoint) == null) {
59 print("Port %s does not exist.", port);
60 return;
61 }
62
63 if (removeCfg && key == null) {
64 // remove whole port annotation config
65 netcfgService.removeConfig(connectPoint, PortAnnotationConfig.class);
66 print("Annotation Config about %s removed", connectPoint);
67 return;
68 }
69
70 if (key == null) {
71 print("[ERROR] Annotation key not specified.");
72 return;
73 }
74
75 PortAnnotationConfig cfg = netcfgService.addConfig(connectPoint, PortAnnotationConfig.class);
76 if (removeCfg) {
77 // remove config about entry
78 cfg.annotation(key);
79 } else {
80 // add remove request config
81 cfg.annotation(key, value);
82 }
83 cfg.apply();
84 }
85
86}