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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 23 | import org.onosproject.net.config.NetworkConfigService; |
| 24 | import org.onosproject.net.config.basics.DeviceAnnotationConfig; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 25 | import org.onosproject.net.provider.ProviderId; |
| 26 | |
| 27 | /** |
| 28 | * Annotates network device model. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "annotate-device", |
| 31 | description = "Annotates network model entities") |
| 32 | public class AnnotateDeviceCommand extends AbstractShellCommand { |
| 33 | |
| 34 | static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true); |
| 35 | |
| 36 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 37 | required = true, multiValued = false) |
| 38 | String uri = null; |
| 39 | |
| 40 | @Argument(index = 1, name = "key", description = "Annotation key", |
| 41 | required = true, multiValued = false) |
| 42 | String key = null; |
| 43 | |
| 44 | @Argument(index = 2, name = "value", |
| 45 | description = "Annotation value (null to remove)", |
| 46 | required = false, multiValued = false) |
| 47 | String value = null; |
| 48 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 49 | @Option(name = "--remove-config", |
| 50 | description = "Remove annotation config") |
| 51 | private boolean removeCfg = false; |
| 52 | |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 53 | @Override |
| 54 | protected void execute() { |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 55 | NetworkConfigService netcfgService = get(NetworkConfigService.class); |
| 56 | DeviceId deviceId = DeviceId.deviceId(uri); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 57 | |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 58 | if (key == null) { |
| 59 | print("[ERROR] Annotation key not specified."); |
| 60 | return; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 61 | } |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 62 | DeviceAnnotationConfig cfg = netcfgService.getConfig(deviceId, DeviceAnnotationConfig.class); |
| 63 | if (cfg == null) { |
| 64 | cfg = new DeviceAnnotationConfig(deviceId); |
| 65 | } |
| 66 | if (removeCfg) { |
| 67 | // remove config about entry |
| 68 | cfg.annotation(key); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 69 | } else { |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 70 | // add remove request config |
| 71 | cfg.annotation(key, value); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 72 | } |
Palash Kala | a06a616 | 2017-11-15 20:42:40 +0900 | [diff] [blame] | 73 | netcfgService.applyConfig(deviceId, DeviceAnnotationConfig.class, cfg.node()); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 74 | } |
| 75 | } |