Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 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 org.onosproject.net.LinkKey.linkKey; |
| 19 | |
| 20 | import java.util.Optional; |
| 21 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 26 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 27 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 28 | import org.onosproject.cli.net.completer.PeerConnectPointCompleter; |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.LinkKey; |
| 32 | import org.onosproject.net.config.NetworkConfigService; |
| 33 | import org.onosproject.net.config.basics.BasicLinkConfig; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | |
| 36 | /** |
| 37 | * Add Link configuration. |
| 38 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Service |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 40 | @Command(scope = "onos", name = "config-link", |
| 41 | description = "Configure link.") |
| 42 | public class ConfigureLinkCommand extends AbstractShellCommand { |
| 43 | |
| 44 | @Argument(index = 0, name = "src", description = "src port", |
| 45 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 46 | @Completion(ConnectPointCompleter.class) |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 47 | String src = null; |
| 48 | |
| 49 | @Argument(index = 1, name = "dst", description = "dst port", |
| 50 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 51 | @Completion(PeerConnectPointCompleter.class) |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 52 | 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 HIGUCHI | 537e47c | 2017-03-14 20:16:00 -0700 | [diff] [blame] | 59 | |
| 60 | @Option(name = "--uni-directional", |
| 61 | description = "specify that link is uni-directional") |
| 62 | boolean isUniDi = false; |
| 63 | |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 64 | // 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 80 | protected void doExecute() { |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 81 | 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 HIGUCHI | 537e47c | 2017-03-14 20:16:00 -0700 | [diff] [blame] | 110 | cfg.isBidirectional(!isUniDi); |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 111 | cfg.type(linkType); |
| 112 | if (bw != null) { |
| 113 | cfg.bandwidth(bw); |
| 114 | } |
| 115 | |
| 116 | cfg.apply(); |
| 117 | } |
| 118 | |
| 119 | } |