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