lishuai | d269aab | 2015-11-26 19:55:39 +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 | d269aab | 2015-11-26 19:55:39 +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.flow.instructions.Instructions; |
| 34 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 35 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 37 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 38 | import org.onosproject.net.flowobjective.Objective; |
| 39 | import org.onosproject.net.flowobjective.Objective.Operation; |
| 40 | import org.onosproject.vtn.table.L3ForwardService; |
| 41 | import org.onosproject.vtnrsc.SegmentationId; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | /** |
| 45 | * Provides implementation of L3ForwardService. |
| 46 | */ |
| 47 | public class L3ForwardServiceImpl implements L3ForwardService { |
| 48 | private final Logger log = getLogger(getClass()); |
| 49 | |
| 50 | private static final int L3FWD_PRIORITY = 0xffff; |
| 51 | private static final short IP_TYPE = Ethernet.TYPE_IPV4; |
| 52 | private static final int PREFIX_LENGTH = 32; |
| 53 | |
| 54 | private final FlowObjectiveService flowObjectiveService; |
| 55 | private final ApplicationId appId; |
| 56 | |
| 57 | /** |
| 58 | * Construct a L3ForwardServiceImpl object. |
| 59 | * |
| 60 | * @param appId the application id of vtn |
| 61 | */ |
| 62 | public L3ForwardServiceImpl(ApplicationId appId) { |
| 63 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 64 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 65 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void programRouteRules(DeviceId deviceId, SegmentationId l3Vni, |
| 70 | IpAddress dstVmIP, SegmentationId dstVni, |
| 71 | MacAddress dstVmGwMac, MacAddress dstVmMac, |
| 72 | Operation type) { |
| 73 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 74 | .matchEthType(IP_TYPE) |
| 75 | .matchTunnelId(Long.parseLong(l3Vni.segmentationId())) |
| 76 | .matchIPDst(IpPrefix.valueOf(dstVmIP, PREFIX_LENGTH)).build(); |
| 77 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 78 | treatment.setEthSrc(dstVmGwMac) |
| 79 | .setEthDst(dstVmMac) |
| 80 | .add(Instructions.modTunnelId(Long.parseLong(dstVni |
| 81 | .segmentationId()))); |
| 82 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 83 | .builder().withTreatment(treatment.build()) |
| 84 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) |
| 85 | .withPriority(L3FWD_PRIORITY); |
| 86 | if (type.equals(Objective.Operation.ADD)) { |
| 87 | log.debug("RouteRules-->ADD"); |
| 88 | flowObjectiveService.forward(deviceId, objective.add()); |
| 89 | } else { |
| 90 | log.debug("RouteRules-->REMOVE"); |
| 91 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } |