Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.app.AllApplicationNamesCompleter; |
| 22 | import org.onosproject.cli.net.ConnectPointCompleter; |
| 23 | import org.onosproject.cli.net.ConnectivityIntentCommand; |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 24 | import org.onosproject.net.CltSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 26 | import org.onosproject.net.Device; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 28 | import org.onosproject.net.OduSignalType; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.Intent; |
| 32 | import org.onosproject.net.intent.IntentService; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 33 | import org.onosproject.net.intent.OpticalCircuitIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 35 | import org.onosproject.net.intent.OpticalOduIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 36 | import org.onosproject.net.optical.OchPort; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 37 | import org.onosproject.net.optical.OduCltPort; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 38 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkArgument; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 42 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 43 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 44 | /** |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 45 | * Installs optical connectivity or circuit intents, depending on given port types. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 46 | */ |
| 47 | @Command(scope = "onos", name = "add-optical-intent", |
| 48 | description = "Installs optical connectivity intent") |
| 49 | public class AddOpticalIntentCommand extends ConnectivityIntentCommand { |
| 50 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 51 | // OSGi workaround |
| 52 | @SuppressWarnings("unused") |
| 53 | private ConnectPointCompleter cpCompleter; |
| 54 | |
| 55 | // OSGi workaround |
| 56 | @SuppressWarnings("unused") |
| 57 | private AllApplicationNamesCompleter appCompleter; |
| 58 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 59 | @Argument(index = 0, name = "ingressDevice", |
| 60 | description = "Ingress Device/Port Description", |
| 61 | required = true, multiValued = false) |
Ayaka Koshibe | afb546f | 2015-10-23 17:13:58 -0700 | [diff] [blame] | 62 | String ingressDeviceString = ""; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 63 | |
| 64 | @Argument(index = 1, name = "egressDevice", |
| 65 | description = "Egress Device/Port Description", |
| 66 | required = true, multiValued = false) |
Ayaka Koshibe | afb546f | 2015-10-23 17:13:58 -0700 | [diff] [blame] | 67 | String egressDeviceString = ""; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 68 | |
| 69 | @Option(name = "-b", aliases = "--bidirectional", |
| 70 | description = "If this argument is passed the optical link created will be bidirectional, " + |
| 71 | "else the link will be unidirectional.", |
| 72 | required = false, multiValued = false) |
| 73 | private boolean bidirectional = false; |
| 74 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 75 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 76 | private ConnectPoint createConnectPoint(String devicePortString) { |
| 77 | String[] splitted = devicePortString.split("/"); |
| 78 | |
| 79 | checkArgument(splitted.length == 2, |
| 80 | "Connect point must be in \"deviceUri/portNumber\" format"); |
| 81 | |
| 82 | DeviceId deviceId = DeviceId.deviceId(splitted[0]); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 83 | DeviceService deviceService = get(DeviceService.class); |
| 84 | |
| 85 | List<Port> ports = deviceService.getPorts(deviceId); |
| 86 | |
| 87 | for (Port port : ports) { |
| 88 | if (splitted[1].equals(port.number().name())) { |
| 89 | return new ConnectPoint(deviceId, port.number()); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return null; |
| 94 | } |
| 95 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 96 | @Override |
| 97 | protected void execute() { |
| 98 | IntentService service = get(IntentService.class); |
| 99 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 100 | ConnectPoint ingress = createConnectPoint(ingressDeviceString); |
| 101 | ConnectPoint egress = createConnectPoint(egressDeviceString); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 102 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 103 | if (ingress == null || egress == null) { |
Ayaka Koshibe | afb546f | 2015-10-23 17:13:58 -0700 | [diff] [blame] | 104 | print("Invalid endpoint(s); could not create optical intent"); |
| 105 | return; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 106 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 107 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 108 | DeviceService deviceService = opticalView(get(DeviceService.class)); |
| 109 | |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 110 | Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port()); |
| 111 | Port dstPort = deviceService.getPort(egress.deviceId(), egress.port()); |
| 112 | |
| 113 | Intent intent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 114 | |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 115 | if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 116 | Device srcDevice = deviceService.getDevice(ingress.deviceId()); |
| 117 | Device dstDevice = deviceService.getDevice(egress.deviceId()); |
| 118 | |
| 119 | // continue only if both OduClt port's Devices are of the same type |
| 120 | if (!(srcDevice.type().equals(dstDevice.type()))) { |
| 121 | print("Devices without same deviceType: SRC=%s and DST=%s", srcDevice.type(), dstDevice.type()); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | CltSignalType signalType = ((OduCltPort) srcPort).signalType(); |
Konstantinos Kanonakis | 5c69c75 | 2017-02-22 13:20:41 -0600 | [diff] [blame] | 126 | if (Device.Type.ROADM.equals(srcDevice.type()) || |
| 127 | Device.Type.ROADM_OTN.equals(srcDevice.type())) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 128 | intent = OpticalCircuitIntent.builder() |
| 129 | .appId(appId()) |
| 130 | .key(key()) |
| 131 | .src(ingress) |
| 132 | .dst(egress) |
| 133 | .signalType(signalType) |
| 134 | .bidirectional(bidirectional) |
| 135 | .build(); |
| 136 | } else if (Device.Type.OTN.equals(srcDevice.type())) { |
| 137 | intent = OpticalOduIntent.builder() |
| 138 | .appId(appId()) |
| 139 | .key(key()) |
| 140 | .src(ingress) |
| 141 | .dst(egress) |
| 142 | .signalType(signalType) |
| 143 | .bidirectional(bidirectional) |
| 144 | .build(); |
| 145 | } else { |
| 146 | print("Wrong Device Type for connect points %s and %s", ingress, egress); |
| 147 | return; |
| 148 | } |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 149 | } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 150 | OduSignalType signalType = ((OchPort) srcPort).signalType(); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 151 | intent = OpticalConnectivityIntent.builder() |
| 152 | .appId(appId()) |
| 153 | .key(key()) |
| 154 | .src(ingress) |
| 155 | .dst(egress) |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 156 | .signalType(signalType) |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 157 | .bidirectional(bidirectional) |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 158 | .build(); |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 159 | } else if (srcPort instanceof org.onosproject.net.OchPort && |
| 160 | dstPort instanceof org.onosproject.net.OchPort) { |
| 161 | print("WARN: encountered old OchPort model"); |
| 162 | // old OchPort model can be removed when ready |
| 163 | OduSignalType signalType = ((org.onosproject.net.OchPort) srcPort).signalType(); |
| 164 | intent = OpticalConnectivityIntent.builder() |
| 165 | .appId(appId()) |
| 166 | .key(key()) |
| 167 | .src(ingress) |
| 168 | .dst(egress) |
| 169 | .signalType(signalType) |
| 170 | .bidirectional(bidirectional) |
| 171 | .build(); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 172 | } else { |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 173 | 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] | 174 | return; |
| 175 | } |
| 176 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 177 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 178 | print("Optical intent submitted:\n%s", intent.toString()); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 179 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 180 | } |