1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
3 import com.google.common.base.Joiner;
4 import com.google.common.base.Strings;
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
12 import java.util.Comparator;
13 import java.util.TreeSet;
15 public class FlowIdUtils {
17 private static final String TABLE_ID_PREFIX = "t";
18 private static final String FLOWID_SEPARATOR = "|";
19 private static final String MATCH_PREFIX = "match[";
20 private static final String MATCH_SUFFIX = "]";
21 private static final String MATCH_SEPARATOR = ", ";
23 // *** flow from FlowTable (abstract parent) ***
26 * For flow without match specified (actually, only "drop all" flow)
28 * @param prefix String
31 public static FlowId newFlowId(String prefix) {
33 return new FlowId(prefix);
37 * FlowId based on match (with prefix like "t2|localL3|")
39 * @param tableId Short
40 * @param prefix String
44 public static FlowId newFlowId(Short tableId, String prefix, Match match) {
46 return new FlowId((tableId != null ? TABLE_ID_PREFIX + tableId + FLOWID_SEPARATOR : "")
47 + prefix + FLOWID_SEPARATOR
48 + formatMatch(match));
51 private static String formatMatch(Match match) {
52 StringBuilder builder = new StringBuilder(MATCH_PREFIX);
54 if (match.getEthernetMatch() != null) {
58 builder.append(MATCH_SEPARATOR);
60 builder.append(match.getEthernetMatch());
62 if (match.getIcmpv4Match() != null) {
66 builder.append(MATCH_SEPARATOR);
68 builder.append(match.getIcmpv4Match());
70 if (match.getIcmpv6Match() != null) {
74 builder.append(MATCH_SEPARATOR);
76 builder.append(match.getIcmpv6Match());
78 if (match.getInPhyPort() != null) {
82 builder.append(MATCH_SEPARATOR);
84 builder.append("inPhyPort=").append(match.getInPhyPort());
86 if (match.getInPort() != null) {
90 builder.append(MATCH_SEPARATOR);
92 builder.append("inPort=").append(match.getInPort());
94 if (match.getIpMatch() != null) {
98 builder.append(MATCH_SEPARATOR);
100 builder.append(match.getIpMatch());
102 if (match.getLayer3Match() != null) {
106 builder.append(MATCH_SEPARATOR);
108 builder.append(match.getLayer3Match());
110 if (match.getLayer4Match() != null) {
114 builder.append(MATCH_SEPARATOR);
116 builder.append(match.getLayer4Match());
118 if (match.getMetadata() != null) {
122 builder.append(MATCH_SEPARATOR);
124 builder.append(match.getMetadata());
126 if (match.getProtocolMatchFields() != null) {
130 builder.append(MATCH_SEPARATOR);
132 builder.append(match.getProtocolMatchFields());
134 if (match.getTcpFlagMatch() != null) {
138 builder.append(MATCH_SEPARATOR);
140 builder.append(match.getTcpFlagMatch());
142 if (match.getTunnel() != null) {
146 builder.append(MATCH_SEPARATOR);
148 builder.append(match.getTunnel());
150 if (match.getVlanMatch() != null) {
154 builder.append(MATCH_SEPARATOR);
156 builder.append(match.getVlanMatch());
159 // only one augmentation is used in Match at the moment;
160 // if in the future there will be more of them, similar handling has to be implemented
161 GeneralAugMatchNodesNodeTableFlow generalAug = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
162 if(generalAug != null && generalAug.getExtensionList() != null) {
163 TreeSet<String> extensionAugmentationStrings = new TreeSet<>(new Comparator<String>() {
165 public int compare(String a, String b) {
166 return Strings.nullToEmpty(a).compareTo(Strings.nullToEmpty(b));
169 for (ExtensionList e : generalAug.getExtensionList()) {
170 Extension ext = e.getExtension();
171 // only one augmentation is used in Extension at the moment;
172 // if in the future there will be more of them, similar handling has to be implemented,
173 // probing augmentations one by one and adding their toString results to our TreeSet
174 // (and every List<> in them needs to be cast to Set<> to avoid non-equivalence
175 // due to different element order, and possible element duplication)
176 NxAugMatchNodesNodeTableFlow nxAug = ext.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
178 extensionAugmentationStrings.add(nxAug.toString());
183 builder.append(MATCH_SEPARATOR);
185 builder.append("GeneralAugMatchNodesNodeTableFlow[<ExtensionList>=")
186 .append(Joiner.on(", ").skipNulls().join(extensionAugmentationStrings))
189 builder.append(MATCH_SUFFIX);
191 return builder.toString();