blob: e1ede9fb07b995e2a884ed8f7cd688bbcfac68a9 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ray Milkeya058c732014-10-08 13:52:34 -070016package org.onlab.onos.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Ray Milkeya058c732014-10-08 13:52:34 -070020import org.onlab.onos.net.ConnectPoint;
21import org.onlab.onos.net.DeviceId;
22import org.onlab.onos.net.PortNumber;
Ray Milkeya058c732014-10-08 13:52:34 -070023import org.onlab.onos.net.flow.TrafficSelector;
24import org.onlab.onos.net.flow.TrafficTreatment;
25import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070026import org.onlab.onos.net.intent.IntentService;
27import org.onlab.onos.net.intent.PointToPointIntent;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070028
29import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeya058c732014-10-08 13:52:34 -070030
Thomas Vachuskab97cf282014-10-20 23:31:12 -070031import static org.onlab.onos.net.DeviceId.deviceId;
32import static org.onlab.onos.net.PortNumber.portNumber;
33
Ray Milkeya058c732014-10-08 13:52:34 -070034/**
35 * Installs point-to-point connectivity intents.
36 */
37@Command(scope = "onos", name = "add-point-intent",
38 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070039public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070040
41 @Argument(index = 0, name = "ingressDevice",
42 description = "Ingress Device/Port Description",
43 required = true, multiValued = false)
44 String ingressDeviceString = null;
45
46 @Argument(index = 1, name = "egressDevice",
47 description = "Egress Device/Port Description",
48 required = true, multiValued = false)
49 String egressDeviceString = null;
50
Ray Milkeya058c732014-10-08 13:52:34 -070051 @Override
52 protected void execute() {
53 IntentService service = get(IntentService.class);
54
Thomas Vachuskab97cf282014-10-20 23:31:12 -070055 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
56 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070057 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
58
Thomas Vachuskab97cf282014-10-20 23:31:12 -070059 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
60 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070061 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
62
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070063 TrafficSelector selector = buildTrafficSelector();
64 TrafficTreatment treatment = builder().build();
Ray Milkeya058c732014-10-08 13:52:34 -070065
Thomas Vachuskab97cf282014-10-20 23:31:12 -070066 Intent intent = new PointToPointIntent(appId(), selector, treatment,
67 ingress, egress);
Ray Milkeya058c732014-10-08 13:52:34 -070068 service.submit(intent);
69 }
70
71 /**
72 * Extracts the port number portion of the ConnectPoint.
73 *
74 * @param deviceString string representing the device/port
75 * @return port number as a string, empty string if the port is not found
76 */
77 private String getPortNumber(String deviceString) {
78 int slash = deviceString.indexOf('/');
79 if (slash <= 0) {
80 return "";
81 }
82 return deviceString.substring(slash + 1, deviceString.length());
83 }
84
85 /**
86 * Extracts the device ID portion of the ConnectPoint.
87 *
88 * @param deviceString string representing the device/port
89 * @return device ID string
90 */
91 private String getDeviceId(String deviceString) {
92 int slash = deviceString.indexOf('/');
93 if (slash <= 0) {
94 return "";
95 }
96 return deviceString.substring(0, slash);
97 }
98}