Fix odlparent 3 Checkstyle issues
[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.api.sf.EtherTypeClassifierDefinition;
12 import org.opendaylight.groupbasedpolicy.api.sf.IpProtoClassifierDefinition;
13 import org.opendaylight.groupbasedpolicy.api.sf.L4ClassifierDefinition;
14 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
15 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
25
26 public final class SecRuleNameDecoder {
27
28     static final String MIN_PORT = "_min";
29     static final String MAX_PORT = "_max";
30
31     private SecRuleNameDecoder() {
32         throw new UnsupportedOperationException("Cannot create an instance.");
33     }
34
35     public static SubjectName getSubjectName(SecurityRule secRule) {
36         RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
37         return new SubjectName(ruleName);
38     }
39
40     public static RuleName getRuleName(SecurityRule secRule) {
41         ClassifierName classifierRefName = SecRuleNameDecoder.getClassifierRefName(secRule);
42         String ruleName = new StringBuilder(MappingUtils.ACTION_ALLOW.getName().getValue())
43             .append(MappingUtils.NAME_DOUBLE_DELIMETER).append(classifierRefName.getValue()).toString();
44         return new RuleName(ruleName);
45     }
46
47     public static ClassifierName getClassifierRefName(SecurityRule secRule) {
48         Direction direction = SecRuleEntityDecoder.getDirection(secRule);
49         ClassifierName classifierInstanceName = getClassifierInstanceName(secRule);
50         String crName = new StringBuilder().append(direction.name())
51             .append(MappingUtils.NAME_DOUBLE_DELIMETER)
52             .append(classifierInstanceName.getValue())
53             .toString();
54         return new ClassifierName(crName);
55     }
56
57     public static ClassifierName getClassifierInstanceName(SecurityRule secRule) {
58         StringBuilder keyBuilder = new StringBuilder();
59         Integer portMin = secRule.getPortRangeMin();
60         Integer portMax = secRule.getPortRangeMax();
61         if (portMin != null && portMax != null) {
62             keyBuilder.append(L4ClassifierDefinition.DEFINITION.getName().getValue());
63             if (portMin.equals(portMax)) {
64                 keyBuilder.append(MappingUtils.NAME_DELIMETER)
65                     .append(L4ClassifierDefinition.DST_PORT_PARAM)
66                     .append(MappingUtils.NAME_VALUE_DELIMETER)
67                     .append(portMin.longValue());
68             } else {
69                 keyBuilder.append(MappingUtils.NAME_DELIMETER)
70                     .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
71                     .append(MIN_PORT)
72                     .append(MappingUtils.NAME_VALUE_DELIMETER)
73                     .append(portMin.longValue())
74                     .append(MAX_PORT)
75                     .append(MappingUtils.NAME_VALUE_DELIMETER)
76                     .append(portMax.longValue());
77             }
78         }
79         SecurityRuleAttributes.Protocol protocol = secRule.getProtocol();
80         if (protocol != null) {
81             if (keyBuilder.length() > 0) {
82                 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
83             }
84             String protocolString = "";
85             if (protocol.getUint8() != null) {
86                 protocolString = protocol.getUint8().toString();
87             } else if (protocol.getIdentityref() != null) {
88                 protocolString = protocol.getIdentityref().getSimpleName();
89             }
90             keyBuilder.append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
91                 .append(MappingUtils.NAME_VALUE_DELIMETER)
92                 .append(protocolString);
93         }
94         Class<? extends EthertypeBase> ethertype = secRule.getEthertype();
95         if (ethertype != null) {
96             if (keyBuilder.length() > 0) {
97                 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
98             }
99             keyBuilder.append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
100                 .append(MappingUtils.NAME_VALUE_DELIMETER)
101                 .append(ethertype.getSimpleName());
102         }
103         return new ClassifierName(keyBuilder.toString());
104     }
105
106     public static ClauseName getClauseName(SecurityRule secRule) {
107         IpPrefix remoteIpPrefix = secRule.getRemoteIpPrefix();
108         SubjectName subjectName = getSubjectName(secRule);
109         if (remoteIpPrefix == null) {
110             return new ClauseName(subjectName);
111         }
112         return new ClauseName(subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER
113                 + Utils.getStringIpPrefix(remoteIpPrefix).replace('/', '_'));
114     }
115
116 }