Implementation of IP address classifier.
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / L4Classifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.sf;
10
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.IsRequired;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.Type;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.ParameterBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinition;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinitionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
27
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableMap;
30
31 /**
32  * Match against TCP or UDP, and source and/or destination ports
33  * @author readams
34  */
35 public class L4Classifier extends IpProtoClassifier {
36     public static final ClassifierDefinitionId ID = 
37             new ClassifierDefinitionId("4250ab32-e8b8-445a-aebb-e1bd2cdd291f");
38     private static final String SPORT = "sourceport";
39     private static final String SPORT_RANGE = "sourceport_range";
40     private static final String DPORT = "destport";
41     private static final String DPORT_RANGE = "destport_range";
42     private static final ClassifierDefinition DEF =
43             new ClassifierDefinitionBuilder()
44                 .setId(new ClassifierDefinitionId("4250ab32-e8b8-445a-aebb-e1bd2cdd291f"))
45                 .setParent(IpProtoClassifier.ID)
46                 .setName(new ClassifierName("l4"))
47                 .setDescription(new Description("Match on the port number of UDP or TCP traffic"))
48                 .setParameter(ImmutableList.of(new ParameterBuilder()
49                                 .setName(new ParameterName(SPORT))
50                                 .setDescription(new Description("The source port number to match against"))
51                                 .setType(Type.Int)
52                                 .build(),
53                         new ParameterBuilder()
54                                 .setName(new ParameterName(SPORT_RANGE))
55                                 .setDescription(new Description("The source port range to match against"))
56                                 .setType(Type.Range)
57                                 .build(),
58                         new ParameterBuilder()
59                                 .setName(new ParameterName(DPORT))
60                                 .setDescription(new Description("The destination port number to match against"))
61                                 .setType(Type.Int)
62                                 .build(),
63                         new ParameterBuilder()
64                                 .setName(new ParameterName(DPORT_RANGE))
65                                 .setDescription(new Description("The destination port range to match against"))
66                                 .setType(Type.Range)
67                                 .build(),
68                         new ParameterBuilder()
69                                 .setName(new ParameterName(TYPE))
70                                 .setDescription(new Description("TCP or UDP"))
71                                 .setIsRequired(IsRequired.Required)
72                                 .setType(Type.String)
73                                 .build()))
74                 .build();
75     
76     private static final Map<String, Object> tcp = 
77             ImmutableMap.<String,Object>of(PROTO, Long.valueOf(6));
78     private static final Map<String, Object> udp = 
79             ImmutableMap.<String,Object>of(PROTO, Long.valueOf(17));
80     
81     @Override
82     public ClassifierDefinitionId getId() {
83         return ID;
84     }
85
86     @Override
87     public ClassifierDefinition getClassDef() {
88         return DEF;
89     }
90
91     @Override
92     public List<MatchBuilder> updateMatch(List<MatchBuilder> matches,
93                                           Map<String, Object> params) {
94         Object t = params.get(TYPE);
95         // XXX TODO generate exception and fail the match
96         if (t == null || !(t instanceof String)) return matches;
97         String type = (String)t;
98
99         if ("UDP".equals(type)){
100             params.putAll(udp);
101         } else {
102             params.putAll(tcp);
103         }
104         matches = super.updateMatch(matches, params);
105
106         Long sport = null;
107         Long dport = null;
108         t = params.get(SPORT);
109         if (t != null && (t instanceof Long))
110             sport = (Long)t;
111         t = params.get(DPORT);
112         if (t != null && (t instanceof Long))
113             dport = (Long)t;
114
115         for (MatchBuilder b : matches) {
116             if ("UDP".equals(type)) {
117                 UdpMatchBuilder m = new UdpMatchBuilder();
118                 if (sport != null)
119                     m.setUdpSourcePort(new PortNumber(sport.intValue()));
120                 if (dport != null)
121                     m.setUdpDestinationPort(new PortNumber(dport.intValue()));
122                 b.setLayer4Match(m.build());
123             } else {
124                 TcpMatchBuilder m = new TcpMatchBuilder();
125                 if (sport != null)
126                     m.setTcpSourcePort(new PortNumber(sport.intValue()));
127                 if (dport != null)
128                     m.setTcpDestinationPort(new PortNumber(dport.intValue()));
129                 b.setLayer4Match(m.build());
130             }
131         }
132         return matches;
133     }
134 }