blob: e813fd99ebf061100edaa806612c6ec4d0334922 [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
Brian O'Connorabafb502014-12-02 22:26:20 -080018import static org.onosproject.net.DeviceId.deviceId;
19import static org.onosproject.net.PortNumber.portNumber;
Michele Santuari4a338072014-11-05 18:38:55 +010020
21import java.util.HashSet;
22import java.util.List;
23import java.util.Set;
24
25import org.apache.karaf.shell.commands.Argument;
26import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Constraint;
34import org.onosproject.net.intent.IntentService;
35import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Michele Santuari4a338072014-11-05 18:38:55 +010036
37
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",
42 description = "egress Device/Port...egress Device/Port ingressDevice/port",
43 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);
82 }
83
84 /**
85 * Extracts the port number portion of the ConnectPoint.
86 *
87 * @param deviceString string representing the device/port
88 * @return port number as a string, empty string if the port is not found
89 */
90 private String getPortNumber(String deviceString) {
91 int slash = deviceString.indexOf('/');
92 if (slash <= 0) {
93 return "";
94 }
95 return deviceString.substring(slash + 1, deviceString.length());
96 }
97
98 /**
99 * Extracts the device ID portion of the ConnectPoint.
100 *
101 * @param deviceString string representing the device/port
102 * @return device ID string
103 */
104 private String getDeviceId(String deviceString) {
105 int slash = deviceString.indexOf('/');
106 if (slash <= 0) {
107 return "";
108 }
109 return deviceString.substring(0, slash);
110 }
111
112}