Merge "Use odlparent configuration for jacoco"
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleNameDecoder.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
3 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
4 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.EtherTypeClassifier;
5 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.IpProtoClassifier;
6 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.L4Classifier;
7 import org.opendaylight.neutron.spi.NeutronSecurityRule;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
13
14 import com.google.common.base.Strings;
15
16 public class SecRuleNameDecoder {
17
18     final static String MIN_PORT = "_min";
19     final static String MAX_PORT = "_max";
20
21     private SecRuleNameDecoder() {
22         throw new UnsupportedOperationException("Cannot create an instance.");
23     }
24
25     public static SubjectName getSubjectName(NeutronSecurityRule secRule) {
26         RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
27         return new SubjectName(ruleName);
28     }
29
30     public static RuleName getRuleName(NeutronSecurityRule secRule) {
31         ClassifierName classifierRefName = SecRuleNameDecoder.getClassifierRefName(secRule);
32         String ruleName = new StringBuilder(MappingUtils.ACTION_ALLOW.getName().getValue())
33             .append(MappingUtils.NAME_DOUBLE_DELIMETER).append(classifierRefName.getValue()).toString();
34         return new RuleName(ruleName);
35     }
36
37     public static ClassifierName getClassifierRefName(NeutronSecurityRule secRule) {
38         Direction direction = SecRuleEntityDecoder.getDirection(secRule);
39         ClassifierName classifierInstanceName = getClassifierInstanceName(secRule);
40         String crName = new StringBuilder().append(direction.name())
41             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
42             .append(classifierInstanceName.getValue())
43             .toString();
44         return new ClassifierName(crName);
45     }
46
47     public static ClassifierName getClassifierInstanceName(NeutronSecurityRule secRule) {
48         StringBuilder keyBuilder = new StringBuilder();
49         Integer portMin = secRule.getSecurityRulePortMin();
50         Integer portMax = secRule.getSecurityRulePortMax();
51         if (portMin != null && portMax != null) {
52             keyBuilder.append(L4Classifier.DEFINITION.getName().getValue());
53             if (portMin.equals(portMax)) {
54                 keyBuilder.append(MappingUtils.NAME_DELIMETER)
55                     .append(L4Classifier.DST_PORT_PARAM)
56                     .append(MappingUtils.NAME_VALUE_DELIMETER)
57                     .append(portMin.longValue());
58             } else {
59                 keyBuilder.append(MappingUtils.NAME_DELIMETER)
60                     .append(L4Classifier.DST_PORT_RANGE_PARAM)
61                     .append(MIN_PORT)
62                     .append(MappingUtils.NAME_VALUE_DELIMETER)
63                     .append(portMin.longValue())
64                     .append(MAX_PORT)
65                     .append(MappingUtils.NAME_VALUE_DELIMETER)
66                     .append(portMax.longValue());
67             }
68         }
69         String protocol = secRule.getSecurityRuleProtocol();
70         if (!Strings.isNullOrEmpty(protocol)) {
71             if (keyBuilder.length() > 0) {
72                 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
73             }
74             keyBuilder.append(IpProtoClassifier.DEFINITION.getName().getValue())
75                 .append(MappingUtils.NAME_VALUE_DELIMETER)
76                 .append(protocol);
77         }
78         String ethertype = secRule.getSecurityRuleEthertype();
79         if (!Strings.isNullOrEmpty(ethertype)) {
80             if (keyBuilder.length() > 0) {
81                 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
82             }
83             keyBuilder.append(EtherTypeClassifier.DEFINITION.getName().getValue())
84                 .append(MappingUtils.NAME_VALUE_DELIMETER)
85                 .append(ethertype);
86         }
87         return new ClassifierName(keyBuilder.toString());
88     }
89
90     public static ClauseName getClauseName(NeutronSecurityRule secRule) {
91         String remoteIpPrefix = secRule.getSecurityRuleRemoteIpPrefix();
92         SubjectName subjectName = getSubjectName(secRule);
93         if (Strings.isNullOrEmpty(remoteIpPrefix)) {
94             return new ClauseName(subjectName);
95         }
96         return new ClauseName(
97                 subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER + remoteIpPrefix.replace('/', '_'));
98     }
99
100 }