blob: 26bb1c0cedbfa16148a3df6753d4257cf47a9378 [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
Ray Milkey460f4022014-11-05 15:41:43 -080018import java.util.List;
19
Ray Milkeya058c732014-10-08 13:52:34 -070020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
Ray Milkeya058c732014-10-08 13:52:34 -070022import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.DeviceId;
24import org.onlab.onos.net.PortNumber;
Ray Milkeya058c732014-10-08 13:52:34 -070025import org.onlab.onos.net.flow.TrafficSelector;
26import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080027import org.onlab.onos.net.intent.Constraint;
Ray Milkeya058c732014-10-08 13:52:34 -070028import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070029import org.onlab.onos.net.intent.IntentService;
30import org.onlab.onos.net.intent.PointToPointIntent;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070031
32import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeya058c732014-10-08 13:52:34 -070033
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034import static org.onlab.onos.net.DeviceId.deviceId;
35import static org.onlab.onos.net.PortNumber.portNumber;
36
Ray Milkeya058c732014-10-08 13:52:34 -070037/**
38 * Installs point-to-point connectivity intents.
39 */
40@Command(scope = "onos", name = "add-point-intent",
41 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070042public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070043
44 @Argument(index = 0, name = "ingressDevice",
45 description = "Ingress Device/Port Description",
46 required = true, multiValued = false)
47 String ingressDeviceString = null;
48
49 @Argument(index = 1, name = "egressDevice",
50 description = "Egress Device/Port Description",
51 required = true, multiValued = false)
52 String egressDeviceString = null;
53
Ray Milkeya058c732014-10-08 13:52:34 -070054 @Override
55 protected void execute() {
56 IntentService service = get(IntentService.class);
57
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
59 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070060 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
61
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
63 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070064 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
65
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070066 TrafficSelector selector = buildTrafficSelector();
67 TrafficTreatment treatment = builder().build();
Ray Milkeya058c732014-10-08 13:52:34 -070068
Ray Milkey460f4022014-11-05 15:41:43 -080069 List<Constraint> constraints = buildConstraints();
70
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 Intent intent = new PointToPointIntent(appId(), selector, treatment,
Ray Milkey460f4022014-11-05 15:41:43 -080072 ingress, egress, constraints);
Ray Milkeya058c732014-10-08 13:52:34 -070073 service.submit(intent);
74 }
75
76 /**
77 * Extracts the port number portion of the ConnectPoint.
78 *
79 * @param deviceString string representing the device/port
80 * @return port number as a string, empty string if the port is not found
81 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070082 public static String getPortNumber(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -070083 int slash = deviceString.indexOf('/');
84 if (slash <= 0) {
85 return "";
86 }
87 return deviceString.substring(slash + 1, deviceString.length());
88 }
89
90 /**
91 * Extracts the device ID portion of the ConnectPoint.
92 *
93 * @param deviceString string representing the device/port
94 * @return device ID string
95 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070096 public static String getDeviceId(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -070097 int slash = deviceString.indexOf('/');
98 if (slash <= 0) {
99 return "";
100 }
101 return deviceString.substring(0, slash);
102 }
103}