blob: 83ef66f9603a3fde60109294f3d1f164b0f51319 [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
Ray Milkeyc24cde32015-03-10 18:20:18 -070018import java.util.HashSet;
19import java.util.List;
20import java.util.Set;
21
Michele Santuari4a338072014-11-05 18:38:55 +010022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.intent.Constraint;
31import org.onosproject.net.intent.IntentService;
32import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Michele Santuari4a338072014-11-05 18:38:55 +010033
Jonathan Hart9a4157b2015-02-05 10:19:29 -080034import 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",
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070039 description = "Installs connectivity intent between a single ingress device and multiple egress devices")
Michele Santuari4a338072014-11-05 18:38:55 +010040public class AddSinglePointToMultiPointIntentCommand extends ConnectivityIntentCommand {
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070041 @Argument(index = 0, name = "ingressDevice egressDevices",
42 description = "ingressDevice/Port egressDevice/Port...egressDevice/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
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070054 String ingressDeviceString = deviceStrings[0];
Michele Santuari4a338072014-11-05 18:38:55 +010055 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<>();
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070061 for (int index = 1; index < deviceStrings.length; index++) {
Michele Santuari4a338072014-11-05 18:38:55 +010062 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();
Brian O'Connor6b528132015-03-10 16:39:52 -070071 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Michele Santuari4a338072014-11-05 18:38:55 +010072 List<Constraint> constraints = buildConstraints();
73
Ray Milkey5b3717e2015-02-05 11:44:08 -080074 SinglePointToMultiPointIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070075 SinglePointToMultiPointIntent.builder()
76 .appId(appId())
77 .key(key())
78 .selector(selector)
79 .treatment(treatment)
80 .ingressPoint(ingressPoint)
81 .egressPoints(egressPoints)
82 .constraints(constraints)
83 .priority(priority())
84 .build();
Michele Santuari4a338072014-11-05 18:38:55 +010085 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080086 print("Single point to multipoint intent submitted:\n%s", intent.toString());
Michele Santuari4a338072014-11-05 18:38:55 +010087 }
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
117}