blob: 8d962a69fe49e84401c1d8fcb8bd808d25a9e5a2 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Michele Santuari4a338072014-11-05 18:38:55 +010017
Michele Santuari4a338072014-11-05 18:38:55 +010018import 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.DefaultTrafficTreatment;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.intent.Constraint;
27import org.onosproject.net.intent.IntentService;
28import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Michele Santuari4a338072014-11-05 18:38:55 +010029
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
Michele Santuari4a338072014-11-05 18:38:55 +010037
38@Command(scope = "onos", name = "add-single-to-multi-intent",
39 description = "Installs connectivity intent between multiple egress devices and a single ingress device")
40public class AddSinglePointToMultiPointIntentCommand extends ConnectivityIntentCommand {
41 @Argument(index = 0, name = "egressDevices ingressDevice",
Jonathan Hart9a4157b2015-02-05 10:19:29 -080042 description = "egressDevice/Port...egressDevice/Port ingressDevice/port",
Michele Santuari4a338072014-11-05 18:38:55 +010043 required = true, multiValued = true)
44 String[] deviceStrings = null;
45
46 @Override
47 protected void execute() {
48 IntentService service = get(IntentService.class);
49
50 if (deviceStrings.length < 2) {
51 return;
52 }
53
54 String ingressDeviceString = deviceStrings[deviceStrings.length - 1];
55 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
56 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
57 ConnectPoint ingressPoint = new ConnectPoint(ingressDeviceId,
58 ingressPortNumber);
59
60 Set<ConnectPoint> egressPoints = new HashSet<>();
61 for (int index = 0; index < deviceStrings.length - 1; index++) {
62 String egressDeviceString = deviceStrings[index];
63 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
64 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
65 ConnectPoint egress = new ConnectPoint(egressDeviceId,
66 egressPortNumber);
67 egressPoints.add(egress);
68 }
69
70 TrafficSelector selector = buildTrafficSelector();
71 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
72 List<Constraint> constraints = buildConstraints();
73
74 SinglePointToMultiPointIntent intent = new SinglePointToMultiPointIntent(
75 appId(),
76 selector,
77 treatment,
78 ingressPoint,
79 egressPoints,
80 constraints);
81 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080082 print("Single point to multipoint intent submitted:\n%s", intent.toString());
Michele Santuari4a338072014-11-05 18:38:55 +010083 }
84
85 /**
86 * Extracts the port number portion of the ConnectPoint.
87 *
88 * @param deviceString string representing the device/port
89 * @return port number as a string, empty string if the port is not found
90 */
91 private String getPortNumber(String deviceString) {
92 int slash = deviceString.indexOf('/');
93 if (slash <= 0) {
94 return "";
95 }
96 return deviceString.substring(slash + 1, deviceString.length());
97 }
98
99 /**
100 * Extracts the device ID portion of the ConnectPoint.
101 *
102 * @param deviceString string representing the device/port
103 * @return device ID string
104 */
105 private String getDeviceId(String deviceString) {
106 int slash = deviceString.indexOf('/');
107 if (slash <= 0) {
108 return "";
109 }
110 return deviceString.substring(0, slash);
111 }
112
113}