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; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.app.AllApplicationNamesCompleter; |
| 23 | import org.onosproject.cli.net.ConnectPointCompleter; |
| 24 | import org.onosproject.cli.net.ConnectivityIntentCommand; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ChannelSpacing; |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 26 | import org.onosproject.net.CltSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 30 | import org.onosproject.net.GridType; |
| 31 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 32 | import org.onosproject.net.OduSignalType; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.Intent; |
| 36 | import org.onosproject.net.intent.IntentService; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 37 | import org.onosproject.net.intent.OpticalCircuitIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 39 | import org.onosproject.net.intent.OpticalOduIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 40 | import org.onosproject.net.optical.OchPort; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 41 | import org.onosproject.net.optical.OduCltPort; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 42 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 43 | import java.util.Map; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 44 | import java.util.List; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkArgument; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 47 | import static com.google.common.base.Preconditions.checkNotNull; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 49 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 50 | /** |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 51 | * Installs optical connectivity or circuit intents, depending on given port types. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 52 | */ |
| 53 | @Command(scope = "onos", name = "add-optical-intent", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 54 | description = "Installs optical connectivity intent") |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 55 | public class AddOpticalIntentCommand extends ConnectivityIntentCommand { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 56 | private static final String SIGNAL_FORMAT = "slotGranularity/channelSpacing(in GHz e.g 6.25,12.5,25,50,100)/" + |
| 57 | "spaceMultiplier/gridType(cwdm, flex, dwdm) " + "e.g 1/6.25/1/flex"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 58 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 59 | private static final String CH_6P25 = "6.25"; |
| 60 | private static final String CH_12P5 = "12.5"; |
| 61 | private static final String CH_25 = "25"; |
| 62 | private static final String CH_50 = "50"; |
| 63 | private static final String CH_100 = "100"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 64 | |
| 65 | private static final Map<String, ChannelSpacing> CHANNEL_SPACING_MAP = ImmutableMap |
| 66 | .<String, ChannelSpacing>builder() |
| 67 | .put(CH_6P25, ChannelSpacing.CHL_6P25GHZ) |
| 68 | .put(CH_12P5, ChannelSpacing.CHL_12P5GHZ) |
| 69 | .put(CH_25, ChannelSpacing.CHL_25GHZ) |
| 70 | .put(CH_50, ChannelSpacing.CHL_50GHZ) |
| 71 | .put(CH_100, ChannelSpacing.CHL_100GHZ) |
| 72 | .build(); |
| 73 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 74 | // OSGi workaround |
| 75 | @SuppressWarnings("unused") |
| 76 | private ConnectPointCompleter cpCompleter; |
| 77 | |
| 78 | // OSGi workaround |
| 79 | @SuppressWarnings("unused") |
| 80 | private AllApplicationNamesCompleter appCompleter; |
| 81 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 82 | @Argument(index = 0, name = "ingress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 83 | description = "Ingress Device/Port Description", |
| 84 | required = true, multiValued = false) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 85 | String ingressString = ""; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 86 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 87 | @Argument(index = 1, name = "egress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 88 | description = "Egress Device/Port Description", |
| 89 | required = true, multiValued = false) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 90 | String egressString = ""; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 91 | |
| 92 | @Option(name = "-b", aliases = "--bidirectional", |
| 93 | description = "If this argument is passed the optical link created will be bidirectional, " + |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 94 | "else the link will be unidirectional.", |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 95 | required = false, multiValued = false) |
| 96 | private boolean bidirectional = false; |
| 97 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 98 | @Option(name = "-s", aliases = "--signal", |
| 99 | description = "Optical Signal. Format = " + SIGNAL_FORMAT, |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 100 | required = false, multiValued = false) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 101 | private String signal; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 102 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 103 | private ConnectPoint createConnectPoint(String devicePortString) { |
| 104 | String[] splitted = devicePortString.split("/"); |
| 105 | |
| 106 | checkArgument(splitted.length == 2, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 107 | "Connect point must be in \"deviceUri/portNumber\" format"); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 108 | |
| 109 | DeviceId deviceId = DeviceId.deviceId(splitted[0]); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 110 | DeviceService deviceService = get(DeviceService.class); |
| 111 | |
| 112 | List<Port> ports = deviceService.getPorts(deviceId); |
| 113 | |
| 114 | for (Port port : ports) { |
| 115 | if (splitted[1].equals(port.number().name())) { |
| 116 | return new ConnectPoint(deviceId, port.number()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return null; |
| 121 | } |
| 122 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 123 | private OchSignal createOchSignal() throws IllegalArgumentException { |
| 124 | if (signal == null) { |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 125 | return null; |
| 126 | } |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 127 | try { |
| 128 | String[] splitted = signal.split("/"); |
| 129 | checkArgument(splitted.length == 4, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 130 | "signal requires 4 parameters: " + SIGNAL_FORMAT); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 131 | int slotGranularity = Integer.parseInt(splitted[0]); |
| 132 | String chSpacing = splitted[1]; |
| 133 | ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing), |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 134 | String.format("invalid channel spacing: %s", chSpacing)); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 135 | int multiplier = Integer.parseInt(splitted[2]); |
| 136 | String gdType = splitted[3].toUpperCase(); |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 137 | GridType gridType = GridType.valueOf(gdType); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 138 | return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity); |
| 139 | } catch (RuntimeException e) { |
| 140 | /* catching RuntimeException as both NullPointerException (thrown by |
| 141 | * checkNotNull) and IllegalArgumentException (thrown by checkArgument) |
| 142 | * are subclasses of RuntimeException. |
| 143 | */ |
| 144 | String msg = String.format("Invalid signal format: %s, expected format is %s.", |
| 145 | signal, SIGNAL_FORMAT); |
| 146 | print(msg); |
| 147 | throw new IllegalArgumentException(msg, e); |
| 148 | } |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 151 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 152 | @Override |
| 153 | protected void execute() { |
| 154 | IntentService service = get(IntentService.class); |
| 155 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 156 | ConnectPoint ingress = createConnectPoint(ingressString); |
| 157 | ConnectPoint egress = createConnectPoint(egressString); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 158 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 159 | if (ingress == null || egress == null) { |
Ayaka Koshibe | afb546f | 2015-10-23 17:13:58 -0700 | [diff] [blame] | 160 | print("Invalid endpoint(s); could not create optical intent"); |
| 161 | return; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 162 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 163 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 164 | DeviceService deviceService = opticalView(get(DeviceService.class)); |
| 165 | |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 166 | Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port()); |
| 167 | Port dstPort = deviceService.getPort(egress.deviceId(), egress.port()); |
| 168 | |
| 169 | Intent intent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 170 | |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 171 | if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 172 | Device srcDevice = deviceService.getDevice(ingress.deviceId()); |
| 173 | Device dstDevice = deviceService.getDevice(egress.deviceId()); |
| 174 | |
| 175 | // continue only if both OduClt port's Devices are of the same type |
| 176 | if (!(srcDevice.type().equals(dstDevice.type()))) { |
| 177 | print("Devices without same deviceType: SRC=%s and DST=%s", srcDevice.type(), dstDevice.type()); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | CltSignalType signalType = ((OduCltPort) srcPort).signalType(); |
Konstantinos Kanonakis | 5c69c75 | 2017-02-22 13:20:41 -0600 | [diff] [blame] | 182 | if (Device.Type.ROADM.equals(srcDevice.type()) || |
| 183 | Device.Type.ROADM_OTN.equals(srcDevice.type())) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 184 | intent = OpticalCircuitIntent.builder() |
| 185 | .appId(appId()) |
| 186 | .key(key()) |
| 187 | .src(ingress) |
| 188 | .dst(egress) |
| 189 | .signalType(signalType) |
| 190 | .bidirectional(bidirectional) |
| 191 | .build(); |
| 192 | } else if (Device.Type.OTN.equals(srcDevice.type())) { |
| 193 | intent = OpticalOduIntent.builder() |
| 194 | .appId(appId()) |
| 195 | .key(key()) |
| 196 | .src(ingress) |
| 197 | .dst(egress) |
| 198 | .signalType(signalType) |
| 199 | .bidirectional(bidirectional) |
| 200 | .build(); |
| 201 | } else { |
| 202 | print("Wrong Device Type for connect points %s and %s", ingress, egress); |
| 203 | return; |
| 204 | } |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 205 | } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 206 | OduSignalType signalType = ((OchPort) srcPort).signalType(); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 207 | intent = OpticalConnectivityIntent.builder() |
| 208 | .appId(appId()) |
| 209 | .key(key()) |
| 210 | .src(ingress) |
| 211 | .dst(egress) |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 212 | .signalType(signalType) |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 213 | .bidirectional(bidirectional) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 214 | .ochSignal(createOchSignal()) |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 215 | .build(); |
| 216 | } else { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 217 | print("Unable to create optical intent between connect points %s and %s", ingress, egress); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 218 | return; |
| 219 | } |
| 220 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 221 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 222 | print("Optical intent submitted:\n%s", intent.toString()); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 223 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 224 | } |