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