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