blob: 1d35e37a994c9e3205f1168e0ec7d478fe07b084 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070023import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080026import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070027import org.onosproject.cli.net.completer.AnnotationKeysCompleter;
28import org.onosproject.cli.net.completer.PeerConnectPointCompleter;
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DefaultAnnotations;
31import org.onosproject.net.Link;
32import org.onosproject.net.link.DefaultLinkDescription;
33import org.onosproject.net.link.LinkDescription;
34import org.onosproject.net.link.LinkProvider;
35import org.onosproject.net.link.LinkProviderRegistry;
36import org.onosproject.net.link.LinkProviderService;
37import org.onosproject.net.link.LinkService;
38import org.onosproject.net.provider.ProviderId;
39
40/**
41 * Annotates network link model.
42 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043@Service
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080044@Command(scope = "onos", name = "annotate-link",
45 description = "Annotates network model entities")
46public class AnnotateLinkCommand extends AbstractShellCommand {
47
alessio7dbb22d2019-07-25 11:43:43 +020048 static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli");
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080049
50 @Option(name = "--both",
51 description = "Add to both direction")
52 private boolean both = false;
53
54 @Argument(index = 0, name = "srcConnectPoint", description = "source Connect Point",
55 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070056 @Completion(ConnectPointCompleter.class)
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080057 private String srcCp = null;
58
59 @Argument(index = 1, name = "dstConnectPoint", description = "destination Connect Point",
60 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070061 @Completion(PeerConnectPointCompleter.class)
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080062 private String dstCp = null;
63
64
65
66 @Argument(index = 2, name = "key", description = "Annotation key",
67 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070068 @Completion(AnnotationKeysCompleter.class)
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080069 private String key = null;
70
71 @Argument(index = 3, name = "value",
72 description = "Annotation value (null to remove)",
73 required = false, multiValued = false)
74 private String value = null;
75
76
77 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 protected void doExecute() {
Yuta HIGUCHI5f3c0332017-01-09 17:41:07 -080079 LinkService service = get(LinkService.class);
80 ConnectPoint src = deviceConnectPoint(srcCp);
81 ConnectPoint dst = deviceConnectPoint(dstCp);
82
83 LinkProviderRegistry registry = get(LinkProviderRegistry.class);
84 CliLinkProvider provider = new CliLinkProvider();
85 LinkProviderService providerService = registry.register(provider);
86 try {
87 providerService.linkDetected(description(service.getLink(src, dst),
88 key, value));
89 if (both) {
90 providerService.linkDetected(description(service.getLink(dst, src),
91 key, value));
92 }
93 } finally {
94 registry.unregister(provider);
95 }
96 }
97
98
99 private LinkDescription description(Link link, String key, String value) {
100 checkNotNull(key, "Key cannot be null");
101 DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
102 if (value != null) {
103 builder.set(key, value);
104 } else {
105 builder.remove(key);
106 }
107 return new DefaultLinkDescription(link.src(),
108 link.dst(),
109 link.type(),
110 link.isExpected(),
111 builder.build());
112 }
113
114 private static final class CliLinkProvider implements LinkProvider {
115 @Override
116 public ProviderId id() {
117 return PID;
118 }
119 }
120
121}