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