blob: 001c2732d3e052868b1c940db89c09fb504dcf15 [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
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;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080021import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.ConnectPoint;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020023import org.onosproject.net.Device;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070024import org.onosproject.net.OduCltPort;
Marc De Leenheer88194c32015-05-29 22:10:59 -070025import org.onosproject.net.DeviceId;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070026import org.onosproject.net.OduSignalType;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070027import org.onosproject.net.Port;
28import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentService;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070031import org.onosproject.net.intent.OpticalCircuitIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.OpticalConnectivityIntent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020033import org.onosproject.net.intent.OpticalOduIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080034import org.onosproject.net.optical.OchPort;
Brian O'Connor5296b322014-10-23 14:59:05 -070035
Marc De Leenheer88194c32015-05-29 22:10:59 -070036import java.util.List;
37
38import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080039import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer88194c32015-05-29 22:10:59 -070040
Brian O'Connor5296b322014-10-23 14:59:05 -070041/**
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070042 * Installs optical connectivity or circuit intents, depending on given port types.
Brian O'Connor5296b322014-10-23 14:59:05 -070043 */
44@Command(scope = "onos", name = "add-optical-intent",
45 description = "Installs optical connectivity intent")
46public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
47
48 @Argument(index = 0, name = "ingressDevice",
49 description = "Ingress Device/Port Description",
50 required = true, multiValued = false)
Ayaka Koshibeafb546f2015-10-23 17:13:58 -070051 String ingressDeviceString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070052
53 @Argument(index = 1, name = "egressDevice",
54 description = "Egress Device/Port Description",
55 required = true, multiValued = false)
Ayaka Koshibeafb546f2015-10-23 17:13:58 -070056 String egressDeviceString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070057
58 @Option(name = "-b", aliases = "--bidirectional",
59 description = "If this argument is passed the optical link created will be bidirectional, " +
60 "else the link will be unidirectional.",
61 required = false, multiValued = false)
62 private boolean bidirectional = false;
63
Brian O'Connor5296b322014-10-23 14:59:05 -070064
Marc De Leenheer88194c32015-05-29 22:10:59 -070065 private ConnectPoint createConnectPoint(String devicePortString) {
66 String[] splitted = devicePortString.split("/");
67
68 checkArgument(splitted.length == 2,
69 "Connect point must be in \"deviceUri/portNumber\" format");
70
71 DeviceId deviceId = DeviceId.deviceId(splitted[0]);
Marc De Leenheer88194c32015-05-29 22:10:59 -070072 DeviceService deviceService = get(DeviceService.class);
73
74 List<Port> ports = deviceService.getPorts(deviceId);
75
76 for (Port port : ports) {
77 if (splitted[1].equals(port.number().name())) {
78 return new ConnectPoint(deviceId, port.number());
79 }
80 }
81
82 return null;
83 }
84
Brian O'Connor5296b322014-10-23 14:59:05 -070085 @Override
86 protected void execute() {
87 IntentService service = get(IntentService.class);
88
Marc De Leenheer88194c32015-05-29 22:10:59 -070089 ConnectPoint ingress = createConnectPoint(ingressDeviceString);
90 ConnectPoint egress = createConnectPoint(egressDeviceString);
Brian O'Connor5296b322014-10-23 14:59:05 -070091
Marc De Leenheer88194c32015-05-29 22:10:59 -070092 if (ingress == null || egress == null) {
Ayaka Koshibeafb546f2015-10-23 17:13:58 -070093 print("Invalid endpoint(s); could not create optical intent");
94 return;
Marc De Leenheer88194c32015-05-29 22:10:59 -070095 }
Brian O'Connor5296b322014-10-23 14:59:05 -070096
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080097 DeviceService deviceService = opticalView(get(DeviceService.class));
98
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070099 Port srcPort = deviceService.getPort(ingress.deviceId(), ingress.port());
100 Port dstPort = deviceService.getPort(egress.deviceId(), egress.port());
101
102 Intent intent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200103
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700104 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200105 Device srcDevice = deviceService.getDevice(ingress.deviceId());
106 Device dstDevice = deviceService.getDevice(egress.deviceId());
107
108 // continue only if both OduClt port's Devices are of the same type
109 if (!(srcDevice.type().equals(dstDevice.type()))) {
110 print("Devices without same deviceType: SRC=%s and DST=%s", srcDevice.type(), dstDevice.type());
111 return;
112 }
113
114 CltSignalType signalType = ((OduCltPort) srcPort).signalType();
115 if (Device.Type.ROADM.equals(srcDevice.type())) {
116 intent = OpticalCircuitIntent.builder()
117 .appId(appId())
118 .key(key())
119 .src(ingress)
120 .dst(egress)
121 .signalType(signalType)
122 .bidirectional(bidirectional)
123 .build();
124 } else if (Device.Type.OTN.equals(srcDevice.type())) {
125 intent = OpticalOduIntent.builder()
126 .appId(appId())
127 .key(key())
128 .src(ingress)
129 .dst(egress)
130 .signalType(signalType)
131 .bidirectional(bidirectional)
132 .build();
133 } else {
134 print("Wrong Device Type for connect points %s and %s", ingress, egress);
135 return;
136 }
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700137 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200138 OduSignalType signalType = ((OchPort) srcPort).signalType();
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700139 intent = OpticalConnectivityIntent.builder()
140 .appId(appId())
141 .key(key())
142 .src(ingress)
143 .dst(egress)
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200144 .signalType(signalType)
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -0700145 .bidirectional(bidirectional)
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700146 .build();
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800147 } else if (srcPort instanceof org.onosproject.net.OchPort &&
148 dstPort instanceof org.onosproject.net.OchPort) {
149 print("WARN: encountered old OchPort model");
150 // old OchPort model can be removed when ready
151 OduSignalType signalType = ((org.onosproject.net.OchPort) srcPort).signalType();
152 intent = OpticalConnectivityIntent.builder()
153 .appId(appId())
154 .key(key())
155 .src(ingress)
156 .dst(egress)
157 .signalType(signalType)
158 .bidirectional(bidirectional)
159 .build();
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700160 } else {
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200161 print("Unable to create optical intent between connect points %s and %s", ingress, egress);
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700162 return;
163 }
164
Brian O'Connor5296b322014-10-23 14:59:05 -0700165 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800166 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700167 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700168}