blob: d07b2aa72eba71b548b01871485f7db0963a29d0 [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 Milkeya058c732014-10-08 13:52:34 -070019package org.onlab.onos.cli.net;
20
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Ray Milkeya058c732014-10-08 13:52:34 -070023import org.onlab.onos.net.ConnectPoint;
24import org.onlab.onos.net.DeviceId;
25import org.onlab.onos.net.PortNumber;
Ray Milkeya058c732014-10-08 13:52:34 -070026import org.onlab.onos.net.flow.TrafficSelector;
27import org.onlab.onos.net.flow.TrafficTreatment;
28import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070029import org.onlab.onos.net.intent.IntentService;
30import org.onlab.onos.net.intent.PointToPointIntent;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070031
32import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Ray Milkeya058c732014-10-08 13:52:34 -070033
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034import static org.onlab.onos.net.DeviceId.deviceId;
35import static org.onlab.onos.net.PortNumber.portNumber;
36
Ray Milkeya058c732014-10-08 13:52:34 -070037/**
38 * Installs point-to-point connectivity intents.
39 */
40@Command(scope = "onos", name = "add-point-intent",
41 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070042public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070043
44 @Argument(index = 0, name = "ingressDevice",
45 description = "Ingress Device/Port Description",
46 required = true, multiValued = false)
47 String ingressDeviceString = null;
48
49 @Argument(index = 1, name = "egressDevice",
50 description = "Egress Device/Port Description",
51 required = true, multiValued = false)
52 String egressDeviceString = null;
53
Ray Milkeya058c732014-10-08 13:52:34 -070054 @Override
55 protected void execute() {
56 IntentService service = get(IntentService.class);
57
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
59 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070060 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
61
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
63 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070064 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
65
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070066 TrafficSelector selector = buildTrafficSelector();
67 TrafficTreatment treatment = builder().build();
Ray Milkeya058c732014-10-08 13:52:34 -070068
Thomas Vachuskab97cf282014-10-20 23:31:12 -070069 Intent intent = new PointToPointIntent(appId(), selector, treatment,
70 ingress, egress);
Ray Milkeya058c732014-10-08 13:52:34 -070071 service.submit(intent);
72 }
73
74 /**
75 * Extracts the port number portion of the ConnectPoint.
76 *
77 * @param deviceString string representing the device/port
78 * @return port number as a string, empty string if the port is not found
79 */
80 private String getPortNumber(String deviceString) {
81 int slash = deviceString.indexOf('/');
82 if (slash <= 0) {
83 return "";
84 }
85 return deviceString.substring(slash + 1, deviceString.length());
86 }
87
88 /**
89 * Extracts the device ID portion of the ConnectPoint.
90 *
91 * @param deviceString string representing the device/port
92 * @return device ID string
93 */
94 private String getDeviceId(String deviceString) {
95 int slash = deviceString.indexOf('/');
96 if (slash <= 0) {
97 return "";
98 }
99 return deviceString.substring(0, slash);
100 }
101}