GBP ofoverlay.sf test improvements
[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 com.google.common.collect.ImmutableList;
15
16 import org.opendaylight.groupbasedpolicy.api.sf.EtherTypeClassifierDefinition;
17 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
18 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinition;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.has.parameters.type.parameter.type.IntBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.supported.classifier.definition.SupportedParameterValues;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.supported.classifier.definition.SupportedParameterValuesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.supported._int.value.fields.SupportedIntValue;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.supported._int.value.fields.SupportedIntValueBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
30
31 /**
32  * Match on the IP protocol of IP traffic
33  */
34 public class IpProtoClassifier extends Classifier {
35
36     protected IpProtoClassifier(Classifier parent) {
37         super(parent);
38     }
39
40     @Override
41     public ClassifierDefinitionId getId() {
42         return IpProtoClassifierDefinition.ID;
43     }
44
45     @Override
46     public ClassifierDefinition getClassifierDefinition() {
47         return IpProtoClassifierDefinition.DEFINITION;
48     }
49
50     @Override
51     public List<SupportedParameterValues> getSupportedParameterValues() {
52
53         List<SupportedIntValue> values = ImmutableList.of(
54                 new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.ICMP_VALUE).build(),
55                 new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.SCTP_VALUE).build(),
56                 new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.TCP_VALUE).build(),
57                 new SupportedIntValueBuilder().setValue(IpProtoClassifierDefinition.UDP_VALUE).build());
58         SupportedParameterValuesBuilder b = new SupportedParameterValuesBuilder();
59         b.setParameterName(new ParameterName(IpProtoClassifierDefinition.PROTO_PARAM));
60         b.setParameterType(new IntBuilder().setSupportedIntValue(values).build());
61
62         return ImmutableList.of(b.build());
63     }
64
65     @Override
66     protected void checkPresenceOfRequiredParams(Map<String, ParameterValue> params) {
67         if (params.get(IpProtoClassifierDefinition.PROTO_PARAM) == null) {
68             throw new IllegalArgumentException(
69                     "Parameter " + IpProtoClassifierDefinition.PROTO_PARAM + " " + MSG_NOT_SPECIFIED);
70         }
71         if (params.get(IpProtoClassifierDefinition.PROTO_PARAM).getIntValue() == null) {
72             throw new IllegalArgumentException(
73                     "Value of " + IpProtoClassifierDefinition.PROTO_PARAM + " " + MSG_PARAMETER_IS_NOT_PRESENT);
74         }
75     }
76
77     @Override
78     protected List<MatchBuilder> update(List<MatchBuilder> matches, Map<String, ParameterValue> params) {
79         Long proto = params.get(IpProtoClassifierDefinition.PROTO_PARAM).getIntValue();
80         for (MatchBuilder match : matches) {
81             IpMatchBuilder imb;
82             if (match.getIpMatch() != null) {
83                 equalOrNotSetValidation(match.getIpMatch().getIpProtocol(), proto);
84                 continue;
85             } else {
86                 imb = new IpMatchBuilder();
87             }
88             imb.setIpProtocol(proto.shortValue());
89             match.setIpMatch(imb.build());
90         }
91         return matches;
92     }
93
94     private void equalOrNotSetValidation(Short protoInMatch, long paramValue) {
95         if (protoInMatch != null) {
96             if (paramValue != protoInMatch.longValue()) {
97                 throw new IllegalArgumentException(MSG_CLASSIFICATION_CONFLICT_DETECTED + " at "
98                         + IpProtoClassifierDefinition.PROTO_PARAM + " parameter for values " + protoInMatch + " and "
99                         + paramValue + ". It is not allowed "
100                         + "to assign different values to the same parameter among all the classifiers within one rule.");
101             }
102         }
103     }
104
105     @Override
106     protected void checkPrereqs(List<MatchBuilder> matches) {
107         for (MatchBuilder match : matches) {
108             Long readEthType = null;
109             try {
110                 readEthType = match.getEthernetMatch().getEthernetType().getType().getValue();
111             } catch (NullPointerException e) {
112                 throw new IllegalArgumentException(
113                         "Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM + " " + MSG_IS_MISSING);
114             }
115             if (!FlowUtils.IPv4.equals(readEthType) && !FlowUtils.IPv6.equals(readEthType)) {
116                 throw new IllegalArgumentException("Parameter " + EtherTypeClassifierDefinition.ETHERTYPE_PARAM
117                         + " must have value " + FlowUtils.IPv4 + " or " + FlowUtils.IPv6 + ".");
118             }
119         }
120     }
121
122     /**
123      * Return the IpProtocol value. May return null.
124      *
125      * @param params the parameters of classifier-instance inserted by user
126      * @return the IpProtocol value
127      */
128     public static Long getIpProtoValue(Map<String, ParameterValue> params) {
129         if (params == null) {
130             return null;
131         }
132         if (params.get(IpProtoClassifierDefinition.PROTO_PARAM) == null) {
133             return null;
134         }
135         Long proto = params.get(IpProtoClassifierDefinition.PROTO_PARAM).getIntValue();
136         if (proto != null) {
137             return proto;
138         }
139         return null;
140     }
141 }