Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.config.NetworkConfigService; |
| 25 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | |
| 28 | /** |
| 29 | * Annotates network device port model. |
| 30 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | @Service |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 32 | @Command(scope = "onos", name = "annotate-port", |
| 33 | description = "Annotates port entities") |
| 34 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 55 | 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 | } |