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