Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.cli; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 17 | |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | c1e69e8 | 2018-10-09 15:59:22 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.net.ConnectPointCompleter; |
| 25 | import org.onosproject.cli.net.ConnectivityIntentCommand; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 29 | import org.onosproject.net.GridType; |
| 30 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Intent; |
| 34 | import org.onosproject.net.intent.IntentService; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 35 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 36 | import java.util.Map; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 37 | import java.util.List; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkArgument; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 40 | import static com.google.common.base.Preconditions.checkNotNull; |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 41 | import static org.onosproject.net.optical.util.OpticalIntentUtility.createOpticalIntent; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 42 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 43 | /** |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 44 | * Installs optical connectivity or circuit intents, depending on given port types. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 45 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 46 | @Service |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 47 | @Command(scope = "onos", name = "add-optical-intent", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 48 | description = "Installs optical connectivity intent") |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 49 | public class AddOpticalIntentCommand extends ConnectivityIntentCommand { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 50 | private static final String SIGNAL_FORMAT = "slotGranularity/channelSpacing(in GHz e.g 6.25,12.5,25,50,100)/" + |
| 51 | "spaceMultiplier/gridType(cwdm, flex, dwdm) " + "e.g 1/6.25/1/flex"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 52 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 53 | private static final String CH_6P25 = "6.25"; |
| 54 | private static final String CH_12P5 = "12.5"; |
| 55 | private static final String CH_25 = "25"; |
| 56 | private static final String CH_50 = "50"; |
| 57 | private static final String CH_100 = "100"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 58 | |
| 59 | private static final Map<String, ChannelSpacing> CHANNEL_SPACING_MAP = ImmutableMap |
| 60 | .<String, ChannelSpacing>builder() |
| 61 | .put(CH_6P25, ChannelSpacing.CHL_6P25GHZ) |
| 62 | .put(CH_12P5, ChannelSpacing.CHL_12P5GHZ) |
| 63 | .put(CH_25, ChannelSpacing.CHL_25GHZ) |
| 64 | .put(CH_50, ChannelSpacing.CHL_50GHZ) |
| 65 | .put(CH_100, ChannelSpacing.CHL_100GHZ) |
| 66 | .build(); |
| 67 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 68 | @Argument(index = 0, name = "ingress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 69 | description = "Ingress Device/Port Description", |
| 70 | required = true, multiValued = false) |
Ray Milkey | c1e69e8 | 2018-10-09 15:59:22 -0700 | [diff] [blame] | 71 | @Completion(ConnectPointCompleter.class) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 72 | String ingressString = ""; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 73 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 74 | @Argument(index = 1, name = "egress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 75 | description = "Egress Device/Port Description", |
| 76 | required = true, multiValued = false) |
Ray Milkey | c1e69e8 | 2018-10-09 15:59:22 -0700 | [diff] [blame] | 77 | @Completion(ConnectPointCompleter.class) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 78 | String egressString = ""; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 79 | |
| 80 | @Option(name = "-b", aliases = "--bidirectional", |
| 81 | description = "If this argument is passed the optical link created will be bidirectional, " + |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 82 | "else the link will be unidirectional.", |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 83 | required = false, multiValued = false) |
| 84 | private boolean bidirectional = false; |
| 85 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 86 | @Option(name = "-s", aliases = "--signal", |
| 87 | description = "Optical Signal. Format = " + SIGNAL_FORMAT, |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 88 | required = false, multiValued = false) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 89 | private String signal; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 90 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 91 | private ConnectPoint createConnectPoint(String devicePortString) { |
| 92 | String[] splitted = devicePortString.split("/"); |
| 93 | |
| 94 | checkArgument(splitted.length == 2, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 95 | "Connect point must be in \"deviceUri/portNumber\" format"); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 96 | |
| 97 | DeviceId deviceId = DeviceId.deviceId(splitted[0]); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 98 | DeviceService deviceService = get(DeviceService.class); |
| 99 | |
| 100 | List<Port> ports = deviceService.getPorts(deviceId); |
| 101 | |
| 102 | for (Port port : ports) { |
| 103 | if (splitted[1].equals(port.number().name())) { |
| 104 | return new ConnectPoint(deviceId, port.number()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return null; |
| 109 | } |
| 110 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 111 | private OchSignal createOchSignal() throws IllegalArgumentException { |
| 112 | if (signal == null) { |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 113 | return null; |
| 114 | } |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 115 | try { |
| 116 | String[] splitted = signal.split("/"); |
| 117 | checkArgument(splitted.length == 4, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 118 | "signal requires 4 parameters: " + SIGNAL_FORMAT); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 119 | int slotGranularity = Integer.parseInt(splitted[0]); |
| 120 | String chSpacing = splitted[1]; |
| 121 | ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing), |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 122 | String.format("invalid channel spacing: %s", chSpacing)); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 123 | int multiplier = Integer.parseInt(splitted[2]); |
| 124 | String gdType = splitted[3].toUpperCase(); |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 125 | GridType gridType = GridType.valueOf(gdType); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 126 | return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity); |
| 127 | } catch (RuntimeException e) { |
| 128 | /* catching RuntimeException as both NullPointerException (thrown by |
| 129 | * checkNotNull) and IllegalArgumentException (thrown by checkArgument) |
| 130 | * are subclasses of RuntimeException. |
| 131 | */ |
| 132 | String msg = String.format("Invalid signal format: %s, expected format is %s.", |
| 133 | signal, SIGNAL_FORMAT); |
| 134 | print(msg); |
| 135 | throw new IllegalArgumentException(msg, e); |
| 136 | } |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 137 | } |
| 138 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 139 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 140 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | protected void doExecute() { |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 142 | IntentService service = get(IntentService.class); |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 143 | DeviceService deviceService = get(DeviceService.class); |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 144 | ConnectPoint ingress = createConnectPoint(ingressString); |
| 145 | ConnectPoint egress = createConnectPoint(egressString); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 146 | |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 147 | Intent intent = createOpticalIntent(ingress, egress, deviceService, |
Andrea Campanella | e1e3e44 | 2019-10-21 13:45:32 +0200 | [diff] [blame] | 148 | key(), appId(), bidirectional, createOchSignal(), null); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 149 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 150 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 151 | print("Optical intent submitted:\n%s", intent.toString()); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 152 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 153 | } |