2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
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;
22 import com.google.common.base.Strings;
24 public class SecRuleNameDecoder {
26 final static String MIN_PORT = "_min";
27 final static String MAX_PORT = "_max";
29 private SecRuleNameDecoder() {
30 throw new UnsupportedOperationException("Cannot create an instance.");
33 public static SubjectName getSubjectName(NeutronSecurityRule secRule) {
34 RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
35 return new SubjectName(ruleName);
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);
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())
52 return new ClassifierName(crName);
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());
67 keyBuilder.append(MappingUtils.NAME_DELIMETER)
68 .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
70 .append(MappingUtils.NAME_VALUE_DELIMETER)
71 .append(portMin.longValue())
73 .append(MappingUtils.NAME_VALUE_DELIMETER)
74 .append(portMax.longValue());
77 String protocol = secRule.getSecurityRuleProtocol();
78 if (!Strings.isNullOrEmpty(protocol)) {
79 if (keyBuilder.length() > 0) {
80 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
82 keyBuilder.append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
83 .append(MappingUtils.NAME_VALUE_DELIMETER)
86 String ethertype = secRule.getSecurityRuleEthertype();
87 if (!Strings.isNullOrEmpty(ethertype)) {
88 if (keyBuilder.length() > 0) {
89 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
91 keyBuilder.append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
92 .append(MappingUtils.NAME_VALUE_DELIMETER)
95 return new ClassifierName(keyBuilder.toString());
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);
104 return new ClauseName(
105 subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER + remoteIpPrefix.replace('/', '_'));