blob: b20546f5de9141f70f9ade58d5d5509c85cf5224 [file] [log] [blame]
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -08003 *
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 static com.google.common.base.Preconditions.checkNotNull;
19import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
20
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.Link;
29import org.onosproject.net.link.DefaultLinkDescription;
30import org.onosproject.net.link.LinkDescription;
31import org.onosproject.net.link.LinkProvider;
32import org.onosproject.net.link.LinkProviderRegistry;
33import org.onosproject.net.link.LinkProviderService;
34import org.onosproject.net.link.LinkService;
35import org.onosproject.net.provider.ProviderId;
36
37/**
38 * Annotates network link model.
39 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080041@Command(scope = "onos", name = "annotate-link",
42 description = "Annotates network model entities")
43public class AnnotateLinkCommand extends AbstractShellCommand {
44
45 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true);
46
47 @Option(name = "--both",
48 description = "Add to both direction")
49 private boolean both = false;
50
51 @Argument(index = 0, name = "srcConnectPoint", description = "source Connect Point",
52 required = true, multiValued = false)
53 private String srcCp = null;
54
55 @Argument(index = 1, name = "dstConnectPoint", description = "destination Connect Point",
56 required = true, multiValued = false)
57 private String dstCp = null;
58
59
60
61 @Argument(index = 2, name = "key", description = "Annotation key",
62 required = true, multiValued = false)
63 private String key = null;
64
65 @Argument(index = 3, name = "value",
66 description = "Annotation value (null to remove)",
67 required = false, multiValued = false)
68 private String value = null;
69
70
71 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 protected void doExecute() {
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080073 LinkService service = get(LinkService.class);
74 ConnectPoint src = deviceConnectPoint(srcCp);
75 ConnectPoint dst = deviceConnectPoint(dstCp);
76
77 LinkProviderRegistry registry = get(LinkProviderRegistry.class);
78 CliLinkProvider provider = new CliLinkProvider();
79 LinkProviderService providerService = registry.register(provider);
80 try {
81 providerService.linkDetected(description(service.getLink(src, dst),
82 key, value));
83 if (both) {
84 providerService.linkDetected(description(service.getLink(dst, src),
85 key, value));
86 }
87 } finally {
88 registry.unregister(provider);
89 }
90 }
91
92
93 private LinkDescription description(Link link, String key, String value) {
94 checkNotNull(key, "Key cannot be null");
95 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
96 if (value != null) {
97 builder.set(key, value);
98 } else {
99 builder.remove(key);
100 }
101 return new DefaultLinkDescription(link.src(),
102 link.dst(),
103 link.type(),
104 link.isExpected(),
105 builder.build());
106 }
107
108 private static final class CliLinkProvider implements LinkProvider {
109 @Override
110 public ProviderId id() {
111 return PID;
112 }
113 }
114
115}