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