Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [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 static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onosproject.net.ConnectPoint.deviceConnectPoint; |
| 20 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.net.completer.AnnotationKeysCompleter; |
| 28 | import org.onosproject.cli.net.completer.PeerConnectPointCompleter; |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.Link; |
| 32 | import org.onosproject.net.link.DefaultLinkDescription; |
| 33 | import org.onosproject.net.link.LinkDescription; |
| 34 | import org.onosproject.net.link.LinkProvider; |
| 35 | import org.onosproject.net.link.LinkProviderRegistry; |
| 36 | import org.onosproject.net.link.LinkProviderService; |
| 37 | import org.onosproject.net.link.LinkService; |
| 38 | import org.onosproject.net.provider.ProviderId; |
| 39 | |
| 40 | /** |
| 41 | * Annotates network link model. |
| 42 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 43 | @Service |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 44 | @Command(scope = "onos", name = "annotate-link", |
| 45 | description = "Annotates network model entities") |
| 46 | public class AnnotateLinkCommand extends AbstractShellCommand { |
| 47 | |
| 48 | static final ProviderId PID = new ProviderId("cli", "org.onosproject.cli", true); |
| 49 | |
| 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 56 | @Completion(ConnectPointCompleter.class) |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 57 | private String srcCp = null; |
| 58 | |
| 59 | @Argument(index = 1, name = "dstConnectPoint", description = "destination Connect Point", |
| 60 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 61 | @Completion(PeerConnectPointCompleter.class) |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 62 | private String dstCp = null; |
| 63 | |
| 64 | |
| 65 | |
| 66 | @Argument(index = 2, name = "key", description = "Annotation key", |
| 67 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 68 | @Completion(AnnotationKeysCompleter.class) |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 69 | 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | protected void doExecute() { |
Yuta HIGUCHI | 5f3c033 | 2017-01-09 17:41:07 -0800 | [diff] [blame] | 79 | 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 | } |