blob: da59e9c161b86fe43f4fce3d2f75332a3fa6de24 [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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
Brian O'Connor5296b322014-10-23 14:59:05 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070020import org.apache.karaf.shell.commands.Option;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070021import org.onosproject.cli.app.AllApplicationNamesCompleter;
22import org.onosproject.cli.net.ConnectPointCompleter;
23import org.onosproject.cli.net.ConnectivityIntentCommand;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080024import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020026import org.onosproject.net.Device;
Marc De Leenheer88194c32015-05-29 22:10:59 -070027import org.onosproject.net.DeviceId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070028import org.onosproject.net.OduSignalType;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070029import org.onosproject.net.Port;
30import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentService;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070033import org.onosproject.net.intent.OpticalCircuitIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.OpticalConnectivityIntent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020035import org.onosproject.net.intent.OpticalOduIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080036import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070037import org.onosproject.net.optical.OduCltPort;
Brian O'Connor5296b322014-10-23 14:59:05 -070038
Marc De Leenheer88194c32015-05-29 22:10:59 -070039import java.util.List;
40
41import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer88194c32015-05-29 22:10:59 -070043
Brian O'Connor5296b322014-10-23 14:59:05 -070044/**
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070045 * Installs optical connectivity or circuit intents, depending on given port types.
Brian O'Connor5296b322014-10-23 14:59:05 -070046 */
47@Command(scope = "onos", name = "add-optical-intent",
48 description = "Installs optical connectivity intent")
49public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
50
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070051 // OSGi workaround
52 @SuppressWarnings("unused")
53 private ConnectPointCompleter cpCompleter;
54
55 // OSGi workaround
56 @SuppressWarnings("unused")
57 private AllApplicationNamesCompleter appCompleter;
58
Brian O'Connor5296b322014-10-23 14:59:05 -070059 @Argument(index = 0, name = "ingressDevice",
60 description = "Ingress Device/Port Description",
61 required = true, multiValued = false)
Ayaka Koshibeafb546f2015-10-23 17:13:58 -070062 String ingressDeviceString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070063
64 @Argument(index = 1, name = "egressDevice",
65 description = "Egress Device/Port Description",
66 required = true, multiValued = false)
Ayaka Koshibeafb546f2015-10-23 17:13:58 -070067 String egressDeviceString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070068
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'Connor5296b322014-10-23 14:59:05 -070075
Marc De Leenheer88194c32015-05-29 22:10:59 -070076 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 Leenheer88194c32015-05-29 22:10:59 -070083 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'Connor5296b322014-10-23 14:59:05 -070096 @Override
97 protected void execute() {
98 IntentService service = get(IntentService.class);
99
Marc De Leenheer88194c32015-05-29 22:10:59 -0700100 ConnectPoint ingress = createConnectPoint(ingressDeviceString);
101 ConnectPoint egress = createConnectPoint(egressDeviceString);
Brian O'Connor5296b322014-10-23 14:59:05 -0700102
Marc De Leenheer88194c32015-05-29 22:10:59 -0700103 if (ingress == null || egress == null) {
Ayaka Koshibeafb546f2015-10-23 17:13:58 -0700104 print("Invalid endpoint(s); could not create optical intent");
105 return;
Marc De Leenheer88194c32015-05-29 22:10:59 -0700106 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700107
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800108 DeviceService deviceService = opticalView(get(DeviceService.class));
109
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700110 Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port());
111 Port dstPort = deviceService.getPort(egress.deviceId(), egress.port());
112
113 Intent intent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200114
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700115 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200116 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();
126 if (Device.Type.ROADM.equals(srcDevice.type())) {
127 intent = OpticalCircuitIntent.builder()
128 .appId(appId())
129 .key(key())
130 .src(ingress)
131 .dst(egress)
132 .signalType(signalType)
133 .bidirectional(bidirectional)
134 .build();
135 } else if (Device.Type.OTN.equals(srcDevice.type())) {
136 intent = OpticalOduIntent.builder()
137 .appId(appId())
138 .key(key())
139 .src(ingress)
140 .dst(egress)
141 .signalType(signalType)
142 .bidirectional(bidirectional)
143 .build();
144 } else {
145 print("Wrong Device Type for connect points %s and %s", ingress, egress);
146 return;
147 }
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700148 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200149 OduSignalType signalType = ((OchPort) srcPort).signalType();
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700150 intent = OpticalConnectivityIntent.builder()
151 .appId(appId())
152 .key(key())
153 .src(ingress)
154 .dst(egress)
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200155 .signalType(signalType)
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -0700156 .bidirectional(bidirectional)
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700157 .build();
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800158 } else if (srcPort instanceof org.onosproject.net.OchPort &&
159 dstPort instanceof org.onosproject.net.OchPort) {
160 print("WARN: encountered old OchPort model");
161 // old OchPort model can be removed when ready
162 OduSignalType signalType = ((org.onosproject.net.OchPort) srcPort).signalType();
163 intent = OpticalConnectivityIntent.builder()
164 .appId(appId())
165 .key(key())
166 .src(ingress)
167 .dst(egress)
168 .signalType(signalType)
169 .bidirectional(bidirectional)
170 .build();
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700171 } else {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200172 print("Unable to create optical intent between connect points %s and %s", ingress, egress);
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700173 return;
174 }
175
Brian O'Connor5296b322014-10-23 14:59:05 -0700176 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800177 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700178 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700179}