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.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;
26 public class SecRuleNameDecoder {
28 static final String MIN_PORT = "_min";
29 static final String MAX_PORT = "_max";
31 private SecRuleNameDecoder() {
32 throw new UnsupportedOperationException("Cannot create an instance.");
35 public static SubjectName getSubjectName(SecurityRule secRule) {
36 RuleName ruleName = SecRuleNameDecoder.getRuleName(secRule);
37 return new SubjectName(ruleName);
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);
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())
54 return new ClassifierName(crName);
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());
69 keyBuilder.append(MappingUtils.NAME_DELIMETER)
70 .append(L4ClassifierDefinition.DST_PORT_RANGE_PARAM)
72 .append(MappingUtils.NAME_VALUE_DELIMETER)
73 .append(portMin.longValue())
75 .append(MappingUtils.NAME_VALUE_DELIMETER)
76 .append(portMax.longValue());
79 SecurityRuleAttributes.Protocol protocol = secRule.getProtocol();
80 if (protocol != null) {
81 if (keyBuilder.length() > 0) {
82 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
84 String protocolString = "";
85 if (protocol.getUint8() != null) {
86 protocolString = protocol.getUint8().toString();
87 } else if (protocol.getIdentityref() != null) {
88 protocolString = protocol.getIdentityref().getSimpleName();
90 keyBuilder.append(IpProtoClassifierDefinition.DEFINITION.getName().getValue())
91 .append(MappingUtils.NAME_VALUE_DELIMETER)
92 .append(protocolString);
94 Class<? extends EthertypeBase> ethertype = secRule.getEthertype();
95 if (ethertype != null) {
96 if (keyBuilder.length() > 0) {
97 keyBuilder.append(MappingUtils.NAME_DOUBLE_DELIMETER);
99 keyBuilder.append(EtherTypeClassifierDefinition.DEFINITION.getName().getValue())
100 .append(MappingUtils.NAME_VALUE_DELIMETER)
101 .append(ethertype.getSimpleName());
103 return new ClassifierName(keyBuilder.toString());
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);
112 return new ClauseName(subjectName.getValue() + MappingUtils.NAME_DOUBLE_DELIMETER
113 + Utils.getStringIpPrefix(remoteIpPrefix).replace('/', '_'));