blob: f0980ce7074c77b7d4f94701317b7e05c77531ce [file] [log] [blame]
Thomas Vachuska8ceee942015-04-14 16:53:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska8ceee942015-04-14 16:53:57 -07003 *
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070025import org.onosproject.net.PortNumber;
Thomas Vachuska8ceee942015-04-14 16:53:57 -070026import org.onosproject.net.device.DefaultDeviceDescription;
27import org.onosproject.net.device.DeviceDescription;
28import org.onosproject.net.device.DeviceProvider;
29import org.onosproject.net.device.DeviceProviderRegistry;
30import org.onosproject.net.device.DeviceProviderService;
31import org.onosproject.net.device.DeviceService;
32import org.onosproject.net.provider.AbstractProvider;
33import 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")
40public 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(),
82 device.chassisId(), builder.build());
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 Dasa2d37502016-03-25 17:50:40 -0700104
105 @Override
106 public void changePortState(DeviceId deviceId, PortNumber portNumber,
107 boolean enable) {
108 }
Thomas Vachuska8ceee942015-04-14 16:53:57 -0700109 }
110}