1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
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;
14 import com.google.common.base.Strings;
16 public class SecRuleNameDecoder {
18 final static String MIN_PORT = "_min";
19 final static String MAX_PORT = "_max";
21 private SecRuleNameDecoder() {
22 throw new UnsupportedOperationException("Cannot create an instance.");
25 public static SubjectName getSubjectName(NeutronSecurityRule secRule) {
26 RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
27 return new SubjectName(ruleName);
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);
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())
44 return new ClassifierName(crName);
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());
59 keyBuilder.append(MappingUtils.NAME_DELIMETER)
60 .append(L4Classifier.DST_PORT_RANGE_PARAM)
62 .append(MappingUtils.NAME_VALUE_DELIMETER)
63 .append(portMin.longValue())
65 .append(MappingUtils.NAME_VALUE_DELIMETER)
66 .append(portMax.longValue());
69 String protocol = secRule.getSecurityRuleProtocol();
70 if (!Strings.isNullOrEmpty(protocol)) {
71 if (keyBuilder.length() > 0) {
72 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
74 keyBuilder.append(IpProtoClassifier.DEFINITION.getName().getValue())
75 .append(MappingUtils.NAME_VALUE_DELIMETER)
78 String ethertype = secRule.getSecurityRuleEthertype();
79 if (!Strings.isNullOrEmpty(ethertype)) {
80 if (keyBuilder.length() > 0) {
81 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
83 keyBuilder.append(EtherTypeClassifier.DEFINITION.getName().getValue())
84 .append(MappingUtils.NAME_VALUE_DELIMETER)
87 return new ClassifierName(keyBuilder.toString());
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);
96 return new ClauseName(
97 subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER + remoteIpPrefix.replace('/', '_'));