lishuai | 38e0758 | 2015-11-26 20:00:14 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
lishuai | 38e0758 | 2015-11-26 20:00:14 +0800 | [diff] [blame] | 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.vtn.table.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 34 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 37 | import org.onosproject.net.flowobjective.Objective; |
| 38 | import org.onosproject.vtn.table.DnatService; |
| 39 | import org.onosproject.vtnrsc.SegmentationId; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | /** |
| 43 | * Provides implementation of DnatService. |
| 44 | */ |
| 45 | public class DnatServiceImpl implements DnatService { |
| 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | private static final int DNAT_PRIORITY = 0xffff; |
| 49 | private static final int PREFIX_LENGTH = 32; |
| 50 | |
| 51 | private final FlowObjectiveService flowObjectiveService; |
| 52 | private final ApplicationId appId; |
| 53 | |
| 54 | /** |
| 55 | * Construct a DnatServiceImpl object. |
| 56 | * |
| 57 | * @param appId the application id of vtn |
| 58 | */ |
| 59 | public DnatServiceImpl(ApplicationId appId) { |
| 60 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 61 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 62 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void programRules(DeviceId deviceId, IpAddress dstIp, |
| 67 | MacAddress ethSrc, IpAddress ipDst, |
| 68 | SegmentationId actionVni, Objective.Operation type) { |
| 69 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 70 | .matchEthType(Ethernet.TYPE_IPV4) |
| 71 | .matchIPDst(IpPrefix.valueOf(dstIp, PREFIX_LENGTH)).build(); |
| 72 | |
| 73 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 74 | treatment.setEthSrc(ethSrc).setIpDst(ipDst) |
| 75 | .setTunnelId(Long.parseLong(actionVni.segmentationId())); |
| 76 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 77 | .builder().withTreatment(treatment.build()) |
| 78 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) |
| 79 | .withPriority(DNAT_PRIORITY); |
| 80 | if (type.equals(Objective.Operation.ADD)) { |
| 81 | log.debug("RouteRules-->ADD"); |
| 82 | flowObjectiveService.forward(deviceId, objective.add()); |
| 83 | } else { |
| 84 | log.debug("RouteRules-->REMOVE"); |
| 85 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 86 | } |
| 87 | } |
| 88 | } |