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; |
| 24 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 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 | /** |
| 77 | * Removes flow rules with the supplied information. |
| 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 |
| 83 | * @param flag flag |
| 84 | * @param priority priority |
| 85 | */ |
| 86 | public static void removeRule(FlowObjectiveService flowObjectiveService, |
| 87 | ApplicationId appId, |
| 88 | DeviceId deviceId, |
| 89 | TrafficSelector selector, |
| 90 | ForwardingObjective.Flag flag, |
| 91 | int priority) { |
| 92 | ForwardingObjective fo = DefaultForwardingObjective.builder() |
| 93 | .withSelector(selector) |
| 94 | .withTreatment(DefaultTrafficTreatment.builder().build()) |
| 95 | .withFlag(flag) |
| 96 | .withPriority(priority) |
| 97 | .fromApp(appId) |
| 98 | .remove(); |
| 99 | |
| 100 | flowObjectiveService.forward(deviceId, fo); |
| 101 | } |
| 102 | } |