blob: cdf4a739afd6c9093aec544efc74c9d55e31fb41 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08003 *
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
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080037/**
38 * Installs connectivity intent between a single ingress device and multiple egress devices.
39 */
Michele Santuari4a338072014-11-05 18:38:55 +010040@Command(scope = "onos", name = "add-single-to-multi-intent",
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070041 description = "Installs connectivity intent between a single ingress device and multiple egress devices")
Michele Santuari4a338072014-11-05 18:38:55 +010042public class AddSinglePointToMultiPointIntentCommand extends ConnectivityIntentCommand {
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070043 @Argument(index = 0, name = "ingressDevice egressDevices",
44 description = "ingressDevice/Port egressDevice/Port...egressDevice/Port",
Michele Santuari4a338072014-11-05 18:38:55 +010045 required = true, multiValued = true)
46 String[] deviceStrings = null;
47
48 @Override
49 protected void execute() {
50 IntentService service = get(IntentService.class);
51
52 if (deviceStrings.length < 2) {
53 return;
54 }
55
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070056 String ingressDeviceString = deviceStrings[0];
Michele Santuari4a338072014-11-05 18:38:55 +010057 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
58 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
59 ConnectPoint ingressPoint = new ConnectPoint(ingressDeviceId,
60 ingressPortNumber);
61
62 Set<ConnectPoint> egressPoints = new HashSet<>();
Pavlin Radoslavov70761df2015-03-09 11:05:49 -070063 for (int index = 1; index < deviceStrings.length; index++) {
Michele Santuari4a338072014-11-05 18:38:55 +010064 String egressDeviceString = deviceStrings[index];
65 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
66 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
67 ConnectPoint egress = new ConnectPoint(egressDeviceId,
68 egressPortNumber);
69 egressPoints.add(egress);
70 }
71
72 TrafficSelector selector = buildTrafficSelector();
Brian O'Connor6b528132015-03-10 16:39:52 -070073 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Michele Santuari4a338072014-11-05 18:38:55 +010074 List<Constraint> constraints = buildConstraints();
75
Ray Milkey5b3717e2015-02-05 11:44:08 -080076 SinglePointToMultiPointIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070077 SinglePointToMultiPointIntent.builder()
78 .appId(appId())
79 .key(key())
80 .selector(selector)
81 .treatment(treatment)
82 .ingressPoint(ingressPoint)
83 .egressPoints(egressPoints)
84 .constraints(constraints)
85 .priority(priority())
86 .build();
Michele Santuari4a338072014-11-05 18:38:55 +010087 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080088 print("Single point to multipoint intent submitted:\n%s", intent.toString());
Michele Santuari4a338072014-11-05 18:38:55 +010089 }
90
91 /**
92 * Extracts the port number portion of the ConnectPoint.
93 *
94 * @param deviceString string representing the device/port
95 * @return port number as a string, empty string if the port is not found
96 */
97 private String getPortNumber(String deviceString) {
98 int slash = deviceString.indexOf('/');
99 if (slash <= 0) {
100 return "";
101 }
102 return deviceString.substring(slash + 1, deviceString.length());
103 }
104
105 /**
106 * Extracts the device ID portion of the ConnectPoint.
107 *
108 * @param deviceString string representing the device/port
109 * @return device ID string
110 */
111 private String getDeviceId(String deviceString) {
112 int slash = deviceString.indexOf('/');
113 if (slash <= 0) {
114 return "";
115 }
116 return deviceString.substring(0, slash);
117 }
118
119}