blob: 89b2e48feeb4396cae8119982e1691a1653f78c8 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
26import org.apache.karaf.shell.api.action.Option;
Yuta HIGUCHI31207782017-02-17 14:43:40 -080027import org.onosproject.cli.AbstractShellCommand;
Ray Milkey0068fd02018-10-11 15:45:39 -070028import org.onosproject.cli.net.completer.PeerConnectPointCompleter;
Yuta HIGUCHI31207782017-02-17 14:43:40 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Link;
31import org.onosproject.net.LinkKey;
32import org.onosproject.net.config.NetworkConfigService;
33import org.onosproject.net.config.basics.BasicLinkConfig;
34import org.onosproject.net.device.DeviceService;
35
36/**
37 * Add Link configuration.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Yuta HIGUCHI31207782017-02-17 14:43:40 -080040@Command(scope = "onos", name = "config-link",
41 description = "Configure link.")
42public class ConfigureLinkCommand extends AbstractShellCommand {
43
44 @Argument(index = 0, name = "src", description = "src port",
45 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070046 @Completion(ConnectPointCompleter.class)
Yuta HIGUCHI31207782017-02-17 14:43:40 -080047 String src = null;
48
49 @Argument(index = 1, name = "dst", description = "dst port",
50 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070051 @Completion(PeerConnectPointCompleter.class)
Yuta HIGUCHI31207782017-02-17 14:43:40 -080052 String dst = null;
53
54 @Option(name = "--type",
55 description = "specify link type",
56 valueToShowInHelp = "DIRECT")
57 String type = Link.Type.DIRECT.name();
58
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -070059
60 @Option(name = "--uni-directional",
61 description = "specify that link is uni-directional")
62 boolean isUniDi = false;
63
Yuta HIGUCHI31207782017-02-17 14:43:40 -080064 // TODO add metric, latency, durable
65
66 @Option(name = "--bandwidth",
67 description = "bandwidth in Mbps (integer)")
68 String bandwidth = null;
69
70 @Option(name = "--disallow",
71 description = "disallow link")
72 boolean disallow = false;
73
74 @Option(name = "--remove-config",
75 description = "remove link configuration")
76 boolean remove = false;
77
78
79 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 protected void doExecute() {
Yuta HIGUCHI31207782017-02-17 14:43:40 -080081 DeviceService deviceService = get(DeviceService.class);
82 NetworkConfigService netCfgService = get(NetworkConfigService.class);
83
84 ConnectPoint srcCp = ConnectPoint.deviceConnectPoint(src);
85 if (deviceService.getPort(srcCp) == null) {
86 print("[ERROR] %s does not exist", srcCp);
87 return;
88 }
89
90 ConnectPoint dstCp = ConnectPoint.deviceConnectPoint(dst);
91 if (deviceService.getPort(dstCp) == null) {
92 print("[ERROR] %s does not exist", dstCp);
93 return;
94 }
95
96 LinkKey link = linkKey(srcCp, dstCp);
97 if (remove) {
98 netCfgService.removeConfig(link, BasicLinkConfig.class);
99 return;
100 }
101
102 Long bw = Optional.ofNullable(bandwidth)
103 .map(Long::valueOf)
104 .orElse(null);
105
106 Link.Type linkType = Link.Type.valueOf(type);
107
108 BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class);
109 cfg.isAllowed(!disallow);
Yuta HIGUCHI537e47c2017-03-14 20:16:00 -0700110 cfg.isBidirectional(!isUniDi);
Yuta HIGUCHI31207782017-02-17 14:43:40 -0800111 cfg.type(linkType);
112 if (bw != null) {
113 cfg.bandwidth(bw);
114 }
115
116 cfg.apply();
117 }
118
119}