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