Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.net.Device; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 23 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficSelector; |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 25 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.ExtensionPropertyException; |
| 27 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 28 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 29 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 30 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Provides common methods to help populating flow rules for SONA applications. |
| 38 | */ |
| 39 | public 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 | /** |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 77 | * Adds flow rules with the supplied information. |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 78 | * |
| 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 |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 83 | * @param treatment traffic treatment |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 84 | * @param flag flag |
| 85 | * @param priority priority |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 86 | * @param install populate flows if true, remove them otherwise |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 87 | */ |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 88 | 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 Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 97 | .withSelector(selector) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 98 | .withTreatment(treatment) |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 99 | .withFlag(flag) |
| 100 | .withPriority(priority) |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 101 | .fromApp(appId); |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 102 | |
sangho | fb3b501 | 2016-11-10 15:47:53 +0900 | [diff] [blame^] | 103 | if (install) { |
| 104 | flowObjectiveService.forward(deviceId, foBuilder.add()); |
| 105 | } else { |
| 106 | flowObjectiveService.forward(deviceId, foBuilder.remove()); |
| 107 | } |
Hyunsun Moon | b3eb84d | 2016-07-27 19:10:52 -0700 | [diff] [blame] | 108 | } |
| 109 | } |