blob: 9e8e2fc4749313fca17ea32e9974e41dd2b32030 [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;
26import org.onlab.onos.net.intent.Intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070027import org.onlab.onos.net.intent.IntentService;
28import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070029
Thomas Vachuskab97cf282014-10-20 23:31:12 -070030import java.util.HashSet;
31import java.util.Set;
32
33import static org.onlab.onos.net.DeviceId.deviceId;
34import static org.onlab.onos.net.PortNumber.portNumber;
35
Ray Milkey0742ec92014-10-13 08:39:55 -070036/**
37 * Installs point-to-point connectivity intents.
38 */
39@Command(scope = "onos", name = "add-multi-to-single-intent",
40 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070041public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070042
43 @Argument(index = 0, name = "ingressDevices",
44 description = "Ingress Device/Port Description",
45 required = true, multiValued = true)
46 String[] deviceStrings = null;
47
Ray Milkey0742ec92014-10-13 08:39:55 -070048 @Override
49 protected void execute() {
50 IntentService service = get(IntentService.class);
51
52 if (deviceStrings.length < 2) {
53 return;
54 }
55
56 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
58 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070059 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
60 Set<ConnectPoint> ingressPoints = new HashSet<>();
61
62 for (int index = 0; index < deviceStrings.length - 1; index++) {
63 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
65 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070066 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
67 ingressPoints.add(ingress);
68 }
69
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070070 TrafficSelector selector = buildTrafficSelector();
Ray Milkey0742ec92014-10-13 08:39:55 -070071 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
72
Thomas Vachuskab97cf282014-10-20 23:31:12 -070073 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
74 ingressPoints, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070075 service.submit(intent);
76 }
77
78 /**
79 * Extracts the port number portion of the ConnectPoint.
80 *
81 * @param deviceString string representing the device/port
82 * @return port number as a string, empty string if the port is not found
83 */
84 private String getPortNumber(String deviceString) {
85 int slash = deviceString.indexOf('/');
86 if (slash <= 0) {
87 return "";
88 }
89 return deviceString.substring(slash + 1, deviceString.length());
90 }
91
92 /**
93 * Extracts the device ID portion of the ConnectPoint.
94 *
95 * @param deviceString string representing the device/port
96 * @return device ID string
97 */
98 private String getDeviceId(String deviceString) {
99 int slash = deviceString.indexOf('/');
100 if (slash <= 0) {
101 return "";
102 }
103 return deviceString.substring(0, slash);
104 }
105}