blob: 1cd695ed22cb3129419421d0bfaf9f61e957fa59 [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 Milkey0742ec92014-10-13 08:39:55 -070016package org.onlab.onos.cli.net;
17
Ray Milkey0742ec92014-10-13 08:39:55 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Ray Milkey0742ec92014-10-13 08:39:55 -070020import org.onlab.onos.net.ConnectPoint;
21import org.onlab.onos.net.DeviceId;
22import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070023import org.onlab.onos.net.flow.DefaultTrafficTreatment;
24import org.onlab.onos.net.flow.TrafficSelector;
25import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080026import org.onlab.onos.net.intent.Constraint;
Ray Milkey0742ec92014-10-13 08:39:55 -070027import org.onlab.onos.net.intent.Intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070028import org.onlab.onos.net.intent.IntentService;
29import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070030
Thomas Vachuskab97cf282014-10-20 23:31:12 -070031import java.util.HashSet;
Ray Milkey460f4022014-11-05 15:41:43 -080032import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070033import java.util.Set;
34
35import static org.onlab.onos.net.DeviceId.deviceId;
36import static org.onlab.onos.net.PortNumber.portNumber;
37
Ray Milkey0742ec92014-10-13 08:39:55 -070038/**
39 * Installs point-to-point connectivity intents.
40 */
41@Command(scope = "onos", name = "add-multi-to-single-intent",
42 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070043public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070044
45 @Argument(index = 0, name = "ingressDevices",
46 description = "Ingress Device/Port Description",
47 required = true, multiValued = true)
48 String[] deviceStrings = null;
49
Ray Milkey0742ec92014-10-13 08:39:55 -070050 @Override
51 protected void execute() {
52 IntentService service = get(IntentService.class);
53
54 if (deviceStrings.length < 2) {
55 return;
56 }
57
58 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070059 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
60 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070061 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
62 Set<ConnectPoint> ingressPoints = new HashSet<>();
63
64 for (int index = 0; index < deviceStrings.length - 1; index++) {
65 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070066 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
67 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070068 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
69 ingressPoints.add(ingress);
70 }
71
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070072 TrafficSelector selector = buildTrafficSelector();
Ray Milkey0742ec92014-10-13 08:39:55 -070073 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Ray Milkey460f4022014-11-05 15:41:43 -080074 List<Constraint> constraints = buildConstraints();
Ray Milkey0742ec92014-10-13 08:39:55 -070075
Thomas Vachuskab97cf282014-10-20 23:31:12 -070076 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
Ray Milkey460f4022014-11-05 15:41:43 -080077 ingressPoints, egress,
78 constraints);
Ray Milkey0742ec92014-10-13 08:39:55 -070079 service.submit(intent);
80 }
81
82 /**
83 * Extracts the port number portion of the ConnectPoint.
84 *
85 * @param deviceString string representing the device/port
86 * @return port number as a string, empty string if the port is not found
87 */
88 private String getPortNumber(String deviceString) {
89 int slash = deviceString.indexOf('/');
90 if (slash <= 0) {
91 return "";
92 }
93 return deviceString.substring(slash + 1, deviceString.length());
94 }
95
96 /**
97 * Extracts the device ID portion of the ConnectPoint.
98 *
99 * @param deviceString string representing the device/port
100 * @return device ID string
101 */
102 private String getDeviceId(String deviceString) {
103 int slash = deviceString.indexOf('/');
104 if (slash <= 0) {
105 return "";
106 }
107 return deviceString.substring(0, slash);
108 }
109}