Add INFO.yaml for GBP
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / FlowIdUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import com.google.common.base.Joiner;
12 import com.google.common.base.Strings;
13
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;
21
22 import java.util.Comparator;
23 import java.util.TreeSet;
24
25 public class FlowIdUtils {
26
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 = ", ";
32
33     // *** flow from FlowTable (abstract parent) ***
34
35     /**
36      * For flow without match specified (actually, only "drop all" flow)
37      *
38      * @param prefix String
39      * @return FlowId
40      */
41     public static FlowId newFlowId(String prefix) {
42
43         return new FlowId(prefix);
44     }
45
46     /**
47      * FlowId based on match (with prefix like "t2|localL3|")
48      *
49      * @param tableId Short
50      * @param prefix String
51      * @param match Match
52      * @return FlowId
53      */
54     public static FlowId newFlowId(Short tableId, String prefix, Match match) {
55
56         return new FlowId((tableId != null ? TABLE_ID_PREFIX + tableId + FLOWID_SEPARATOR : "")
57                 + prefix + FLOWID_SEPARATOR
58                 + formatMatch(match));
59     }
60
61     private static String formatMatch(Match match) {
62         if (match == null) {
63             return StringUtils.EMPTY;
64         }
65         StringBuilder builder = new StringBuilder(MATCH_PREFIX);
66         boolean first = true;
67         if (match.getEthernetMatch() != null) {
68             if (first) {
69                 first = false;
70             } else {
71                 builder.append(MATCH_SEPARATOR);
72             }
73             builder.append(match.getEthernetMatch());
74         }
75         if (match.getIcmpv4Match() != null) {
76             if (first) {
77                 first = false;
78             } else {
79                 builder.append(MATCH_SEPARATOR);
80             }
81             builder.append(match.getIcmpv4Match());
82         }
83         if (match.getIcmpv6Match() != null) {
84             if (first) {
85                 first = false;
86             } else {
87                 builder.append(MATCH_SEPARATOR);
88             }
89             builder.append(match.getIcmpv6Match());
90         }
91         if (match.getInPhyPort() != null) {
92             if (first) {
93                 first = false;
94             } else {
95                 builder.append(MATCH_SEPARATOR);
96             }
97             builder.append("inPhyPort=").append(match.getInPhyPort());
98         }
99         if (match.getInPort() != null) {
100             if (first) {
101                 first = false;
102             } else {
103                 builder.append(MATCH_SEPARATOR);
104             }
105             builder.append("inPort=").append(match.getInPort());
106         }
107         if (match.getIpMatch() != null) {
108             if (first) {
109                 first = false;
110             } else {
111                 builder.append(MATCH_SEPARATOR);
112             }
113             builder.append(match.getIpMatch());
114         }
115         if (match.getLayer3Match() != null) {
116             if (first) {
117                 first = false;
118             } else {
119                 builder.append(MATCH_SEPARATOR);
120             }
121             builder.append(match.getLayer3Match());
122         }
123         if (match.getLayer4Match() != null) {
124             if (first) {
125                 first = false;
126             } else {
127                 builder.append(MATCH_SEPARATOR);
128             }
129             builder.append(match.getLayer4Match());
130         }
131         if (match.getMetadata() != null) {
132             if (first) {
133                 first = false;
134             } else {
135                 builder.append(MATCH_SEPARATOR);
136             }
137             builder.append(match.getMetadata());
138         }
139         if (match.getProtocolMatchFields() != null) {
140             if (first) {
141                 first = false;
142             } else {
143                 builder.append(MATCH_SEPARATOR);
144             }
145             builder.append(match.getProtocolMatchFields());
146         }
147         if (match.getTcpFlagsMatch() != null) {
148             if (first) {
149                 first = false;
150             } else {
151                 builder.append(MATCH_SEPARATOR);
152             }
153             builder.append(match.getTcpFlagsMatch());
154         }
155         if (match.getTunnel() != null) {
156             if (first) {
157                 first = false;
158             } else {
159                 builder.append(MATCH_SEPARATOR);
160             }
161             builder.append(match.getTunnel());
162         }
163         if (match.getVlanMatch() != null) {
164             if (first) {
165                 first = false;
166             } else {
167                 builder.append(MATCH_SEPARATOR);
168             }
169             builder.append(match.getVlanMatch());
170         }
171
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>() {
177                 @Override
178                 public int compare(String a, String b) {
179                     return Strings.nullToEmpty(a).compareTo(Strings.nullToEmpty(b));
180                 }
181             });
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);
190                 if (nxAug != null) {
191                     extensionAugmentationStrings.add(nxAug.toString());
192                 }
193             }
194
195             if (!first) {
196                 builder.append(MATCH_SEPARATOR);
197             }
198             builder.append("GeneralAugMatchNodesNodeTableFlow[<ExtensionList>=")
199                     .append(Joiner.on(", ").skipNulls().join(extensionAugmentationStrings))
200                     .append(']');
201         }
202         builder.append(MATCH_SUFFIX);
203
204         return builder.toString();
205     }
206
207 }