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