blob: 4ca0ebd4a5ff95f679bf9cfd0cf47ef357469b66 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Ray Milkeya058c732014-10-08 13:52:34 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.intent.Constraint;
28import org.onosproject.net.intent.Intent;
29import org.onosproject.net.intent.IntentService;
30import org.onosproject.net.intent.PointToPointIntent;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070031
Ray Milkey5b3717e2015-02-05 11:44:08 -080032import static org.onosproject.net.DeviceId.deviceId;
33import static org.onosproject.net.PortNumber.portNumber;
34
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
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070070 Intent intent = PointToPointIntent.builder()
71 .appId(appId())
72 .key(key())
73 .selector(selector)
74 .treatment(treatment)
75 .ingressPoint(ingress)
76 .egressPoint(egress)
77 .constraints(constraints)
78 .priority(priority())
79 .build();
Ray Milkeya058c732014-10-08 13:52:34 -070080 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080081 print("Point to point intent submitted:\n%s", intent.toString());
Ray Milkeya058c732014-10-08 13:52:34 -070082 }
83
84 /**
85 * Extracts the port number portion of the ConnectPoint.
86 *
87 * @param deviceString string representing the device/port
88 * @return port number as a string, empty string if the port is not found
89 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070090 public static String getPortNumber(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -070091 int slash = deviceString.indexOf('/');
92 if (slash <= 0) {
93 return "";
94 }
95 return deviceString.substring(slash + 1, deviceString.length());
96 }
97
98 /**
99 * Extracts the device ID portion of the ConnectPoint.
100 *
101 * @param deviceString string representing the device/port
102 * @return device ID string
103 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700104 public static String getDeviceId(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -0700105 int slash = deviceString.indexOf('/');
106 if (slash <= 0) {
107 return "";
108 }
109 return deviceString.substring(0, slash);
110 }
111}