blob: 112062148191329277560bc754c7d28eb1cde0e8 [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 */
16package org.onlab.onos.cli.net;
17
18import static org.onlab.onos.net.DeviceId.deviceId;
19import static org.onlab.onos.net.PortNumber.portNumber;
20
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.PortNumber;
26import org.onlab.onos.net.intent.Intent;
27import org.onlab.onos.net.intent.IntentService;
28import org.onlab.onos.net.intent.OpticalConnectivityIntent;
29
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
59 Intent intent = new OpticalConnectivityIntent(appId(), ingress, egress);
60 service.submit(intent);
61 }
62
63 /**
64 * Extracts the port number portion of the ConnectPoint.
65 *
66 * @param deviceString string representing the device/port
67 * @return port number as a string, empty string if the port is not found
68 */
69 private String getPortNumber(String deviceString) {
70 int slash = deviceString.indexOf('/');
71 if (slash <= 0) {
72 return "";
73 }
74 return deviceString.substring(slash + 1, deviceString.length());
75 }
76
77 /**
78 * Extracts the device ID portion of the ConnectPoint.
79 *
80 * @param deviceString string representing the device/port
81 * @return device ID string
82 */
83 private String getDeviceId(String deviceString) {
84 int slash = deviceString.indexOf('/');
85 if (slash <= 0) {
86 return "";
87 }
88 return deviceString.substring(0, slash);
89 }
90}