blob: 6af7a26f4f67747686e952ea360c1c23bd2091bf [file] [log] [blame]
Yuta HIGUCHI31207782017-02-17 14:43:40 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHI31207782017-02-17 14:43:40 -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 org.onosproject.net.LinkKey.linkKey;
19
20import java.util.Optional;
21
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI31207782017-02-17 14:43:40 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Link;
29import org.onosproject.net.LinkKey;
30import org.onosproject.net.config.NetworkConfigService;
31import org.onosproject.net.config.basics.BasicLinkConfig;
32import org.onosproject.net.device.DeviceService;
33
34/**
35 * Add Link configuration.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Yuta HIGUCHI31207782017-02-17 14:43:40 -080038@Command(scope = "onos", name = "config-link",
39 description = "Configure link.")
40public class ConfigureLinkCommand extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "src", description = "src port",
43 required = true, multiValued = false)
44 String src = null;
45
46 @Argument(index = 1, name = "dst", description = "dst port",
47 required = true, multiValued = false)
48 String dst = null;
49
50 @Option(name = "--type",
51 description = "specify link type",
52 valueToShowInHelp = "DIRECT")
53 String type = Link.Type.DIRECT.name();
54
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -070055
56 @Option(name = "--uni-directional",
57 description = "specify that link is uni-directional")
58 boolean isUniDi = false;
59
Yuta HIGUCHI31207782017-02-17 14:43:40 -080060 // TODO add metric, latency, durable
61
62 @Option(name = "--bandwidth",
63 description = "bandwidth in Mbps (integer)")
64 String bandwidth = null;
65
66 @Option(name = "--disallow",
67 description = "disallow link")
68 boolean disallow = false;
69
70 @Option(name = "--remove-config",
71 description = "remove link configuration")
72 boolean remove = false;
73
74
75 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 protected void doExecute() {
Yuta HIGUCHI31207782017-02-17 14:43:40 -080077 DeviceService deviceService = get(DeviceService.class);
78 NetworkConfigService netCfgService = get(NetworkConfigService.class);
79
80 ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(src);
81 if (deviceService.getPort(srcCp) == null) {
82 print("[ERROR] %s does not exist", srcCp);
83 return;
84 }
85
86 ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(dst);
87 if (deviceService.getPort(dstCp) == null) {
88 print("[ERROR] %s does not exist", dstCp);
89 return;
90 }
91
92 LinkKey link = linkKey(srcCp, dstCp);
93 if (remove) {
94 netCfgService.removeConfig(link, BasicLinkConfig.class);
95 return;
96 }
97
98 Long bw = Optional.ofNullable(bandwidth)
99 .map(Long::valueOf)
100 .orElse(null);
101
102 Link.Type linkType = Link.Type.valueOf(type);
103
104 BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class);
105 cfg.isAllowed(!disallow);
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -0700106 cfg.isBidirectional(!isUniDi);
Yuta HIGUCHI31207782017-02-17 14:43:40 -0800107 cfg.type(linkType);
108 if (bw != null) {
109 cfg.bandwidth(bw);
110 }
111
112 cfg.apply();
113 }
114
115}