blob: 22379ea68ff9a5326198e795a22d98fb5c951f51 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -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
Thomas Vachuska7d693f52014-10-21 19:17:57 -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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ray Milkeya058c732014-10-08 13:52:34 -070016package org.onlab.onos.cli.net;
17
Jonathan Hart2c25e362014-11-17 18:14:19 -080018import static org.onlab.onos.net.DeviceId.deviceId;
19import static org.onlab.onos.net.PortNumber.portNumber;
20
Ray Milkey460f4022014-11-05 15:41:43 -080021import java.util.List;
22
Ray Milkeya058c732014-10-08 13:52:34 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
Ray Milkeya058c732014-10-08 13:52:34 -070025import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.PortNumber;
Ray Milkeya058c732014-10-08 13:52:34 -070028import org.onlab.onos.net.flow.TrafficSelector;
29import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080030import org.onlab.onos.net.intent.Constraint;
Ray Milkeya058c732014-10-08 13:52:34 -070031import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070032import org.onlab.onos.net.intent.IntentService;
33import org.onlab.onos.net.intent.PointToPointIntent;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034
Ray Milkeya058c732014-10-08 13:52:34 -070035/**
36 * Installs point-to-point connectivity intents.
37 */
38@Command(scope = "onos", name = "add-point-intent",
39 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070040public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070041
42 @Argument(index = 0, name = "ingressDevice",
43 description = "Ingress Device/Port Description",
44 required = true, multiValued = false)
45 String ingressDeviceString = null;
46
47 @Argument(index = 1, name = "egressDevice",
48 description = "Egress Device/Port Description",
49 required = true, multiValued = false)
50 String egressDeviceString = null;
51
Ray Milkeyac1441d2014-11-13 09:31:47 -080052
Ray Milkeya058c732014-10-08 13:52:34 -070053 @Override
54 protected void execute() {
55 IntentService service = get(IntentService.class);
56
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
58 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070059 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
60
Thomas Vachuskab97cf282014-10-20 23:31:12 -070061 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
62 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070063 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
64
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070065 TrafficSelector selector = buildTrafficSelector();
Ray Milkeyac1441d2014-11-13 09:31:47 -080066 TrafficTreatment treatment = buildTrafficTreatment();
Ray Milkeya058c732014-10-08 13:52:34 -070067
Ray Milkey460f4022014-11-05 15:41:43 -080068 List<Constraint> constraints = buildConstraints();
69
Thomas Vachuskab97cf282014-10-20 23:31:12 -070070 Intent intent = new PointToPointIntent(appId(), selector, treatment,
Ray Milkey460f4022014-11-05 15:41:43 -080071 ingress, egress, constraints);
Ray Milkeya058c732014-10-08 13:52:34 -070072 service.submit(intent);
73 }
74
75 /**
76 * Extracts the port number portion of the ConnectPoint.
77 *
78 * @param deviceString string representing the device/port
79 * @return port number as a string, empty string if the port is not found
80 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070081 public static String getPortNumber(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -070082 int slash = deviceString.indexOf('/');
83 if (slash <= 0) {
84 return "";
85 }
86 return deviceString.substring(slash + 1, deviceString.length());
87 }
88
89 /**
90 * Extracts the device ID portion of the ConnectPoint.
91 *
92 * @param deviceString string representing the device/port
93 * @return device ID string
94 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070095 public static String getDeviceId(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -070096 int slash = deviceString.indexOf('/');
97 if (slash <= 0) {
98 return "";
99 }
100 return deviceString.substring(0, slash);
101 }
102}