alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 17 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 18 | import java.util.HashSet; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.intent.Constraint; |
| 31 | import org.onosproject.net.intent.IntentService; |
| 32 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 33 | |
Jonathan Hart | 9a4157b | 2015-02-05 10:19:29 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.DeviceId.deviceId; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
| 36 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 37 | |
| 38 | @Command(scope = "onos", name = "add-single-to-multi-intent", |
Pavlin Radoslavov | 70761df | 2015-03-09 11:05:49 -0700 | [diff] [blame] | 39 | description = "Installs connectivity intent between a single ingress device and multiple egress devices") |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 40 | public class AddSinglePointToMultiPointIntentCommand extends ConnectivityIntentCommand { |
Pavlin Radoslavov | 70761df | 2015-03-09 11:05:49 -0700 | [diff] [blame] | 41 | @Argument(index = 0, name = "ingressDevice egressDevices", |
| 42 | description = "ingressDevice/Port egressDevice/Port...egressDevice/Port", |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 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 | |
Pavlin Radoslavov | 70761df | 2015-03-09 11:05:49 -0700 | [diff] [blame] | 54 | String ingressDeviceString = deviceStrings[0]; |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 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<>(); |
Pavlin Radoslavov | 70761df | 2015-03-09 11:05:49 -0700 | [diff] [blame] | 61 | for (int index = 1; index < deviceStrings.length; index++) { |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 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(); |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 71 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 72 | List<Constraint> constraints = buildConstraints(); |
| 73 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 74 | SinglePointToMultiPointIntent intent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame^] | 75 | 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 Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 85 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 86 | print("Single point to multipoint intent submitted:\n%s", intent.toString()); |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 87 | } |
| 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 | } |