blob: cff1005871d6b7cb5fab0862713e1022ddacbbf1 [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
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -070053
54 @Option(name = "--uni-directional",
55 description = "specify that link is uni-directional")
56 boolean isUniDi = false;
57
Yuta HIGUCHI31207782017-02-17 14:43:40 -080058 // TODO add metric, latency, durable
59
60 @Option(name = "--bandwidth",
61 description = "bandwidth in Mbps (integer)")
62 String bandwidth = null;
63
64 @Option(name = "--disallow",
65 description = "disallow link")
66 boolean disallow = false;
67
68 @Option(name = "--remove-config",
69 description = "remove link configuration")
70 boolean remove = false;
71
72
73 @Override
74 protected void execute() {
75 DeviceService deviceService = get(DeviceService.class);
76 NetworkConfigService netCfgService = get(NetworkConfigService.class);
77
78 ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(src);
79 if (deviceService.getPort(srcCp) == null) {
80 print("[ERROR] %s does not exist", srcCp);
81 return;
82 }
83
84 ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(dst);
85 if (deviceService.getPort(dstCp) == null) {
86 print("[ERROR] %s does not exist", dstCp);
87 return;
88 }
89
90 LinkKey link = linkKey(srcCp, dstCp);
91 if (remove) {
92 netCfgService.removeConfig(link, BasicLinkConfig.class);
93 return;
94 }
95
96 Long bw = Optional.ofNullable(bandwidth)
97 .map(Long::valueOf)
98 .orElse(null);
99
100 Link.Type linkType = Link.Type.valueOf(type);
101
102 BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class);
103 cfg.isAllowed(!disallow);
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -0700104 cfg.isBidirectional(!isUniDi);
Yuta HIGUCHI31207782017-02-17 14:43:40 -0800105 cfg.type(linkType);
106 if (bw != null) {
107 cfg.bandwidth(bw);
108 }
109
110 cfg.apply();
111 }
112
113}