1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
3 import com.google.common.base.Joiner;
4 import com.google.common.base.Strings;
6 import org.apache.commons.lang3.StringUtils;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
14 import java.util.Comparator;
15 import java.util.TreeSet;
17 public class FlowIdUtils {
19 private static final String TABLE_ID_PREFIX = "t";
20 private static final String FLOWID_SEPARATOR = "|";
21 private static final String MATCH_PREFIX = "match[";
22 private static final String MATCH_SUFFIX = "]";
23 private static final String MATCH_SEPARATOR = ", ";
25 // *** flow from FlowTable (abstract parent) ***
28 * For flow without match specified (actually, only "drop all" flow)
30 * @param prefix String
33 public static FlowId newFlowId(String prefix) {
35 return new FlowId(prefix);
39 * FlowId based on match (with prefix like "t2|localL3|")
41 * @param tableId Short
42 * @param prefix String
46 public static FlowId newFlowId(Short tableId, String prefix, Match match) {
48 return new FlowId((tableId != null ? TABLE_ID_PREFIX + tableId + FLOWID_SEPARATOR : "")
49 + prefix + FLOWID_SEPARATOR
50 + formatMatch(match));
53 private static String formatMatch(Match match) {
55 return StringUtils.EMPTY;
57 StringBuilder builder = new StringBuilder(MATCH_PREFIX);
59 if (match.getEthernetMatch() != null) {
63 builder.append(MATCH_SEPARATOR);
65 builder.append(match.getEthernetMatch());
67 if (match.getIcmpv4Match() != null) {
71 builder.append(MATCH_SEPARATOR);
73 builder.append(match.getIcmpv4Match());
75 if (match.getIcmpv6Match() != null) {
79 builder.append(MATCH_SEPARATOR);
81 builder.append(match.getIcmpv6Match());
83 if (match.getInPhyPort() != null) {
87 builder.append(MATCH_SEPARATOR);
89 builder.append("inPhyPort=").append(match.getInPhyPort());
91 if (match.getInPort() != null) {
95 builder.append(MATCH_SEPARATOR);
97 builder.append("inPort=").append(match.getInPort());
99 if (match.getIpMatch() != null) {
103 builder.append(MATCH_SEPARATOR);
105 builder.append(match.getIpMatch());
107 if (match.getLayer3Match() != null) {
111 builder.append(MATCH_SEPARATOR);
113 builder.append(match.getLayer3Match());
115 if (match.getLayer4Match() != null) {
119 builder.append(MATCH_SEPARATOR);
121 builder.append(match.getLayer4Match());
123 if (match.getMetadata() != null) {
127 builder.append(MATCH_SEPARATOR);
129 builder.append(match.getMetadata());
131 if (match.getProtocolMatchFields() != null) {
135 builder.append(MATCH_SEPARATOR);
137 builder.append(match.getProtocolMatchFields());
139 if (match.getTcpFlagMatch() != null) {
143 builder.append(MATCH_SEPARATOR);
145 builder.append(match.getTcpFlagMatch());
147 if (match.getTunnel() != null) {
151 builder.append(MATCH_SEPARATOR);
153 builder.append(match.getTunnel());
155 if (match.getVlanMatch() != null) {
159 builder.append(MATCH_SEPARATOR);
161 builder.append(match.getVlanMatch());
164 // only one augmentation is used in Match at the moment;
165 // if in the future there will be more of them, similar handling has to be implemented
166 GeneralAugMatchNodesNodeTableFlow generalAug = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
167 if(generalAug != null && generalAug.getExtensionList() != null) {
168 TreeSet<String> extensionAugmentationStrings = new TreeSet<>(new Comparator<String>() {
170 public int compare(String a, String b) {
171 return Strings.nullToEmpty(a).compareTo(Strings.nullToEmpty(b));
174 for (ExtensionList e : generalAug.getExtensionList()) {
175 Extension ext = e.getExtension();
176 // only one augmentation is used in Extension at the moment;
177 // if in the future there will be more of them, similar handling has to be implemented,
178 // probing augmentations one by one and adding their toString results to our TreeSet
179 // (and every List<> in them needs to be cast to Set<> to avoid non-equivalence
180 // due to different element order, and possible element duplication)
181 NxAugMatchNodesNodeTableFlow nxAug = ext.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
183 extensionAugmentationStrings.add(nxAug.toString());
188 builder.append(MATCH_SEPARATOR);
190 builder.append("GeneralAugMatchNodesNodeTableFlow[<ExtensionList>=")
191 .append(Joiner.on(", ").skipNulls().join(extensionAugmentationStrings))
194 builder.append(MATCH_SUFFIX);
196 return builder.toString();