blob: 3326417c99759b7f1b84f6527d8bae60b826fea6 [file] [log] [blame]
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -07001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.openstacknetworking;
17
18import org.onlab.packet.Ip4Address;
19import org.onosproject.core.ApplicationId;
20import org.onosproject.net.Device;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
23import org.onosproject.net.device.DeviceService;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070024import org.onosproject.net.flow.TrafficSelector;
sanghofb3b5012016-11-10 15:47:53 +090025import org.onosproject.net.flow.TrafficTreatment;
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070026import org.onosproject.net.flow.instructions.ExtensionPropertyException;
27import org.onosproject.net.flow.instructions.ExtensionTreatment;
28import org.onosproject.net.flowobjective.DefaultForwardingObjective;
29import org.onosproject.net.flowobjective.FlowObjectiveService;
30import org.onosproject.net.flowobjective.ForwardingObjective;
31import org.slf4j.Logger;
32
33import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST;
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Provides common methods to help populating flow rules for SONA applications.
38 */
39public final class RulePopulatorUtil {
40
41 protected static final Logger log = getLogger(RulePopulatorUtil.class);
42
43 private static final String TUNNEL_DST = "tunnelDst";
44
45 private RulePopulatorUtil() {
46 }
47
48 /**
49 * Returns tunnel destination extension treatment object.
50 *
51 * @param deviceService driver service
52 * @param deviceId device id to apply this treatment
53 * @param remoteIp tunnel destination ip address
54 * @return extension treatment
55 */
56 public static ExtensionTreatment buildExtension(DeviceService deviceService,
57 DeviceId deviceId,
58 Ip4Address remoteIp) {
59 Device device = deviceService.getDevice(deviceId);
60 if (device != null && !device.is(ExtensionTreatmentResolver.class)) {
61 log.error("The extension treatment is not supported");
62 return null;
63 }
64
65 ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
66 ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
67 try {
68 treatment.setPropertyValue(TUNNEL_DST, remoteIp);
69 return treatment;
70 } catch (ExtensionPropertyException e) {
71 log.warn("Failed to get tunnelDst extension treatment for {}", deviceId);
72 return null;
73 }
74 }
75
76 /**
sanghofb3b5012016-11-10 15:47:53 +090077 * Adds flow rules with the supplied information.
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070078 *
79 * @param flowObjectiveService flow objective service
80 * @param appId application id
81 * @param deviceId device id to remove this flow rule
82 * @param selector traffic selector
sanghofb3b5012016-11-10 15:47:53 +090083 * @param treatment traffic treatment
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070084 * @param flag flag
85 * @param priority priority
sanghofb3b5012016-11-10 15:47:53 +090086 * @param install populate flows if true, remove them otherwise
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070087 */
sanghofb3b5012016-11-10 15:47:53 +090088 public static void setRule(FlowObjectiveService flowObjectiveService,
89 ApplicationId appId,
90 DeviceId deviceId,
91 TrafficSelector selector,
92 TrafficTreatment treatment,
93 ForwardingObjective.Flag flag,
94 int priority,
95 boolean install) {
96 ForwardingObjective.Builder foBuilder = DefaultForwardingObjective.builder()
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070097 .withSelector(selector)
sanghofb3b5012016-11-10 15:47:53 +090098 .withTreatment(treatment)
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -070099 .withFlag(flag)
100 .withPriority(priority)
sanghofb3b5012016-11-10 15:47:53 +0900101 .fromApp(appId);
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700102
sanghofb3b5012016-11-10 15:47:53 +0900103 if (install) {
104 flowObjectiveService.forward(deviceId, foBuilder.add());
105 } else {
106 flowObjectiveService.forward(deviceId, foBuilder.remove());
107 }
Hyunsun Moonb3eb84d2016-07-27 19:10:52 -0700108 }
109}