blob: be310e8a99e949cd322b38377d6f13f4f1b1f005 [file] [log] [blame]
Yuta HIGUCHI31207782017-02-17 14:43:40 -08001/*
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 */
16package org.onosproject.cli.net;
17
18import static org.onosproject.net.LinkKey.linkKey;
19
20import java.util.Optional;
21
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.apache.karaf.shell.commands.Option;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Link;
28import org.onosproject.net.LinkKey;
29import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.basics.BasicLinkConfig;
31import org.onosproject.net.device.DeviceService;
32
33/**
34 * Add Link configuration.
35 */
36@Command(scope = "onos", name = "config-link",
37 description = "Configure link.")
38public class ConfigureLinkCommand extends AbstractShellCommand {
39
40 @Argument(index = 0, name = "src", description = "src port",
41 required = true, multiValued = false)
42 String src = null;
43
44 @Argument(index = 1, name = "dst", description = "dst port",
45 required = true, multiValued = false)
46 String dst = null;
47
48 @Option(name = "--type",
49 description = "specify link type",
50 valueToShowInHelp = "DIRECT")
51 String type = Link.Type.DIRECT.name();
52
53 // TODO add metric, latency, durable
54
55 @Option(name = "--bandwidth",
56 description = "bandwidth in Mbps (integer)")
57 String bandwidth = null;
58
59 @Option(name = "--disallow",
60 description = "disallow link")
61 boolean disallow = false;
62
63 @Option(name = "--remove-config",
64 description = "remove link configuration")
65 boolean remove = false;
66
67
68 @Override
69 protected void execute() {
70 DeviceService deviceService = get(DeviceService.class);
71 NetworkConfigService netCfgService = get(NetworkConfigService.class);
72
73 ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(src);
74 if (deviceService.getPort(srcCp) == null) {
75 print("[ERROR] %s does not exist", srcCp);
76 return;
77 }
78
79 ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(dst);
80 if (deviceService.getPort(dstCp) == null) {
81 print("[ERROR] %s does not exist", dstCp);
82 return;
83 }
84
85 LinkKey link = linkKey(srcCp, dstCp);
86 if (remove) {
87 netCfgService.removeConfig(link, BasicLinkConfig.class);
88 return;
89 }
90
91 Long bw = Optional.ofNullable(bandwidth)
92 .map(Long::valueOf)
93 .orElse(null);
94
95 Link.Type linkType = Link.Type.valueOf(type);
96
97 BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class);
98 cfg.isAllowed(!disallow);
99 cfg.type(linkType);
100 if (bw != null) {
101 cfg.bandwidth(bw);
102 }
103
104 cfg.apply();
105 }
106
107}