blob: dc73c819d710b62dd40d3fb25b1e684f9ebd3988 [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.onlab.onos.cli.net;
20
21import static org.onlab.onos.net.DeviceId.deviceId;
22import static org.onlab.onos.net.PortNumber.portNumber;
23
24import org.apache.karaf.shell.commands.Argument;
25import org.apache.karaf.shell.commands.Command;
26import org.onlab.onos.net.ConnectPoint;
27import org.onlab.onos.net.DeviceId;
28import org.onlab.onos.net.PortNumber;
29import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentService;
31import org.onlab.onos.net.intent.OpticalConnectivityIntent;
32
33/**
34 * Installs optical connectivity intents.
35 */
36@Command(scope = "onos", name = "add-optical-intent",
37 description = "Installs optical connectivity intent")
38public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
39
40 @Argument(index = 0, name = "ingressDevice",
41 description = "Ingress Device/Port Description",
42 required = true, multiValued = false)
43 String ingressDeviceString = null;
44
45 @Argument(index = 1, name = "egressDevice",
46 description = "Egress Device/Port Description",
47 required = true, multiValued = false)
48 String egressDeviceString = null;
49
50 @Override
51 protected void execute() {
52 IntentService service = get(IntentService.class);
53
54 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
55 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
56 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
57
58 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
59 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
60 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
61
62 Intent intent = new OpticalConnectivityIntent(appId(), ingress, egress);
63 service.submit(intent);
64 }
65
66 /**
67 * Extracts the port number portion of the ConnectPoint.
68 *
69 * @param deviceString string representing the device/port
70 * @return port number as a string, empty string if the port is not found
71 */
72 private String getPortNumber(String deviceString) {
73 int slash = deviceString.indexOf('/');
74 if (slash <= 0) {
75 return "";
76 }
77 return deviceString.substring(slash + 1, deviceString.length());
78 }
79
80 /**
81 * Extracts the device ID portion of the ConnectPoint.
82 *
83 * @param deviceString string representing the device/port
84 * @return device ID string
85 */
86 private String getDeviceId(String deviceString) {
87 int slash = deviceString.indexOf('/');
88 if (slash <= 0) {
89 return "";
90 }
91 return deviceString.substring(0, slash);
92 }
93}