blob: 5185d658698726ffda16a3b134248d8a163d4d8f [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",
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();
71 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
72 List<Constraint> constraints = buildConstraints();
73
Ray Milkey5b3717e2015-02-05 11:44:08 -080074 SinglePointToMultiPointIntent intent =
75 new SinglePointToMultiPointIntent(
76 appId(),
77 key(),
78 selector,
79 treatment,
80 ingressPoint,
81 egressPoints,
82 constraints);
Michele Santuari4a338072014-11-05 18:38:55 +010083 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -080084 print("Single point to multipoint intent submitted:\n%s", intent.toString());
Michele Santuari4a338072014-11-05 18:38:55 +010085 }
86
87 /**
88 * Extracts the port number portion of the ConnectPoint.
89 *
90 * @param deviceString string representing the device/port
91 * @return port number as a string, empty string if the port is not found
92 */
93 private String getPortNumber(String deviceString) {
94 int slash = deviceString.indexOf('/');
95 if (slash <= 0) {
96 return "";
97 }
98 return deviceString.substring(slash + 1, deviceString.length());
99 }
100
101 /**
102 * Extracts the device ID portion of the ConnectPoint.
103 *
104 * @param deviceString string representing the device/port
105 * @return device ID string
106 */
107 private String getDeviceId(String deviceString) {
108 int slash = deviceString.indexOf('/');
109 if (slash <= 0) {
110 return "";
111 }
112 return deviceString.substring(0, slash);
113 }
114
115}