add rsp option
[netvirt.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / workaround / NetvirtSfcWorkaroundOF13Provider.java
1 /*
2  * Copyright © 2015 Red Hat, 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.ovsdb.openstack.netvirt.sfc.workaround;
10
11 import com.google.common.base.Preconditions;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
19 import org.opendaylight.ovsdb.openstack.netvirt.sfc.INetvirtSfcOF13Provider;
20 import org.opendaylight.ovsdb.openstack.netvirt.sfc.ISfcClassifierService;
21 import org.opendaylight.ovsdb.openstack.netvirt.sfc.NshUtils;
22 import org.opendaylight.ovsdb.openstack.netvirt.sfc.SfcUtils;
23 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
24 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
27 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
28 import org.opendaylight.sfc.provider.api.SfcProviderServiceFunctionAPI;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sl.rev140701.data.plane.locator.locator.type.Ip;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.path.first.hop.info.RenderedServicePathFirstHop;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.Acl;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.Ace;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.ace.Matches;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.acl.rev150105.RedirectToSfc;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.Classifier;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.Bridges;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.bridges.Bridge;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.classifiers.classifier.sffs.Sff;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
56 import org.osgi.framework.ServiceReference;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 public class NetvirtSfcWorkaroundOF13Provider implements INetvirtSfcOF13Provider {
61     private static final Logger LOG = LoggerFactory.getLogger(NetvirtSfcWorkaroundOF13Provider.class);
62     private volatile NodeCacheManager nodeCacheManager;
63     private volatile Southbound southbound;
64     private volatile ISfcClassifierService sfcClassifierService;
65     private static final short SFC_TABLE = 150;
66     private static final int GPE_PORT = 6633;
67     private static final String NETWORK_TYPE_VXLAN = "vxlan";
68     private MdsalUtils mdsalUtils;
69     private SfcUtils sfcUtils;
70     private static final String VXGPE = "vxgpe";
71     private static final String TUNNEL_DST = "192.168.120.31";
72     private static final String TUNNEL_VNID = "10";
73     public static final String TUNNEL_ENDPOINT_KEY = "local_ip";
74
75     public NetvirtSfcWorkaroundOF13Provider(final DataBroker dataBroker, MdsalUtils mdsalUtils, SfcUtils sfcUtils) {
76         Preconditions.checkNotNull(dataBroker, "Input dataBroker cannot be NULL!");
77         Preconditions.checkNotNull(mdsalUtils, "Input mdsalUtils cannot be NULL!");
78         Preconditions.checkNotNull(sfcUtils, "Input sfcUtils cannot be NULL!");
79
80         this.mdsalUtils = mdsalUtils;
81         this.sfcUtils = sfcUtils;
82         //this.setDependencies(null);
83     }
84
85     public void setSfcClassifierService(ISfcClassifierService sfcClassifierService) {
86         this.sfcClassifierService = sfcClassifierService;
87     }
88
89     @Override
90     public void addClassifierRules(Bridge bridge, Acl acl) {
91
92     }
93
94     @Override
95     public void addClassifierRules(Bridges bridges, Acl acl) {
96         Preconditions.checkNotNull(bridges, "Input bridges cannot be NULL!");
97         Preconditions.checkNotNull(acl, "Input acl cannot be NULL!");
98     }
99
100     @Override
101     public void removeClassifierRules(Sff sff, Acl acl) {
102
103     }
104
105     @Override
106     public void addClassifierRules(Acl acl) {
107         String aclName = acl.getAclName();
108         Classifiers classifiers = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, sfcUtils.getClassifierIid());
109         if (classifiers == null) {
110             LOG.debug("addClassifierRules: No Classifiers found");
111             return;
112         }
113
114         LOG.debug("addClassifierRules: Classifiers: {}", classifiers);
115         for (Classifier classifier : classifiers.getClassifier()) {
116             if (classifier.getAcl().equals(aclName)) {
117                 for (Ace ace : acl.getAccessListEntries().getAce()) {
118                     processAclEntry(ace);
119                 }
120             }
121         }
122     }
123
124     @Override
125     public void removeClassifierRules(Acl acl) {
126
127     }
128
129     private void processAclEntry(Ace entry) {
130         Matches matches = entry.getMatches();
131         Preconditions.checkNotNull(matches, "Input bridges cannot be NULL!");
132
133         RenderedServicePath rsp = getRenderedServicePath(entry);
134         if (rsp == null) {
135             LOG.warn("Failed to get renderedServicePatch for entry: {}", entry);
136             return;
137         }
138
139         handleRenderedServicePath2(rsp, entry);
140     }
141
142     private void handleRenderedServicePath(RenderedServicePath rsp, Ace entry) {
143         LOG.info("handleRenderedServicePath: RSP: {}", rsp);
144
145         Matches matches = entry.getMatches();
146         if (matches == null) {
147             LOG.warn("processAclEntry: matches not found");
148             return;
149         }
150
151         List<RenderedServicePathHop> pathHopList = rsp.getRenderedServicePathHop();
152         if (pathHopList.isEmpty()) {
153             LOG.warn("handleRenderedServicePath: RSP {} has empty hops!!", rsp.getName());
154             return;
155         }
156         LOG.info("handleRenderedServicePath: pathHopList: {}", pathHopList);
157
158         final List<Node> bridgeNodes = nodeCacheManager.getBridgeNodes();
159         if (bridgeNodes == null || bridgeNodes.isEmpty()) {
160             LOG.warn("handleRenderedServicePath: There are no bridges to process");
161             return;
162         }
163         for (Node bridgeNode : bridgeNodes) {
164             OvsdbBridgeAugmentation ovsdbBridgeAugmentation = southbound.getBridge(bridgeNode, "br-int");
165             if (ovsdbBridgeAugmentation == null) {
166                 continue;
167             }
168             long vxGpeOfPort = getOFPort(bridgeNode, VXGPE);
169             if (vxGpeOfPort == 0L) {
170                 LOG.warn("programAclEntry: Could not identify tunnel port {} -> OF ({}) on {}",
171                         VXGPE, vxGpeOfPort, bridgeNode);
172                 continue;
173             }
174             long dataPathId = southbound.getDataPathId(bridgeNode);
175             if (dataPathId == 0L) {
176                 LOG.warn("programAclEntry: Could not identify datapathId on {}", bridgeNode);
177                 continue;
178             }
179
180             // Find the first Hop within an RSP.
181             // The classifier flow needs to send all matched traffic to this first hop SFF.
182             RenderedServicePathFirstHop firstRspHop = SfcProviderRenderedPathAPI
183                     .readRenderedServicePathFirstHop(rsp.getName());
184
185             LOG.info("handleRenderedServicePath: firstRspHop: {}", firstRspHop);
186             LOG.debug("handleRenderedServicePath: First Hop IPAddress = {}, Port = {}",
187                     firstRspHop.getIp().getIpv4Address().getValue(),
188                     firstRspHop.getPort().getValue());
189
190             NshUtils nshHeader = new NshUtils();
191             nshHeader.setNshMetaC1(NshUtils.convertIpAddressToLong(new Ipv4Address(TUNNEL_DST)));
192             nshHeader.setNshMetaC2(Long.parseLong(TUNNEL_VNID)); // get from register //get from
193             nshHeader.setNshNsp(rsp.getPathId());
194
195             RenderedServicePathHop firstHop = pathHopList.get(0);
196             RenderedServicePathHop lastHop = pathHopList.get(pathHopList.size()-1);
197             ServiceFunction serviceFunction =
198                     SfcProviderServiceFunctionAPI.readServiceFunction(
199                             SfName.getDefaultInstance(firstHop.getServiceFunctionName().getValue()));
200             if (serviceFunction == null) {
201                 LOG.warn("programAclEntry: Could not identify ServiceFunction {} on {}",
202                         firstHop.getServiceFunctionName().getValue(), bridgeNode);
203                 continue;
204             }
205
206             nshHeader.setNshNsi(firstHop.getServiceIndex());
207             // workaround: bypass sff and got directly to sf
208             //nshHeader.setNshTunIpDst(firstRspHop.getIp().getIpv4Address());
209             IpAddress sfIpAddress = sfcUtils.getSfIpAddress(serviceFunction);
210             String sfDplName = sfcUtils.getSfDplName(serviceFunction);
211             //sfcUtils.getSfIp(firstHop.getServiceFunctionName().getValue());
212             nshHeader.setNshTunIpDst(sfIpAddress.getIpv4Address());
213             nshHeader.setNshTunUdpPort(firstRspHop.getPort());
214             LOG.debug("handleRenderedServicePath: NSH Header = {}", nshHeader);
215
216             sfcClassifierService.programIngressClassifier(dataPathId, entry.getRuleName(), matches,
217                     nshHeader, vxGpeOfPort, true);
218
219             sfcClassifierService.program_sfEgress(dataPathId, GPE_PORT, true);
220             long sfOfPort = getSfPort(bridgeNode, sfDplName);
221
222             String sfMac = getMacFromExternalIds(bridgeNode, sfDplName);
223             String sfIpString = new String(sfIpAddress.getValue());
224             LOG.info("handleRenderedServicePath: sfDplName: {}, sfMac: {}, sfOfPort: {}, sfIpAddress: {}",
225                     sfDplName, sfMac, sfOfPort, sfIpString);
226             if (sfMac != null) { // install if the sf is on this bridge, expand when using multiple bridges
227                 sfcClassifierService.program_sfIngress(dataPathId, GPE_PORT, sfOfPort, sfIpString, sfDplName, true);
228                 sfcClassifierService.programStaticArpEntry(dataPathId, 0L, sfMac, sfIpString, true);
229             }
230
231             short lastServiceindex = (short)((lastHop.getServiceIndex()).intValue() - 1);
232             sfcClassifierService.programEgressClassifier1(dataPathId, vxGpeOfPort, rsp.getPathId(),
233                     lastServiceindex, (int)sfOfPort, 0, (short)0, true);
234             sfcClassifierService.programEgressClassifier2(dataPathId, vxGpeOfPort, rsp.getPathId(),
235                     lastServiceindex, (int)sfOfPort, 0, true);
236
237             sfcClassifierService.programSfcTable(dataPathId, vxGpeOfPort, SFC_TABLE, true);
238         }
239     }
240
241     private void handleRenderedServicePath2(RenderedServicePath rsp, Ace entry) {
242         LOG.info("handleRenderedServicePath: RSP: {}", rsp);
243
244         Matches matches = entry.getMatches();
245         if (matches == null) {
246             LOG.warn("processAclEntry: matches not found");
247             return;
248         }
249
250         List<RenderedServicePathHop> pathHopList = rsp.getRenderedServicePathHop();
251         if (pathHopList.isEmpty()) {
252             LOG.warn("handleRenderedServicePath: RSP {} has empty hops!!", rsp.getName());
253             return;
254         }
255         LOG.info("handleRenderedServicePath: pathHopList: {}", pathHopList);
256
257         RenderedServicePathFirstHop firstRspHop = SfcProviderRenderedPathAPI
258                 .readRenderedServicePathFirstHop(rsp.getName());
259         LOG.info("handleRenderedServicePath: firstRspHop: {}", firstRspHop);
260
261         RenderedServicePathHop firstHop = pathHopList.get(0);
262         RenderedServicePathHop lastHop = pathHopList.get(pathHopList.size()-1);
263
264         final List<Node> bridgeNodes = nodeCacheManager.getBridgeNodes();
265         if (bridgeNodes == null || bridgeNodes.isEmpty()) {
266             LOG.warn("handleRenderedServicePath: There are no bridges to process");
267             return;
268         }
269         for (RenderedServicePathHop hop : pathHopList) {
270             for (Node bridgeNode : bridgeNodes) {
271                 long vxGpeOfPort = getOFPort(bridgeNode, VXGPE);
272                 if (vxGpeOfPort == 0L) {
273                     LOG.warn("programAclEntry: Could not identify gpe vtep {} -> OF ({}) on {}",
274                             VXGPE, vxGpeOfPort, bridgeNode);
275                     continue;
276                 }
277                 long dataPathId = southbound.getDataPathId(bridgeNode);
278                 if (dataPathId == 0L) {
279                     LOG.warn("programAclEntry: Could not identify datapathId on {}", bridgeNode);
280                     continue;
281                 }
282
283                 ServiceFunction serviceFunction =
284                         SfcProviderServiceFunctionAPI.readServiceFunction(firstHop.getServiceFunctionName());
285                 if (serviceFunction == null) {
286                     LOG.warn("programAclEntry: Could not identify ServiceFunction {} on {}",
287                             firstHop.getServiceFunctionName().getValue(), bridgeNode);
288                     continue;
289                 }
290                 ServiceFunctionForwarder serviceFunctionForwarder =
291                         SfcProviderServiceForwarderAPI
292                                 .readServiceFunctionForwarder(hop.getServiceFunctionForwarder());
293                 if (serviceFunctionForwarder == null) {
294                     LOG.warn("programAclEntry: Could not identify ServiceFunctionForwarder {} on {}",
295                             firstHop.getServiceFunctionName().getValue(), bridgeNode);
296                     continue;
297                 }
298
299                 handleSf(bridgeNode, serviceFunction);
300                 handleSff(bridgeNode, serviceFunctionForwarder, serviceFunction, hop, firstHop, lastHop,
301                         entry.getRuleName(), matches, vxGpeOfPort, rsp);
302                 if (firstHop == lastHop) {
303                     handleSff(bridgeNode, serviceFunctionForwarder, serviceFunction, hop, null, lastHop,
304                             entry.getRuleName(), matches, vxGpeOfPort, rsp);
305                 }
306             }
307         }
308     }
309
310     private void handleSff(Node bridgeNode, ServiceFunctionForwarder serviceFunctionForwarder,
311                            ServiceFunction serviceFunction,
312                            RenderedServicePathHop hop,
313                            RenderedServicePathHop firstHop,
314                            RenderedServicePathHop lastHop,
315                            String ruleName, Matches matches,
316                            long vxGpeOfPort, RenderedServicePath rsp) {
317         long dataPathId = southbound.getDataPathId(bridgeNode);
318
319         if (hop == firstHop) {
320             NshUtils nshHeader = new NshUtils();
321             nshHeader.setNshNsp(rsp.getPathId());
322             nshHeader.setNshNsi(firstHop.getServiceIndex());
323             if (isSffOnBridge(bridgeNode, serviceFunctionForwarder)) {
324                 Ip ip = sfcUtils.getSfIp(serviceFunction);
325                 nshHeader.setNshTunIpDst(ip.getIp().getIpv4Address());
326                 nshHeader.setNshTunUdpPort(ip.getPort());
327             } else {
328                 Ip ip = sfcUtils.getSffIp(serviceFunctionForwarder);
329                 nshHeader.setNshTunIpDst(ip.getIp().getIpv4Address());
330                 nshHeader.setNshTunUdpPort(ip.getPort());
331             }
332             sfcClassifierService.programIngressClassifier(dataPathId, ruleName, matches,
333                     nshHeader, vxGpeOfPort, true);
334         } else if (hop == lastHop) {
335             short lastServiceindex = (short)((lastHop.getServiceIndex()).intValue() - 1);
336             String sfDplName = sfcUtils.getSfDplName(serviceFunction);
337             long sfOfPort = getSfPort(bridgeNode, sfDplName);
338             sfcClassifierService.programEgressClassifier1(dataPathId, vxGpeOfPort, rsp.getPathId(),
339                     lastServiceindex, (int)sfOfPort, 0, (short)0, true);
340             sfcClassifierService.programEgressClassifier2(dataPathId, vxGpeOfPort, rsp.getPathId(),
341                     lastServiceindex, (int)sfOfPort, 0, true);
342         } else {
343             // add typical sff flows
344         }
345
346         sfcClassifierService.programSfcTable(dataPathId, vxGpeOfPort, SFC_TABLE, true);
347     }
348
349     void handleSf(Node bridgeNode, ServiceFunction serviceFunction) {
350         if (isSfOnBridge(bridgeNode, serviceFunction)) {
351             long dataPathId = southbound.getDataPathId(bridgeNode);
352             Ip ip = sfcUtils.getSfIp(serviceFunction);
353             String sfIpAddr = String.valueOf(ip.getIp().getValue());
354             int sfIpPort = ip.getPort().getValue(); //GPE_PORT
355             String sfDplName = sfcUtils.getSfDplName(serviceFunction);
356             long sfOfPort = getSfPort(bridgeNode, sfDplName);
357             String sfMac = getMacFromExternalIds(bridgeNode, sfDplName);
358             //should be sffdplport, but they should all be the same 6633/4790
359             sfcClassifierService.program_sfEgress(dataPathId, sfIpPort, true);
360             sfcClassifierService.program_sfIngress(dataPathId, sfIpPort, sfOfPort, sfIpAddr, sfDplName, true);
361             sfcClassifierService.programStaticArpEntry(dataPathId, 0L, sfMac, sfIpAddr, true);
362         }
363     }
364
365     private boolean isSffOnBridge(Node bridgeNode, ServiceFunctionForwarder serviceFunctionForwarder) {
366         String local_ip = "";
367         Ip ip = sfcUtils.getSffIp(serviceFunctionForwarder);
368         Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
369         if (ovsdbNode != null) {
370             OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
371             if (ovsdbNodeAugmentation != null && ovsdbNodeAugmentation.getOpenvswitchOtherConfigs() != null) {
372                 southbound.getOtherConfig(ovsdbNode, OvsdbTables.OPENVSWITCH, TUNNEL_ENDPOINT_KEY);
373             }
374
375         }
376         return local_ip.equals(String.valueOf(ip.getIp().getValue()));
377     }
378
379     private boolean isSfOnBridge(Node bridgeNode, ServiceFunction serviceFunction) {
380         String sfDplName = sfcUtils.getSfDplName(serviceFunction);
381         long sfOfPort = getSfPort(bridgeNode, sfDplName);
382         return sfOfPort != 0L;
383     }
384
385     private RenderedServicePath getRenderedServicePath (Ace entry) {
386         RenderedServicePath rsp = null;
387         RedirectToSfc sfcRedirect = entry.getActions().getAugmentation(RedirectToSfc.class);
388         LOG.debug("Processing ACL entry = {} sfcRedirect = {}", entry.getRuleName(), sfcRedirect);
389         if (sfcRedirect == null) {
390             LOG.warn("processAClEntry: sfcRedirect is null");
391             return null;
392         }
393
394         if (sfcRedirect.getRspName() != null) {
395             rsp = getRenderedServicePathFromRsp(sfcRedirect.getRspName());
396         } else if (sfcRedirect.getSfpName() != null) {
397             LOG.warn("getRenderedServicePath: sfp not handled yet");
398         } else {
399             rsp = getRenderedServicePathFromSfc(entry);
400         }
401         LOG.info("getRenderedServicePath: rsp: {}", rsp);
402         return rsp;
403     }
404
405     private RenderedServicePath getRenderedServicePathFromRsp(String rspName) {
406         return sfcUtils.getRsp(rspName);
407     }
408
409     private RenderedServicePath getRenderedServicePathFromSfc (Ace entry) {
410         RedirectToSfc sfcRedirect = entry.getActions().getAugmentation(RedirectToSfc.class);
411         LOG.debug("Processing ACL entry = {} sfcRedirect = {}", entry.getRuleName(), sfcRedirect);
412         if (sfcRedirect == null) {
413             LOG.warn("processAClEntry: sfcRedirect is null");
414             return null;
415         }
416
417         String sfcName = sfcRedirect.getSfcName();
418         ServiceFunctionPath sfp = sfcUtils.getSfp(sfcName);
419         if (sfp == null || sfp.getName() == null) {
420             LOG.warn("There is no configured SFP with sfcName = {}; so skip installing the ACL entry!!", sfcName);
421             return null;
422         }
423
424         LOG.debug("Processing Redirect to SFC = {}, SFP = {}", sfcName, sfp);
425         // If RSP doesn't exist, create an RSP.
426         String sfpName = sfp.getName().getValue();
427         RenderedServicePath rsp = sfcUtils.getRspforSfp(sfpName);
428         String rspName = sfp.getName().getValue() + "_rsp";
429         if (rsp == null) {
430             LOG.info("No configured RSP corresponding to SFP = {}, Creating new RSP = {}", sfpName, rspName);
431             CreateRenderedPathInput rspInput = new CreateRenderedPathInputBuilder()
432                     .setParentServiceFunctionPath(sfpName)
433                     .setName(rspName)
434                     .setSymmetric(sfp.isSymmetric())
435                     .build();
436             rsp = SfcProviderRenderedPathAPI.createRenderedServicePathAndState(sfp, rspInput);
437             if (rsp == null) {
438                 LOG.warn("failed to add RSP");
439                 return null;
440             }
441
442             // If SFP is symmetric, create RSP in the reverse direction.
443             if (sfp.isSymmetric()) {
444                 LOG.info("SFP = {} is symmetric, installing RSP in the reverse direction!!", sfpName);
445                 String rspNameRev = rspName + "-Reverse";
446                 RenderedServicePath rspReverse = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL,
447                         sfcUtils.getRspId(rspNameRev));
448                 if (rspReverse == null) {
449                     rspReverse = SfcProviderRenderedPathAPI.createSymmetricRenderedServicePathAndState(rsp);
450                     if (rspReverse == null) {
451                         LOG.warn("failed to add reverse RSP");
452                         return null;
453                     }
454                 }
455             }
456         }
457         return rsp;
458     }
459
460     // loop through all ports looking for vxlan types, skip vxgpe, keep the rest
461     // first pass we only have two tunnels: one for normal vxlan and the other for gpe
462     // so just return the first non-gpe vxlan port
463     private long getTunnelOfPort(Node bridgeNode, String vxGpePortName) {
464         long port = 0L;
465         List<OvsdbTerminationPointAugmentation> ovsdbTerminationPointAugmentations =
466                 southbound.getTerminationPointsOfBridge(bridgeNode);
467         if (!ovsdbTerminationPointAugmentations.isEmpty()) {
468             for (OvsdbTerminationPointAugmentation terminationPointAugmentation :
469                     ovsdbTerminationPointAugmentations) {
470                 if (terminationPointAugmentation.getInterfaceType() ==
471                         SouthboundConstants.OVSDB_INTERFACE_TYPE_MAP.get(NETWORK_TYPE_VXLAN)) {
472                     if (!terminationPointAugmentation.getName().equals(vxGpePortName)) {
473                         port = terminationPointAugmentation.getOfport();
474                         break;
475                     }
476                 }
477             }
478         }
479         return port;
480     }
481
482     private long getSfPort(Node bridgeNode, String sfPortName) {
483         long port = 0L;
484         port = getOFPort(bridgeNode, sfPortName);
485         return port;
486     }
487
488     private long getOFPort(Node bridgeNode, String portName) {
489         long ofPort = 0L;
490         OvsdbTerminationPointAugmentation port =
491                 southbound.extractTerminationPointAugmentation(bridgeNode, portName);
492         if (port != null) {
493             ofPort = southbound.getOFPort(port);
494         }
495         if (ofPort == 0L) {
496             for (int i = 0; i < 5; i++) {
497                 LOG.info("Looking for ofPort {}, try: {}", portName, i);
498                 if (ofPort == 0L) {
499                     TerminationPoint tp = southbound.readTerminationPoint(bridgeNode, null, portName);
500                     if (tp != null) {
501                         port = tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
502                         if (port != null) {
503                             ofPort = southbound.getOFPort(port);
504                             break;
505                         }
506                     }
507                 }
508                 try {
509                     Thread.sleep(1000);
510                 } catch (InterruptedException e) {
511                     e.printStackTrace();
512                 }
513             }
514         }
515         return ofPort;
516     }
517
518     private String getMacFromOptions(Node bridgeNode, String portName) {
519         String mac = null;
520         OvsdbTerminationPointAugmentation port = southbound.extractTerminationPointAugmentation(bridgeNode, portName);
521         LOG.info("getMac: portName: {}, bridgeNode: {},,, port: {}", portName, bridgeNode, port);
522         if (port != null && port.getOptions() != null) {
523             //mac = southbound.getOptionsValue(port.getOptions(), EXTERNAL_ID_VM_MAC);
524             for (Options option : port.getOptions()) {
525                 LOG.info("getMac: option: {}", option);
526                 if (option.getOption().equals(Constants.EXTERNAL_ID_VM_MAC)) {
527                     mac = option.getValue();
528                     break;
529                 }
530             }
531         }
532         return mac;
533     }
534
535     private String getMacFromExternalIds(Node bridgeNode, String portName) {
536         String mac = null;
537         OvsdbTerminationPointAugmentation port = southbound.extractTerminationPointAugmentation(bridgeNode, portName);
538         LOG.info("getMac: portName: {}, bridgeNode: {},,, port: {}", portName, bridgeNode, port);
539         if (port != null && port.getInterfaceExternalIds() != null) {
540             mac = southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_VM_MAC);
541         }
542         return mac;
543     }
544
545     @Override
546     public void setDependencies(ServiceReference serviceReference) {
547         nodeCacheManager = (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
548         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
549         sfcClassifierService =
550                 (ISfcClassifierService) ServiceHelper.getGlobalInstance(ISfcClassifierService.class, this);
551         LOG.info("sfcClassifierService= {}", sfcClassifierService);
552     }
553 }