Merge "Corrections on DHCP caches"
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / utils / AclServiceOFFlowBuilder.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
9 package org.opendaylight.netvirt.aclservice.utils;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.opendaylight.genius.mdsalutil.MatchFieldType;
17 import org.opendaylight.genius.mdsalutil.MatchInfo;
18 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
19 import org.opendaylight.genius.mdsalutil.NwConstants;
20 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
21 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
22 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;
23 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;
24 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.ace.ip.ace.ip.version.AceIpv4;
25 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.ace.ip.ace.ip.version.AceIpv6;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.DestinationPortRange;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.packet.fields.rev160218.acl.transport.header.fields.SourcePortRange;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class AclServiceOFFlowBuilder {
34
35     private static final Logger LOG =
36             LoggerFactory.getLogger(AclServiceOFFlowBuilder.class);
37
38     /**
39      * Converts IP matches into flows.
40      * @param matches
41      *            the matches
42      * @return the map containing the flows and the respective flow id
43      */
44     public static Map<String, List<MatchInfoBase>> programIpFlow(Matches matches) {
45         if (matches != null) {
46             AceIp acl = (AceIp) matches.getAceType();
47             Short protocol = acl.getProtocol();
48             if (protocol == null) {
49                 return programEtherFlow(acl);
50             } else if (acl.getProtocol() == NwConstants.IP_PROT_TCP) {
51                 return programTcpFlow(acl);
52             } else if (acl.getProtocol() == NwConstants.IP_PROT_UDP) {
53                 return programUdpFlow(acl);
54             } else if (acl.getProtocol() == NwConstants.IP_PROT_ICMP) {
55                 return programIcmpFlow(acl);
56             } else if (acl.getProtocol() != -1) {
57                 return programOtherProtocolFlow(acl);
58             }
59         }
60         return null;
61     }
62
63     /** Converts ether  matches to flows.
64      * @param acl the access control list
65      * @return the map containing the flows and the respective flow id
66      */
67     public static Map<String,List<MatchInfoBase>> programEtherFlow(AceIp acl) {
68         List<MatchInfoBase> flowMatches = new ArrayList<>();
69         flowMatches.addAll(addSrcIpMatches(acl));
70         flowMatches.addAll(addDstIpMatches(acl));
71         String flowId = "ETHER" + acl.getProtocol();
72         Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>();
73         flowMatchesMap.put(flowId,flowMatches);
74         return flowMatchesMap;
75     }
76
77     /** Converts generic protocol matches to flows.
78      *
79      * @param acl the access control list
80      * @return the map containing the flows and the respective flow id
81      */
82     public static Map<String,List<MatchInfoBase>> programOtherProtocolFlow(AceIp acl) {
83         List<MatchInfoBase> flowMatches = new ArrayList<>();
84         flowMatches.addAll(addSrcIpMatches(acl));
85         flowMatches.addAll(addDstIpMatches(acl));
86         if (acl.getAceIpVersion() instanceof AceIpv4 ) {
87             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
88                 new long[] { NwConstants.ETHTYPE_IPV4 }));
89         } else if (acl.getAceIpVersion() instanceof AceIpv6 ) {
90             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
91                 new long[] { NwConstants.ETHTYPE_IPV6 }));
92         }
93         flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
94             new long[] { acl.getProtocol() }));
95         String flowId = "OTHER_PROTO" + acl.getProtocol();
96         Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>();
97         flowMatchesMap.put(flowId,flowMatches);
98         return flowMatchesMap;
99     }
100
101     /**Converts icmp matches to flows.
102      * @param acl the access control list
103      * @return the map containing the flows and the respective flow id
104      */
105     public static Map<String,List<MatchInfoBase>> programIcmpFlow(AceIp acl) {
106         List<MatchInfoBase> flowMatches = new ArrayList<>();
107         flowMatches.addAll(addSrcIpMatches(acl));
108         flowMatches.addAll(addDstIpMatches(acl));
109         //For ICMP port range indicates type and code
110         SourcePortRange sourcePortRange = acl.getSourcePortRange();
111         String flowId = "ICMP_";
112         if (sourcePortRange != null) {
113             if (acl.getAceIpVersion() instanceof AceIpv4 ) {
114                 flowMatches.add(new MatchInfo(MatchFieldType.icmp_v4,
115                     new long[] { sourcePortRange.getLowerPort().getValue(),
116                                  sourcePortRange.getUpperPort().getValue() }));
117                 flowId = flowId + "V4_SOURCE_" + sourcePortRange.getLowerPort().getValue()
118                         + sourcePortRange.getUpperPort().getValue();
119             } else if (acl.getAceIpVersion() instanceof AceIpv6 ) {
120                 flowMatches.add(new MatchInfo(MatchFieldType.icmp_v6,
121                     new long[] { sourcePortRange.getLowerPort().getValue(),
122                                  sourcePortRange.getUpperPort().getValue() }));
123                 flowId = flowId + "V6_SOURCE_" + sourcePortRange.getLowerPort().getValue() + "_"
124                         + sourcePortRange.getUpperPort().getValue() + "_";
125             }
126         }
127         DestinationPortRange destinationPortRange = acl.getDestinationPortRange();
128         if (destinationPortRange != null) {
129             if (acl.getAceIpVersion() instanceof AceIpv4 ) {
130                 flowMatches.add(new MatchInfo(MatchFieldType.icmp_v4,
131                     new long[] { destinationPortRange.getLowerPort().getValue(),
132                                  destinationPortRange.getUpperPort().getValue() }));
133                 flowId = flowId + "V4_DESTINATION_" + destinationPortRange.getLowerPort().getValue()
134                         + destinationPortRange.getUpperPort().getValue() + "_";
135             } else if (acl.getAceIpVersion() instanceof AceIpv6 ) {
136                 flowMatches.add(new MatchInfo(MatchFieldType.icmp_v6,
137                     new long[] { destinationPortRange.getLowerPort().getValue(),
138                                  destinationPortRange.getUpperPort().getValue() }));
139                 flowId = flowId + "V6_DESTINATION_" + destinationPortRange.getLowerPort().getValue()
140                         + destinationPortRange.getUpperPort().getValue() + "_";
141             }
142         }
143         flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
144             new long[] { acl.getProtocol() }));
145         Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>();
146         flowMatchesMap.put(flowId,flowMatches);
147         return flowMatchesMap;
148     }
149
150     /**Converts TCP matches to flows.
151      * @param acl the access control list
152      * @return the map containing the flows and the respective flow id
153      */
154     public static Map<String,List<MatchInfoBase>> programTcpFlow(AceIp acl) {
155         Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>();
156         SourcePortRange sourcePortRange = acl.getSourcePortRange();
157         DestinationPortRange destinationPortRange = acl.getDestinationPortRange();
158         if (sourcePortRange == null && destinationPortRange == null) {
159             List<MatchInfoBase> flowMatches = new ArrayList<>();
160             flowMatches.addAll(addSrcIpMatches(acl));
161             flowMatches.addAll(addDstIpMatches(acl));
162             flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
163                 new long[] { acl.getProtocol() }));
164             String flowId = "TCP_SOURCE_ALL_";
165             flowMatchesMap.put(flowId,flowMatches);
166             return flowMatchesMap;
167         }
168         if (sourcePortRange != null) {
169             Map<Integer, Integer> portMaskMap = getLayer4MaskForRange(sourcePortRange.getLowerPort().getValue(),
170                 sourcePortRange.getUpperPort().getValue());
171             for (Integer port: portMaskMap.keySet()) {
172                 List<MatchInfoBase> flowMatches = new ArrayList<>();
173                 flowMatches.addAll(addSrcIpMatches(acl));
174                 flowMatches.addAll(addDstIpMatches(acl));
175                 flowMatches.add(new NxMatchInfo(NxMatchFieldType.nx_tcp_src_with_mask,
176                     new long[] {  port, portMaskMap.get(port) }));
177                 flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
178                     new long[] { acl.getProtocol() }));
179                 String flowId = "TCP_SOURCE_" + port + "_" + portMaskMap.get(port);
180                 flowMatchesMap.put(flowId,flowMatches);
181             }
182         }
183         if (destinationPortRange != null) {
184             Map<Integer, Integer> portMaskMap = getLayer4MaskForRange(destinationPortRange.getLowerPort().getValue(),
185                 destinationPortRange.getUpperPort().getValue());
186             for (Integer port: portMaskMap.keySet()) {
187                 List<MatchInfoBase> flowMatches = new ArrayList<>();
188                 flowMatches.addAll(addSrcIpMatches(acl));
189                 flowMatches.addAll(addDstIpMatches(acl));
190                 flowMatches.add(new NxMatchInfo(NxMatchFieldType.nx_tcp_dst_with_mask,
191                     new long[] {  port, portMaskMap.get(port) }));
192                 flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
193                     new long[] { acl.getProtocol() }));
194                 String flowId = "TCP_DESTINATION_" + port + "_" + portMaskMap.get(port);
195                 flowMatchesMap.put(flowId,flowMatches);
196             }
197         }
198         return flowMatchesMap;
199     }
200
201     /**Converts UDP matches to flows.
202      * @param acl the access control list
203      * @return the map containing the flows and the respective flow id
204      */
205     public static Map<String,List<MatchInfoBase>> programUdpFlow(AceIp acl) {
206         Map<String,List<MatchInfoBase>> flowMatchesMap = new HashMap<>();
207         SourcePortRange sourcePortRange = acl.getSourcePortRange();
208         DestinationPortRange destinationPortRange = acl.getDestinationPortRange();
209         if (sourcePortRange == null && destinationPortRange == null) {
210             List<MatchInfoBase> flowMatches = new ArrayList<>();
211             flowMatches.addAll(addSrcIpMatches(acl));
212             flowMatches.addAll(addDstIpMatches(acl));
213             flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
214                 new long[] { acl.getProtocol() }));
215             String flowId = "UDP_SOURCE_ALL_";
216             flowMatchesMap.put(flowId,flowMatches);
217             return flowMatchesMap;
218         }
219         if (sourcePortRange != null) {
220             Map<Integer, Integer> portMaskMap = getLayer4MaskForRange(sourcePortRange.getLowerPort().getValue(),
221                 sourcePortRange.getUpperPort().getValue());
222             for (Integer port: portMaskMap.keySet()) {
223                 List<MatchInfoBase> flowMatches = new ArrayList<>();
224                 flowMatches.addAll(addSrcIpMatches(acl));
225                 flowMatches.addAll(addDstIpMatches(acl));
226                 flowMatches.add(new NxMatchInfo(NxMatchFieldType.nx_udp_src_with_mask,
227                     new long[] {  port, portMaskMap.get(port) }));
228                 flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
229                     new long[] { acl.getProtocol() }));
230                 String flowId = "UDP_SOURCE_" + port + "_" + portMaskMap.get(port);
231                 flowMatchesMap.put(flowId ,flowMatches);
232             }
233         }
234         if (destinationPortRange != null) {
235             Map<Integer, Integer> portMaskMap = getLayer4MaskForRange(destinationPortRange.getLowerPort().getValue(),
236                 destinationPortRange.getUpperPort().getValue());
237             for (Integer port: portMaskMap.keySet()) {
238                 List<MatchInfoBase> flowMatches = new ArrayList<>();
239                 flowMatches.addAll(addSrcIpMatches(acl));
240                 flowMatches.addAll(addDstIpMatches(acl));
241                 flowMatches.add(new NxMatchInfo(NxMatchFieldType.nx_udp_dst_with_mask,
242                     new long[] {  port, portMaskMap.get(port) }));
243                 flowMatches.add(new MatchInfo(MatchFieldType.ip_proto,
244                     new long[] { acl.getProtocol() }));
245                 String flowId = "UDP_DESTINATION_" + port + "_" + portMaskMap.get(port);
246                 flowMatchesMap.put(flowId, flowMatches);
247             }
248         }
249
250         return flowMatchesMap;
251     }
252
253     /** Adds source ip matches to the flows.
254      * @param acl the access control list
255      * @return the list of flows.
256      */
257     public static List<MatchInfoBase> addSrcIpMatches(AceIp acl) {
258         List<MatchInfoBase> flowMatches = new ArrayList<>();
259         if (acl.getAceIpVersion() instanceof AceIpv4 ) {
260             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
261                 new long[] { NwConstants.ETHTYPE_IPV4 }));
262             Ipv4Prefix srcNetwork = ((AceIpv4)acl.getAceIpVersion()).getSourceIpv4Network();
263             if (null != srcNetwork) {
264                 String[] ipaddressValues = srcNetwork.getValue().split("/");
265                 flowMatches.add(new MatchInfo(MatchFieldType.ipv4_source,
266                     new String[] {ipaddressValues[0], ipaddressValues[1]}));
267             }
268         } else if (acl.getAceIpVersion() instanceof AceIpv6 ) {
269             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
270                 new long[] { NwConstants.ETHTYPE_IPV6 }));
271             Ipv6Prefix srcNetwork = ((AceIpv6)acl.getAceIpVersion()).getSourceIpv6Network();
272             if (null != srcNetwork) {
273                 flowMatches.add(new MatchInfo(MatchFieldType.ipv6_source,
274                     new String[] {srcNetwork.getValue()}));
275             }
276         }
277         return flowMatches;
278     }
279
280     /** Adds destination ip matches to the flows.
281      * @param acl the access control list
282      * @return the list of flows.
283      */
284     public static List<MatchInfoBase> addDstIpMatches(AceIp acl) {
285         List<MatchInfoBase> flowMatches = new ArrayList<>();
286         if (acl.getAceIpVersion() instanceof AceIpv4 ) {
287             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
288                 new long[] { NwConstants.ETHTYPE_IPV4 }));
289             Ipv4Prefix dstNetwork = ((AceIpv4)acl.getAceIpVersion()).getDestinationIpv4Network();
290             if (null != dstNetwork) {
291                 String[] ipaddressValues = dstNetwork.getValue().split("/");
292                 flowMatches.add(new MatchInfo(MatchFieldType.ipv4_destination,
293                     new String[] {ipaddressValues[0], ipaddressValues[1]}));
294             }
295         } else if (acl.getAceIpVersion() instanceof AceIpv6 ) {
296             flowMatches.add(new MatchInfo(MatchFieldType.eth_type,
297                 new long[] { NwConstants.ETHTYPE_IPV6 }));
298             Ipv6Prefix dstNetwork = ((AceIpv6)acl.getAceIpVersion()).getDestinationIpv6Network();
299             if (null != dstNetwork) {
300                 flowMatches.add(new MatchInfo(MatchFieldType.ipv6_destination,
301                     new String[] {dstNetwork.getValue()}));
302             }
303         }
304         return flowMatches;
305     }
306
307     /**
308      * Converts port range into a set of masked port ranges.
309      *
310      * @param portMin the starting port of the range.
311      * @param portMax the ending port of the range.
312      * @return the map containing the port no and their mask.
313      *
314      */
315     public static Map<Integer,Integer>  getLayer4MaskForRange(int portMin, int portMax) {
316         final int[] offset = { 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 };
317         final int[] mask = { 0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0,
318             0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF };
319         int noOfPorts = portMax - portMin + 1;
320         Map<Integer,Integer> portMap = new HashMap<>();
321         if (noOfPorts == 1) {
322             portMap.put(portMin, mask[15]);
323             return portMap;
324         }
325         if (noOfPorts < 0) { // TODO: replace with infrautils.counter in case of high repetitive usage
326             LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin,
327                     portMax);
328             return portMap;
329         }
330         String binaryNoOfPorts = Integer.toBinaryString(noOfPorts);
331         if (binaryNoOfPorts.length() > 16) { // TODO: replace with infrautils.counter in case of high repetitive usage
332             LOG.warn("Cannot convert port range into a set of masked port ranges - Illegal port range {}-{}", portMin,
333                     portMax);
334             return portMap;
335         }
336         int medianOffset = 16 - binaryNoOfPorts.length();
337         int medianLength = offset[medianOffset];
338         int median = 0;
339         for (int tempMedian = 0;tempMedian < portMax;) {
340             tempMedian = medianLength + tempMedian;
341             if (portMin < tempMedian) {
342                 median = tempMedian;
343                 break;
344             }
345         }
346         int tempMedian = 0;
347         int currentMedain = median;
348         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
349             tempMedian = currentMedain - offset[tempMedianOffset];
350             if (portMin <= tempMedian) {
351                 for (;portMin <= tempMedian;) {
352                     portMap.put(tempMedian, mask[tempMedianOffset]);
353                     currentMedain = tempMedian;
354                     tempMedian = tempMedian - offset[tempMedianOffset];
355                 }
356             }
357         }
358         currentMedain = median;
359         for (int tempMedianOffset = medianOffset;16 > tempMedianOffset;tempMedianOffset++) {
360             tempMedian = currentMedain + offset[tempMedianOffset];
361             if (portMax >= tempMedian - 1) {
362                 for (;portMax >= tempMedian - 1;) {
363                     portMap.put(currentMedain, mask[tempMedianOffset]);
364                     currentMedain = tempMedian;
365                     tempMedian = tempMedian  + offset[tempMedianOffset];
366                 }
367             }
368         }
369         return portMap;
370     }
371
372 }