blob: bfc99b8c487fb7cd7a9d5728feb0c5486a2d55de [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Ray Milkey0742ec92014-10-13 08:39:55 -070019package org.onlab.onos.cli.net;
20
Ray Milkey0742ec92014-10-13 08:39:55 -070021import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Ray Milkey0742ec92014-10-13 08:39:55 -070023import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.PortNumber;
Ray Milkey0742ec92014-10-13 08:39:55 -070026import org.onlab.onos.net.flow.DefaultTrafficTreatment;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.flow.TrafficTreatment;
29import org.onlab.onos.net.intent.Intent;
Ray Milkey0742ec92014-10-13 08:39:55 -070030import org.onlab.onos.net.intent.IntentService;
31import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
Ray Milkey0742ec92014-10-13 08:39:55 -070032
Thomas Vachuskab97cf282014-10-20 23:31:12 -070033import java.util.HashSet;
34import java.util.Set;
35
36import static org.onlab.onos.net.DeviceId.deviceId;
37import static org.onlab.onos.net.PortNumber.portNumber;
38
Ray Milkey0742ec92014-10-13 08:39:55 -070039/**
40 * Installs point-to-point connectivity intents.
41 */
42@Command(scope = "onos", name = "add-multi-to-single-intent",
43 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070044public class AddMultiPointToSinglePointIntentCommand extends ConnectivityIntentCommand {
Ray Milkey0742ec92014-10-13 08:39:55 -070045
46 @Argument(index = 0, name = "ingressDevices",
47 description = "Ingress Device/Port Description",
48 required = true, multiValued = true)
49 String[] deviceStrings = null;
50
Ray Milkey0742ec92014-10-13 08:39:55 -070051 @Override
52 protected void execute() {
53 IntentService service = get(IntentService.class);
54
55 if (deviceStrings.length < 2) {
56 return;
57 }
58
59 String egressDeviceString = deviceStrings[deviceStrings.length - 1];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
61 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070062 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
63 Set<ConnectPoint> ingressPoints = new HashSet<>();
64
65 for (int index = 0; index < deviceStrings.length - 1; index++) {
66 String ingressDeviceString = deviceStrings[index];
Thomas Vachuskab97cf282014-10-20 23:31:12 -070067 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
68 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkey0742ec92014-10-13 08:39:55 -070069 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
70 ingressPoints.add(ingress);
71 }
72
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070073 TrafficSelector selector = buildTrafficSelector();
Ray Milkey0742ec92014-10-13 08:39:55 -070074 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
75
Thomas Vachuskab97cf282014-10-20 23:31:12 -070076 Intent intent = new MultiPointToSinglePointIntent(appId(), selector, treatment,
77 ingressPoints, egress);
Ray Milkey0742ec92014-10-13 08:39:55 -070078 service.submit(intent);
79 }
80
81 /**
82 * Extracts the port number portion of the ConnectPoint.
83 *
84 * @param deviceString string representing the device/port
85 * @return port number as a string, empty string if the port is not found
86 */
87 private String getPortNumber(String deviceString) {
88 int slash = deviceString.indexOf('/');
89 if (slash <= 0) {
90 return "";
91 }
92 return deviceString.substring(slash + 1, deviceString.length());
93 }
94
95 /**
96 * Extracts the device ID portion of the ConnectPoint.
97 *
98 * @param deviceString string representing the device/port
99 * @return device ID string
100 */
101 private String getDeviceId(String deviceString) {
102 int slash = deviceString.indexOf('/');
103 if (slash <= 0) {
104 return "";
105 }
106 return deviceString.substring(0, slash);
107 }
108}