blob: 892f819ecd000318b3fe72b6081995ccf594e0e5 [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 Milkey0742ec92014-10-13 08:39:55 -070017
Ray Milkey0742ec92014-10-13 08:39:55 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.intent.Constraint;
26import org.onosproject.net.intent.Intent;
27import org.onosproject.net.intent.IntentService;
28import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070029
Jonathan Hart9a4157b2015-02-05 10:19:29 -080030import java.util.HashSet;
31import java.util.List;
32import java.util.Set;
33
34import static org.onosproject.net.DeviceId.deviceId;
35import static org.onosproject.net.PortNumber.portNumber;
36
Ray Milkey0742ec92014-10-13 08:39:55 -070037/**
38 * Installs point-to-point connectivity intents.
39 */
40@Command(scope = "onos", name = "add-multi-to-single-intent",
41 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070042public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070043
Jonathan Hart9a4157b2015-02-05 10:19:29 -080044 @Argument(index = 0, name = "ingressDevices egressDevice",
45 description = "ingressDevice/Port..ingressDevice/Port egressDevice/Port",
Ray Milkey0742ec92014-10-13 08:39:55 -070046 required = true, multiValued = true)
47 String[] deviceStrings = null;
48
Ray Milkey0742ec92014-10-13 08:39:55 -070049 @Override
50 protected void execute() {
51 IntentService service = get(IntentService.class);
52
53 if (deviceStrings.length < 2) {
54 return;
55 }
56
57 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
59 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070060 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
61 Set<ConnectPoint> ingressPoints = new HashSet<>();
62
63 for (int index = 0; index < deviceStrings.length - 1; index++) {
64 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
66 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070067 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
68 ingressPoints.add(ingress);
69 }
70
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070071 TrafficSelector selector = buildTrafficSelector();
Jonathan Hart2c25e362014-11-17 18:14:19 -080072 TrafficTreatment treatment = buildTrafficTreatment();
Ray Milkey460f4022014-11-05 15:41:43 -080073 List<Constraint> constraints = buildConstraints();
Ray Milkey0742ec92014-10-13 08:39:55 -070074
Ray Milkeyebc5d222015-03-18 15:45:36 -070075 Intent intent = MultiPointToSinglePointIntent.builder()
76 .appId(appId())
77 .key(key())
78 .selector(selector)
79 .treatment(treatment)
80 .ingressPoints(ingressPoints)
81 .egressPoint(egress)
82 .constraints(constraints)
83 .priority(priority())
84 .build();
Ray Milkey0742ec92014-10-13 08:39:55 -070085 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080086 print("Multipoint to single point intent submitted:\n%s", intent.toString());
Ray Milkey0742ec92014-10-13 08:39:55 -070087 }
88
89 /**
90 * Extracts the port number portion of the ConnectPoint.
91 *
92 * @param deviceString string representing the device/port
93 * @return port number as a string, empty string if the port is not found
94 */
95 private String getPortNumber(String deviceString) {
96 int slash = deviceString.indexOf('/');
97 if (slash <= 0) {
98 return "";
99 }
100 return deviceString.substring(slash + 1, deviceString.length());
101 }
102
103 /**
104 * Extracts the device ID portion of the ConnectPoint.
105 *
106 * @param deviceString string representing the device/port
107 * @return device ID string
108 */
109 private String getDeviceId(String deviceString) {
110 int slash = deviceString.indexOf('/');
111 if (slash <= 0) {
112 return "";
113 }
114 return deviceString.substring(0, slash);
115 }
116}