blob: 4bdde847143635f533f0b3f27b913c14c11533cd [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor5296b322014-10-23 14:59:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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'Connor5296b322014-10-23 14:59:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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'Connor5296b322014-10-23 14:59:05 -070015 */
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070016package org.onosproject.net.optical.cli;
Brian O'Connor5296b322014-10-23 14:59:05 -070017
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070018import com.google.common.collect.ImmutableMap;
Brian O'Connor5296b322014-10-23 14:59:05 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070021import org.apache.karaf.shell.commands.Option;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070022import org.onosproject.cli.app.AllApplicationNamesCompleter;
23import org.onosproject.cli.net.ConnectPointCompleter;
24import org.onosproject.cli.net.ConnectivityIntentCommand;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070025import org.onosproject.net.ChannelSpacing;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080026import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020028import org.onosproject.net.Device;
Marc De Leenheer88194c32015-05-29 22:10:59 -070029import org.onosproject.net.DeviceId;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070030import org.onosproject.net.GridType;
31import org.onosproject.net.OchSignal;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070032import org.onosproject.net.OduSignalType;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070033import org.onosproject.net.Port;
34import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.intent.Intent;
36import org.onosproject.net.intent.IntentService;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070037import org.onosproject.net.intent.OpticalCircuitIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.OpticalConnectivityIntent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020039import org.onosproject.net.intent.OpticalOduIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080040import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070041import org.onosproject.net.optical.OduCltPort;
Brian O'Connor5296b322014-10-23 14:59:05 -070042
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070043import java.util.Map;
Marc De Leenheer88194c32015-05-29 22:10:59 -070044import java.util.List;
45
46import static com.google.common.base.Preconditions.checkArgument;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070047import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080048import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer88194c32015-05-29 22:10:59 -070049
Brian O'Connor5296b322014-10-23 14:59:05 -070050/**
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070051 * Installs optical connectivity or circuit intents, depending on given port types.
Brian O'Connor5296b322014-10-23 14:59:05 -070052 */
53@Command(scope = "onos", name = "add-optical-intent",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070054 description = "Installs optical connectivity intent")
Brian O'Connor5296b322014-10-23 14:59:05 -070055public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070056 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";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070058
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070059 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";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070064
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 HIGUCHI1d547bf2016-08-02 21:44:48 -070074 // OSGi workaround
75 @SuppressWarnings("unused")
76 private ConnectPointCompleter cpCompleter;
77
78 // OSGi workaround
79 @SuppressWarnings("unused")
80 private AllApplicationNamesCompleter appCompleter;
81
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080082 @Argument(index = 0, name = "ingress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070083 description = "Ingress Device/Port Description",
84 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080085 String ingressString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070086
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080087 @Argument(index = 1, name = "egress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070088 description = "Egress Device/Port Description",
89 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080090 String egressString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070091
92 @Option(name = "-b", aliases = "--bidirectional",
93 description = "If this argument is passed the optical link created will be bidirectional, " +
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070094 "else the link will be unidirectional.",
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070095 required = false, multiValued = false)
96 private boolean bidirectional = false;
97
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070098 @Option(name = "-s", aliases = "--signal",
99 description = "Optical Signal. Format = " + SIGNAL_FORMAT,
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700100 required = false, multiValued = false)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700101 private String signal;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700102
Marc De Leenheer88194c32015-05-29 22:10:59 -0700103 private ConnectPoint createConnectPoint(String devicePortString) {
104 String[] splitted = devicePortString.split("/");
105
106 checkArgument(splitted.length == 2,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700107 "Connect point must be in \"deviceUri/portNumber\" format");
Marc De Leenheer88194c32015-05-29 22:10:59 -0700108
109 DeviceId deviceId = DeviceId.deviceId(splitted[0]);
Marc De Leenheer88194c32015-05-29 22:10:59 -0700110 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
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700123 private OchSignal createOchSignal() throws IllegalArgumentException {
124 if (signal == null) {
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700125 return null;
126 }
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700127 try {
128 String[] splitted = signal.split("/");
129 checkArgument(splitted.length == 4,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700130 "signal requires 4 parameters: " + SIGNAL_FORMAT);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700131 int slotGranularity = Integer.parseInt(splitted[0]);
132 String chSpacing = splitted[1];
133 ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing),
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700134 String.format("invalid channel spacing: %s", chSpacing));
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700135 int multiplier = Integer.parseInt(splitted[2]);
136 String gdType = splitted[3].toUpperCase();
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700137 GridType gridType = GridType.valueOf(gdType);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700138 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 Leenheeradfeffd2017-06-22 16:05:34 -0700149 }
150
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700151
Brian O'Connor5296b322014-10-23 14:59:05 -0700152 @Override
153 protected void execute() {
154 IntentService service = get(IntentService.class);
155
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800156 ConnectPoint ingress = createConnectPoint(ingressString);
157 ConnectPoint egress = createConnectPoint(egressString);
Brian O'Connor5296b322014-10-23 14:59:05 -0700158
Marc De Leenheer88194c32015-05-29 22:10:59 -0700159 if (ingress == null || egress == null) {
Ayaka Koshibeafb546f2015-10-23 17:13:58 -0700160 print("Invalid endpoint(s); could not create optical intent");
161 return;
Marc De Leenheer88194c32015-05-29 22:10:59 -0700162 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700163
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800164 DeviceService deviceService = opticalView(get(DeviceService.class));
165
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700166 Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port());
167 Port dstPort = deviceService.getPort(egress.deviceId(), egress.port());
168
169 Intent intent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200170
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700171 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200172 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 Kanonakis5c69c752017-02-22 13:20:41 -0600182 if (Device.Type.ROADM.equals(srcDevice.type()) ||
183 Device.Type.ROADM_OTN.equals(srcDevice.type())) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200184 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 Leenheer9f7d1892015-05-30 13:22:24 -0700205 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200206 OduSignalType signalType = ((OchPort) srcPort).signalType();
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700207 intent = OpticalConnectivityIntent.builder()
208 .appId(appId())
209 .key(key())
210 .src(ingress)
211 .dst(egress)
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200212 .signalType(signalType)
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -0700213 .bidirectional(bidirectional)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700214 .ochSignal(createOchSignal())
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700215 .build();
216 } else {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200217 print("Unable to create optical intent between connect points %s and %s", ingress, egress);
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700218 return;
219 }
220
Brian O'Connor5296b322014-10-23 14:59:05 -0700221 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800222 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700223 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700224}