d884cec61db5c99c635fecbdb930a58624f7412d
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / IpProtoClassifier.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.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
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.ParameterBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.IsRequired;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.Type;
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.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
27
28 import com.google.common.collect.ImmutableList;
29
30 /**
31  * Match on the IP protocol of IP traffic
32  */
33 public class IpProtoClassifier extends Classifier {
34
35     public static final Long TCP = Long.valueOf(6);
36     public static final Long UDP = Long.valueOf(17);
37     public static final Long SCTP = Long.valueOf(132);
38     public static final String PROTO = "proto";
39     public static final ClassifierDefinitionId ID = new ClassifierDefinitionId("79c6fdb2-1e1a-4832-af57-c65baf5c2335");
40     protected static final ClassifierDefinition DEF = new ClassifierDefinitionBuilder().setId(ID)
41         .setParent(EtherTypeClassifier.ID)
42         .setName(new ClassifierName("ip_proto"))
43         .setDescription(new Description("Match on the IP protocol of IP traffic"))
44         .setParameter(
45                 ImmutableList.of(new ParameterBuilder().setName(new ParameterName(PROTO))
46                     .setDescription(new Description("The IP protocol to match against"))
47                     .setIsRequired(IsRequired.Required)
48                     .setType(Type.Int)
49                     .build()))
50         .build();
51
52     protected IpProtoClassifier(Classifier parent) {
53         super(parent);
54     }
55
56     @Override
57     public ClassifierDefinitionId getId() {
58         return ID;
59     }
60
61     @Override
62     public ClassifierDefinition getClassDef() {
63         return DEF;
64     }
65
66     @Override
67     protected void checkPresenceOfRequiredParams(Map<String, ParameterValue> params) {
68         if (params.get(PROTO) == null) {
69             throw new IllegalArgumentException("Classifier: {" + this.getClassDef().getName()
70                     + "}+ Parameter proto not present.");
71         }
72         if (params.get(PROTO).getIntValue() == null) {
73             throw new IllegalArgumentException("Classifier: {" + this.getClassDef().getName()
74                     + "}+ Value of proto parameter is not present.");
75         }
76     }
77
78     @Override
79     protected List<MatchBuilder> update(List<MatchBuilder> matches, Map<String, ParameterValue> params) {
80         Long proto = params.get(PROTO).getIntValue();
81         for (MatchBuilder match : matches) {
82             IpMatchBuilder imb;
83             if (match.getIpMatch() != null) {
84                 equalOrNotSetValidation(match.getIpMatch().getIpProtocol(), proto);
85                 continue;
86             } else {
87                 imb = new IpMatchBuilder();
88             }
89             imb.setIpProtocol(proto.shortValue());
90             match.setIpMatch(imb.build());
91         }
92         return matches;
93     }
94
95     private void equalOrNotSetValidation(Short protoInMatch, long paramValue) {
96         if (protoInMatch != null) {
97             if (paramValue != protoInMatch.longValue()) {
98                 throw new IllegalArgumentException("Classification conflict at " + this.getClassDef().getName()
99                         + ": Trying to override proto value: " + protoInMatch.shortValue() + " by value " + paramValue);
100             }
101         }
102     }
103
104     @Override
105     protected void checkPrereqs(List<MatchBuilder> matches) {
106         for (MatchBuilder match : matches) {
107             Long readEthType = null;
108             try {
109                 readEthType = match.getEthernetMatch().getEthernetType().getType().getValue();
110             } catch (NullPointerException e) {
111                 throw new IllegalArgumentException("Ether-type match is missing.");
112             }
113             if (!FlowUtils.IPv4.equals(readEthType) && !FlowUtils.IPv6.equals(readEthType)) {
114                 throw new IllegalArgumentException("Ether-type value should be " + FlowUtils.IPv4 + " or "
115                         + FlowUtils.IPv6 + ".");
116             }
117         }
118     }
119
120     /**
121      * May return null.
122      */
123     public static Long getIpProtoValue(Map<String, ParameterValue> params) {
124         if (params == null) {
125             return null;
126         }
127         if (params.get(PROTO) == null) {
128             return null;
129         }
130         Long proto = params.get(PROTO).getIntValue();
131         if (proto != null) {
132             return proto;
133         }
134         return null;
135     }
136 }