2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
11 import java.util.List;
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;
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableMap;
32 * Match against TCP or UDP, and source and/or destination ports
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"))
53 new ParameterBuilder()
54 .setName(new ParameterName(SPORT_RANGE))
55 .setDescription(new Description("The source port range to match against"))
58 new ParameterBuilder()
59 .setName(new ParameterName(DPORT))
60 .setDescription(new Description("The destination port number to match against"))
63 new ParameterBuilder()
64 .setName(new ParameterName(DPORT_RANGE))
65 .setDescription(new Description("The destination port range to match against"))
68 new ParameterBuilder()
69 .setName(new ParameterName(TYPE))
70 .setDescription(new Description("TCP or UDP"))
71 .setIsRequired(IsRequired.Required)
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));
82 public ClassifierDefinitionId getId() {
87 public ClassifierDefinition getClassDef() {
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;
99 if ("UDP".equals(type))
100 matches = super.updateMatch(matches, udp);
102 matches = super.updateMatch(matches, tcp);
106 t = params.get(SPORT);
107 if (t != null && (t instanceof Long))
109 t = params.get(DPORT);
110 if (t != null && (t instanceof Long))
113 for (MatchBuilder b : matches) {
114 if ("UDP".equals(type)) {
115 UdpMatchBuilder m = new UdpMatchBuilder();
117 m.setUdpSourcePort(new PortNumber(sport.intValue()));
119 m.setUdpDestinationPort(new PortNumber(dport.intValue()));
120 b.setLayer4Match(m.build());
122 TcpMatchBuilder m = new TcpMatchBuilder();
124 m.setTcpSourcePort(new PortNumber(sport.intValue()));
126 m.setTcpDestinationPort(new PortNumber(dport.intValue()));
127 b.setLayer4Match(m.build());