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.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.rev100924.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.constants.rev150712.ProtocolBase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
27 public class SecRuleNameDecoder {
29 final static String MIN_PORT = "_min";
30 final static String MAX_PORT = "_max";
32 private SecRuleNameDecoder() {
33 throw new UnsupportedOperationException("Cannot create an instance.");
36 public static SubjectName getSubjectName(SecurityRule secRule) {
37 RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
38 return new SubjectName(ruleName);
41 public static RuleName getRuleName(SecurityRule secRule) {
42 ClassifierName classifierRefName = SecRuleNameDecoder.getClassifierRefName(secRule);
43 String ruleName = new StringBuilder(MappingUtils.ACTION_ALLOW.getName().getValue())
44 .append(MappingUtils.NAME_DOUBLE_DELIMETER).append(classifierRefName.getValue()).toString();
45 return new RuleName(ruleName);
48 public static ClassifierName getClassifierRefName(SecurityRule secRule) {
49 Direction direction = SecRuleEntityDecoder.getDirection(secRule);
50 ClassifierName classifierInstanceName = getClassifierInstanceName(secRule);
51 String crName = new StringBuilder().append(direction.name())
52 .append(MappingUtils.NAME_DOUBLE_DELIMETER)
53 .append(classifierInstanceName.getValue())
55 return new ClassifierName(crName);
58 public static ClassifierName getClassifierInstanceName(SecurityRule secRule) {
59 StringBuilder keyBuilder = new StringBuilder();
60 Integer portMin = secRule.getPortRangeMin();
61 Integer portMax = secRule.getPortRangeMax();
62 if (portMin != null && portMax != null) {
63 keyBuilder.append(L4ClassifierDefinition.DEFINITION.getName().getValue());
64 if (portMin.equals(portMax)) {
65 keyBuilder.append(MappingUtils.NAME_DELIMETER)
66 .append(L4ClassifierDefinition.DST_PORT_PARAM)
67 .append(MappingUtils.NAME_VALUE_DELIMETER)
68 .append(portMin.longValue());
70 keyBuilder.append(MappingUtils.NAME_DELIMETER)
71 .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
73 .append(MappingUtils.NAME_VALUE_DELIMETER)
74 .append(portMin.longValue())
76 .append(MappingUtils.NAME_VALUE_DELIMETER)
77 .append(portMax.longValue());
80 SecurityRuleAttributes.Protocol protocol = secRule.getProtocol();
81 if (protocol != null) {
82 if (keyBuilder.length() > 0) {
83 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
85 String protocolString = "";
86 if (protocol.getUint8() != null) {
87 protocolString = protocol.getUint8().toString();
88 } else if (protocol.getIdentityref() != null) {
89 protocolString = protocol.getIdentityref().getSimpleName();
91 keyBuilder.append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
92 .append(MappingUtils.NAME_VALUE_DELIMETER)
93 .append(protocolString);
95 Class<? extends EthertypeBase> ethertype = secRule.getEthertype();
96 if (ethertype != null) {
97 if (keyBuilder.length() > 0) {
98 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
100 keyBuilder.append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
101 .append(MappingUtils.NAME_VALUE_DELIMETER)
102 .append(ethertype.getSimpleName());
104 return new ClassifierName(keyBuilder.toString());
107 public static ClauseName getClauseName(SecurityRule secRule) {
108 IpPrefix remoteIpPrefix = secRule.getRemoteIpPrefix();
109 SubjectName subjectName = getSubjectName(secRule);
110 if (remoteIpPrefix == null) {
111 return new ClauseName(subjectName);
113 return new ClauseName(subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER
114 + Utils.getStringIpPrefix(remoteIpPrefix).replace('/', '_'));