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; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.net.completer.AnnotationKeysCompleter; |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.PortAnnotationConfig; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | |
| 30 | /** |
| 31 | * Annotates network device port model. |
| 32 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | @Service |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 34 | @Command(scope = "onos", name = "annotate-port", |
| 35 | description = "Annotates port entities") |
| 36 | public class AnnotatePortCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Argument(index = 0, name = "port", description = "Device Port", |
| 39 | required = true) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 40 | @Completion(ConnectPointCompleter.class) |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 41 | String port = null; |
| 42 | |
| 43 | @Argument(index = 1, name = "key", description = "Annotation key", |
| 44 | required = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 45 | @Completion(AnnotationKeysCompleter.class) |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 46 | 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | protected void doExecute() { |
Yuta HIGUCHI | 7438f5a | 2017-02-15 22:09:46 -0800 | [diff] [blame] | 59 | 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 | } |