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; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.LinkKey; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.config.basics.BasicLinkConfig; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | |
| 34 | /** |
| 35 | * Add Link configuration. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 38 | @Command(scope = "onos", name = "config-link", |
| 39 | description = "Configure link.") |
| 40 | public 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 HIGUCHI | 537e47c | 2017-03-14 20:16:00 -0700 | [diff] [blame] | 55 | |
| 56 | @Option(name = "--uni-directional", |
| 57 | description = "specify that link is uni-directional") |
| 58 | boolean isUniDi = false; |
| 59 | |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 60 | // 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | protected void doExecute() { |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 77 | 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 HIGUCHI | 537e47c | 2017-03-14 20:16:00 -0700 | [diff] [blame] | 106 | cfg.isBidirectional(!isUniDi); |
Yuta HIGUCHI | 3120778 | 2017-02-17 14:43:40 -0800 | [diff] [blame] | 107 | cfg.type(linkType); |
| 108 | if (bw != null) { |
| 109 | cfg.bandwidth(bw); |
| 110 | } |
| 111 | |
| 112 | cfg.apply(); |
| 113 | } |
| 114 | |
| 115 | } |