Merge "Removing Blind imports across the module"
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / StatelessEgressAclServiceImpl.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.aclservice;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.InstructionInfo;
18 import org.opendaylight.genius.mdsalutil.MatchFieldType;
19 import org.opendaylight.genius.mdsalutil.MatchInfo;
20 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
23 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
25 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
26 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.Matches;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.AceType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Provides the stateless implementation for egress (w.r.t VM) ACL service.
40  *
41  * <p>
42  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
43  * and vice versa.
44  */
45 public class StatelessEgressAclServiceImpl extends AbstractEgressAclServiceImpl {
46
47     private static final Logger LOG = LoggerFactory.getLogger(StatelessEgressAclServiceImpl.class);
48
49     public StatelessEgressAclServiceImpl(DataBroker dataBroker,
50             IMdsalApiManager mdsalManager) {
51         super(dataBroker, mdsalManager);
52     }
53
54     @Override
55     protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
56             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
57     }
58
59     @Override
60     protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
61             Map<String, List<MatchInfoBase>> flowMap, String flowName) {
62         // Not in use here. programAceRule function is overridden.
63         return null;
64     }
65
66     @Override
67     protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
68             List<AllowedAddressPairs> syncAllowedAddresses) {
69         SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
70         if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
71             return;
72         }
73         Matches matches = ace.getMatches();
74         AceType aceType = matches.getAceType();
75         Map<String, List<MatchInfoBase>> flowMap = null;
76         Short protocol = null;
77
78         if (aceType instanceof AceIp) {
79             protocol = ((AceIp)aceType).getProtocol();
80             flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
81         }
82         if (null == flowMap) {
83             LOG.error("Failed to apply ACL {}", ace.getKey());
84             return;
85         }
86         for (Map.Entry<String, List<MatchInfoBase>> flow : flowMap.entrySet()) {
87             String flowName = flow.getKey();
88             List<MatchInfoBase> flowMatches = flow.getValue();
89             boolean hasTcpDstMatch = AclServiceUtils.containsMatchFieldType(flowMatches,
90                     NxMatchFieldType.nx_tcp_dst_with_mask);
91             if (hasTcpDstMatch || protocol == null) {
92                 flowName += "Egress" + lportTag + ace.getKey().getRuleName();
93                 flowMatches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
94
95                 programAllowSynRules(dpId, flowName, flowMatches, addOrRemove);
96             }
97         }
98     }
99
100     private void programAllowSynRules(BigInteger dpId, String origFlowName,
101             List<MatchInfoBase> origFlowMatches, int addFlow) {
102         List<MatchInfoBase> flowMatches = new ArrayList<>();
103         flowMatches.addAll(origFlowMatches);
104         flowMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
105
106         List<ActionInfo> actionsInfos = new ArrayList<>();
107         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
108
109         String flowName = "SYN_" + origFlowName;
110         syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_SYN_ALLOW_PRIORITY,
111                 "ACL_SYN_", 0, 0, AclConstants.COOKIE_ACL_BASE, flowMatches, instructions, addFlow);
112         String oper = getOperAsString(addFlow);
113         LOG.debug("{} allow syn packet flow {}", oper, flowName);
114     }
115
116 }