blob: 4cb43b9e7effd541bfcac0d5f40c121afb30983a [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
Ray Milkey460f4022014-11-05 15:41:43 -080018import java.util.List;
19
Ray Milkeya058c732014-10-08 13:52:34 -070020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
Ray Milkeyac1441d2014-11-13 09:31:47 -080022import org.apache.karaf.shell.commands.Option;
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;
Ray Milkey460f4022014-11-05 15:41:43 -080028import org.onlab.onos.net.intent.Constraint;
Ray Milkeya058c732014-10-08 13:52:34 -070029import org.onlab.onos.net.intent.Intent;
Ray Milkeya058c732014-10-08 13:52:34 -070030import org.onlab.onos.net.intent.IntentService;
31import org.onlab.onos.net.intent.PointToPointIntent;
Ray Milkeyac1441d2014-11-13 09:31:47 -080032import org.onlab.packet.MacAddress;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070033
Ray Milkeyac1441d2014-11-13 09:31:47 -080034import static com.google.common.base.Strings.isNullOrEmpty;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070035import static org.onlab.onos.net.DeviceId.deviceId;
36import static org.onlab.onos.net.PortNumber.portNumber;
Ray Milkeyac1441d2014-11-13 09:31:47 -080037import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070038
Ray Milkeya058c732014-10-08 13:52:34 -070039/**
40 * Installs point-to-point connectivity intents.
41 */
42@Command(scope = "onos", name = "add-point-intent",
43 description = "Installs point-to-point connectivity intent")
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070044public class AddPointToPointIntentCommand extends ConnectivityIntentCommand {
Ray Milkeya058c732014-10-08 13:52:34 -070045
46 @Argument(index = 0, name = "ingressDevice",
47 description = "Ingress Device/Port Description",
48 required = true, multiValued = false)
49 String ingressDeviceString = null;
50
51 @Argument(index = 1, name = "egressDevice",
52 description = "Egress Device/Port Description",
53 required = true, multiValued = false)
54 String egressDeviceString = null;
55
Ray Milkeyac1441d2014-11-13 09:31:47 -080056 @Option(name = "--srcMacRewrite", description = "Source MAC address to rewrite",
57 required = false, multiValued = false)
58 private String rewriteSrcMacAddressString = null;
59
60 @Option(name = "--dstMacRewrite", description = "Destination MAC address to rewrite",
61 required = false, multiValued = false)
62 private String rewriteDstMacAddressString = null;
63
64
65 /**
66 * Generates a traffic treatment for this intent. If the mac address rewrite
67 * argument is specified the treatment is updated
68 * to implement the rewrite rule if necessary.
69 */
70 private TrafficTreatment buildTrafficTreatment() {
71 final TrafficTreatment.Builder builder = builder();
72
73 if (!isNullOrEmpty(rewriteSrcMacAddressString)) {
74 final MacAddress rewriteSrcMacAddress =
75 MacAddress.valueOf(rewriteSrcMacAddressString);
76 builder.setEthSrc(rewriteSrcMacAddress);
77 }
78 if (!isNullOrEmpty(rewriteDstMacAddressString)) {
79 final MacAddress rewriteDstMacAddress =
80 MacAddress.valueOf(rewriteDstMacAddressString);
81 builder.setEthDst(rewriteDstMacAddress);
82 }
83 return builder.build();
84 }
85
Ray Milkeya058c732014-10-08 13:52:34 -070086 @Override
87 protected void execute() {
88 IntentService service = get(IntentService.class);
89
Thomas Vachuskab97cf282014-10-20 23:31:12 -070090 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
91 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070092 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
93
Thomas Vachuskab97cf282014-10-20 23:31:12 -070094 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
95 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Ray Milkeya058c732014-10-08 13:52:34 -070096 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
97
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070098 TrafficSelector selector = buildTrafficSelector();
Ray Milkeyac1441d2014-11-13 09:31:47 -080099 TrafficTreatment treatment = buildTrafficTreatment();
Ray Milkeya058c732014-10-08 13:52:34 -0700100
Ray Milkey460f4022014-11-05 15:41:43 -0800101 List<Constraint> constraints = buildConstraints();
102
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700103 Intent intent = new PointToPointIntent(appId(), selector, treatment,
Ray Milkey460f4022014-11-05 15:41:43 -0800104 ingress, egress, constraints);
Ray Milkeya058c732014-10-08 13:52:34 -0700105 service.submit(intent);
106 }
107
108 /**
109 * Extracts the port number portion of the ConnectPoint.
110 *
111 * @param deviceString string representing the device/port
112 * @return port number as a string, empty string if the port is not found
113 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700114 public static String getPortNumber(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -0700115 int slash = deviceString.indexOf('/');
116 if (slash <= 0) {
117 return "";
118 }
119 return deviceString.substring(slash + 1, deviceString.length());
120 }
121
122 /**
123 * Extracts the device ID portion of the ConnectPoint.
124 *
125 * @param deviceString string representing the device/port
126 * @return device ID string
127 */
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700128 public static String getDeviceId(String deviceString) {
Ray Milkeya058c732014-10-08 13:52:34 -0700129 int slash = deviceString.indexOf('/');
130 if (slash <= 0) {
131 return "";
132 }
133 return deviceString.substring(0, slash);
134 }
135}