blob: 40cd350e01ee3fa5a4eb192c08945063f2b99c84 [file] [log] [blame]
lishuaieb749502015-11-26 20:07:35 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
lishuaieb749502015-11-26 20:07:35 +08003 *
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.vtn.table.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.packet.Ethernet;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flowobjective.DefaultForwardingObjective;
34import org.onosproject.net.flowobjective.FlowObjectiveService;
35import org.onosproject.net.flowobjective.ForwardingObjective;
36import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
37import org.onosproject.net.flowobjective.Objective;
38import org.onosproject.vtn.table.SnatService;
39import org.onosproject.vtnrsc.SegmentationId;
40import org.slf4j.Logger;
41
42/**
43 * Provides implementation of SnatService.
44 */
45public class SnatServiceImpl implements SnatService {
46 private final Logger log = getLogger(getClass());
47
48 private static final int SNAT_PRIORITY = 0xffff;
49 private static final int PREFIC_LENGTH = 32;
50
51 private final FlowObjectiveService flowObjectiveService;
52 private final ApplicationId appId;
53
54 /**
55 * Construct a SnatServiceImpl object.
56 *
57 * @param appId the application id of vtn
58 */
59 public SnatServiceImpl(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, SegmentationId matchVni,
67 IpAddress srcIP, MacAddress ethDst,
68 MacAddress ethSrc, IpAddress ipSrc,
69 SegmentationId actionVni, Objective.Operation type) {
70 TrafficSelector selector = DefaultTrafficSelector.builder()
71 .matchEthType(Ethernet.TYPE_IPV4)
72 .matchTunnelId(Long.parseLong(matchVni.segmentationId()))
73 .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)).build();
74
75 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
76 treatment.setEthDst(ethDst).setEthSrc(ethSrc).setIpSrc(ipSrc)
77 .setTunnelId(Long.parseLong(actionVni.segmentationId()));
78 ForwardingObjective.Builder objective = DefaultForwardingObjective
79 .builder().withTreatment(treatment.build())
80 .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC)
81 .withPriority(SNAT_PRIORITY);
82 if (type.equals(Objective.Operation.ADD)) {
83 log.debug("RouteRules-->ADD");
84 flowObjectiveService.forward(deviceId, objective.add());
85 } else {
86 log.debug("RouteRules-->REMOVE");
87 flowObjectiveService.forward(deviceId, objective.remove());
88 }
89 }
90}