Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 25 | import org.onosproject.net.PortNumber; |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 26 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 27 | import org.onosproject.net.device.DeviceDescription; |
| 28 | import org.onosproject.net.device.DeviceProvider; |
| 29 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 30 | import org.onosproject.net.device.DeviceProviderService; |
| 31 | import org.onosproject.net.device.DeviceService; |
| 32 | import org.onosproject.net.provider.AbstractProvider; |
| 33 | import org.onosproject.net.provider.ProviderId; |
| 34 | |
| 35 | /** |
| 36 | * Annotates network device model. |
| 37 | */ |
| 38 | @Command(scope = "onos", name = "annotate-device", |
| 39 | description = "Annotates network model entities") |
| 40 | public class AnnotateDeviceCommand extends AbstractShellCommand { |
| 41 | |
| 42 | static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true); |
| 43 | |
| 44 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 45 | required = true, multiValued = false) |
| 46 | String uri = null; |
| 47 | |
| 48 | @Argument(index = 1, name = "key", description = "Annotation key", |
| 49 | required = true, multiValued = false) |
| 50 | String key = null; |
| 51 | |
| 52 | @Argument(index = 2, name = "value", |
| 53 | description = "Annotation value (null to remove)", |
| 54 | required = false, multiValued = false) |
| 55 | String value = null; |
| 56 | |
| 57 | @Override |
| 58 | protected void execute() { |
| 59 | DeviceService service = get(DeviceService.class); |
| 60 | Device device = service.getDevice(DeviceId.deviceId(uri)); |
| 61 | |
| 62 | DeviceProviderRegistry registry = get(DeviceProviderRegistry.class); |
| 63 | DeviceProvider provider = new AnnotationProvider(); |
| 64 | try { |
| 65 | DeviceProviderService providerService = registry.register(provider); |
| 66 | providerService.deviceConnected(device.id(), description(device, key, value)); |
| 67 | } finally { |
| 68 | registry.unregister(provider); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private DeviceDescription description(Device device, String key, String value) { |
| 73 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 74 | if (value != null) { |
| 75 | builder.set(key, value); |
| 76 | } else { |
| 77 | builder.remove(key); |
| 78 | } |
| 79 | return new DefaultDeviceDescription(device.id().uri(), device.type(), |
| 80 | device.manufacturer(), device.hwVersion(), |
| 81 | device.swVersion(), device.serialNumber(), |
Yuta HIGUCHI | 385db9a | 2016-07-11 21:15:34 -0700 | [diff] [blame] | 82 | device.chassisId(), false, builder.build()); |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Token provider entity |
| 86 | private static final class AnnotationProvider |
| 87 | extends AbstractProvider implements DeviceProvider { |
| 88 | private AnnotationProvider() { |
| 89 | super(PID); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void triggerProbe(DeviceId deviceId) { |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean isReachable(DeviceId deviceId) { |
| 102 | return false; |
| 103 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 104 | |
| 105 | @Override |
| 106 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 107 | boolean enable) { |
| 108 | } |
Thomas Vachuska | 8ceee94 | 2015-04-14 16:53:57 -0700 | [diff] [blame] | 109 | } |
| 110 | } |