Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [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; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [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; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.DeviceAnnotationConfig; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 28 | import org.onosproject.net.provider.ProviderId; |
| 29 | |
| 30 | /** |
| 31 | * Annotates network device model. |
| 32 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | @Service |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 34 | @Command(scope = "onos", name = "annotate-device", |
| 35 | description = "Annotates network model entities") |
| 36 | public class AnnotateDeviceCommand extends AbstractShellCommand { |
| 37 | |
alessio | 7dbb22d | 2019-07-25 11:43:43 +0200 | [diff] [blame] | 38 | static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli"); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 39 | |
| 40 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 41 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 42 | @Completion(DeviceIdCompleter.class) |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 43 | String uri = null; |
| 44 | |
| 45 | @Argument(index = 1, name = "key", description = "Annotation key", |
| 46 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 47 | @Completion(AnnotationKeysCompleter.class) |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 48 | String key = null; |
| 49 | |
| 50 | @Argument(index = 2, name = "value", |
| 51 | description = "Annotation value (null to remove)", |
| 52 | required = false, multiValued = false) |
| 53 | String value = null; |
| 54 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 55 | @Option(name = "--remove-config", |
| 56 | description = "Remove annotation config") |
| 57 | private boolean removeCfg = false; |
| 58 | |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 59 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 60 | protected void doExecute() { |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 61 | NetworkConfigService netcfgService = get(NetworkConfigService.class); |
| 62 | DeviceId deviceId = DeviceId.deviceId(uri); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 63 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 64 | if (key == null) { |
| 65 | print("[ERROR] Annotation key not specified."); |
| 66 | return; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 67 | } |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 68 | DeviceAnnotationConfig cfg = netcfgService.getConfig(deviceId, DeviceAnnotationConfig.class); |
| 69 | if (cfg == null) { |
| 70 | cfg = new DeviceAnnotationConfig(deviceId); |
| 71 | } |
| 72 | if (removeCfg) { |
| 73 | // remove config about entry |
| 74 | cfg.annotation(key); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 75 | } else { |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 76 | // add remove request config |
| 77 | cfg.annotation(key, value); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 78 | } |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 79 | netcfgService.applyConfig(deviceId, DeviceAnnotationConfig.class, cfg.node()); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 80 | } |
| 81 | } |