blob: 411ade07513bbe730a2203542289cf19cfe9fefa [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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'Connorabafb502014-12-02 22:26:20 -080018import static org.onosproject.net.DeviceId.deviceId;
19import static org.onosproject.net.PortNumber.portNumber;
Brian O'Connor5296b322014-10-23 14:59:05 -070020
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.intent.Intent;
27import org.onosproject.net.intent.IntentService;
28import org.onosproject.net.intent.OpticalConnectivityIntent;
Brian O'Connor5296b322014-10-23 14:59:05 -070029
30/**
31 * Installs optical connectivity intents.
32 */
33@Command(scope = "onos", name = "add-optical-intent",
34 description = "Installs optical connectivity intent")
35public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
36
37 @Argument(index = 0, name = "ingressDevice",
38 description = "Ingress Device/Port Description",
39 required = true, multiValued = false)
40 String ingressDeviceString = null;
41
42 @Argument(index = 1, name = "egressDevice",
43 description = "Egress Device/Port Description",
44 required = true, multiValued = false)
45 String egressDeviceString = null;
46
47 @Override
48 protected void execute() {
49 IntentService service = get(IntentService.class);
50
51 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
52 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
53 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
54
55 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
56 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
57 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
58
Ray Milkeye076c792015-03-24 09:38:30 -070059 Intent intent = OpticalConnectivityIntent.builder()
60 .appId(appId())
61 .key(key())
62 .src(ingress)
63 .dst(egress)
64 .build();
Brian O'Connor5296b322014-10-23 14:59:05 -070065 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080066 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -070067 }
68
69 /**
70 * Extracts the port number portion of the ConnectPoint.
71 *
72 * @param deviceString string representing the device/port
73 * @return port number as a string, empty string if the port is not found
74 */
75 private String getPortNumber(String deviceString) {
76 int slash = deviceString.indexOf('/');
77 if (slash <= 0) {
78 return "";
79 }
80 return deviceString.substring(slash + 1, deviceString.length());
81 }
82
83 /**
84 * Extracts the device ID portion of the ConnectPoint.
85 *
86 * @param deviceString string representing the device/port
87 * @return device ID string
88 */
89 private String getDeviceId(String deviceString) {
90 int slash = deviceString.indexOf('/');
91 if (slash <= 0) {
92 return "";
93 }
94 return deviceString.substring(0, slash);
95 }
96}