Update .gitreview for new repo
[netvirt.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / SfcUtils.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;
10
11 import java.util.List;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
14 import org.opendaylight.sfc.provider.api.SfcProviderAclAPI;
15 import org.opendaylight.sfc.provider.api.SfcProviderRenderedPathAPI;
16 import org.opendaylight.sfc.provider.api.SfcProviderServiceForwarderAPI;
17 import org.opendaylight.sfc.provider.api.SfcProviderServiceFunctionAPI;
18 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SffName;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.RenderedServicePaths;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.path.first.hop.info.RenderedServicePathFirstHop;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
25 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathKey;
26 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.rendered.service.path.RenderedServicePathHop;
27 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.ServiceFunctions;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionKey;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.service.function.forwarder.SffDataPlaneLocator;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sl.rev140701.data.plane.locator.locator.type.Ip;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.AccessLists;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.Acl;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.AccessListEntries;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev150317.access.lists.acl.access.list.entries.Ace;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.acl.rev150105.RedirectToSfc;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class SfcUtils {
48     private static final Logger LOG = LoggerFactory.getLogger(SfcUtils.class);
49     private MdsalUtils mdsalUtils;
50
51     public SfcUtils(MdsalUtils mdsalUtils) {
52         this.mdsalUtils = mdsalUtils;
53     }
54
55     public InstanceIdentifier<Classifiers> getClassifierIid() {
56         return InstanceIdentifier.create(Classifiers.class);
57     }
58
59     public InstanceIdentifier<RenderedServicePaths> getRspsId() {
60         return InstanceIdentifier.builder(RenderedServicePaths.class).build();
61     }
62
63     public InstanceIdentifier<RenderedServicePath> getRspId(String rspName) {
64         return InstanceIdentifier.builder(RenderedServicePaths.class)
65                 .child(RenderedServicePath.class, new RenderedServicePathKey(new RspName(rspName))).build();
66     }
67
68     public InstanceIdentifier<ServiceFunction> getSfId(String sfName) {
69         return InstanceIdentifier.builder(ServiceFunctions.class)
70                 .child(ServiceFunction.class, new ServiceFunctionKey(SfName.getDefaultInstance(sfName))).build();
71     }
72
73     public RenderedServicePath getRsp(String rspName) {
74         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, getRspId(rspName));
75     }
76
77     public RenderedServicePath getRspforSfp(String sfpName) {
78         RenderedServicePath rspFound = null;
79         RenderedServicePaths rsps = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, this.getRspsId());
80         if (rsps != null) {
81             for (RenderedServicePath rsp : rsps.getRenderedServicePath()) {
82                 if (rsp.getParentServiceFunctionPath() != null) {
83                     if (rsp.getParentServiceFunctionPath().getValue().equals(sfpName)) {
84                         rspFound = rsp;
85                     }
86                 }
87             }
88         }
89         return rspFound;
90     }
91
92     public ServiceFunctionPath getSfp(String sfcName) {
93         ServiceFunctionPath sfpFound = null;
94         ServiceFunctionPaths sfps = SfcProviderServicePathAPI.readAllServiceFunctionPaths();
95         if (sfps != null) {
96             for (ServiceFunctionPath sfp: sfps.getServiceFunctionPath()) {
97                 if (sfp.getServiceChainName().getValue().equalsIgnoreCase(sfcName)) {
98                     sfpFound = sfp;
99                 }
100             }
101         }
102         return sfpFound;
103     }
104
105     private AccessLists readAccessLists() {
106         InstanceIdentifier<AccessLists> path = InstanceIdentifier.create(AccessLists.class);
107         return mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, path);
108     }
109
110     public Ace getAce(RenderedServicePath rsp) {
111         return getAce(rsp.getName().getValue(), rsp.getParentServiceFunctionPath().getValue(),
112                 rsp.getServiceChainName().getValue());
113     }
114
115     // TODO: optimize this by adding a ACL to RSP mapping in the netvirt-classifier when the ACL is processed
116     public Ace getAce(String rspName, String sfpName, String sfcName) {
117         Ace aceFound = null;
118         AccessLists accessLists = readAccessLists();
119         if (accessLists != null) {
120             List<Acl> acls = accessLists.getAcl();
121             if (acls != null) {
122                 for (Acl acl : acls) {
123                     AccessListEntries accessListEntries = acl.getAccessListEntries();
124                     if (accessListEntries != null) {
125                         List<Ace> aces = accessListEntries.getAce();
126                         for (Ace ace : aces) {
127                             RedirectToSfc sfcRedirect = ace.getActions().getAugmentation(RedirectToSfc.class);
128                             if (sfcRedirect != null) {
129                                 if ((sfcRedirect.getRspName() != null && sfcRedirect.getRspName().equals(rspName)) ||
130                                     (sfcRedirect.getSfcName() != null && sfcRedirect.getSfcName().equals(sfcName)) ||
131                                     (sfcRedirect.getSfpName() != null && sfcRedirect.getSfpName().equals(sfpName))) {
132                                     aceFound = ace;
133                                     break;
134                                 }
135                             }
136                         }
137                     }
138                     if (aceFound != null) {
139                         break;
140                     }
141                 }
142             }
143         }
144
145         LOG.info("getAce: {}", aceFound);
146         return aceFound;
147     }
148
149     public IpAddress getSfIpAddress(String sfname) {
150         ServiceFunction serviceFunction =
151                 SfcProviderServiceFunctionAPI.readServiceFunction(SfName.getDefaultInstance(sfname));
152
153         if (serviceFunction == null) {
154             LOG.info("Failed to read ServiceFunction: {}", sfname);
155             return null;
156         }
157
158         return getSfIpAddress(serviceFunction);
159     }
160
161     public IpAddress getSfIpAddress(ServiceFunction serviceFunction) {
162         if (serviceFunction == null) {
163             LOG.info("getSfIp: Servicefunction is null");
164             return null;
165         }
166
167         Ip ipLocator = (Ip) serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
168         return ipLocator.getIp();
169     }
170
171     public PortNumber getSfPort(ServiceFunction serviceFunction) {
172         if (serviceFunction == null) {
173             LOG.info("getSfIp: Servicefunction is null");
174             return null;
175         }
176
177         Ip ipLocator = (Ip) serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
178         return ipLocator.getPort();
179     }
180
181     public Ip getSfIp(ServiceFunction serviceFunction) {
182         if (serviceFunction == null) {
183             LOG.info("getSfIp: Servicefunction is null");
184             return null;
185         }
186
187         return (Ip)serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
188     }
189
190     public String getSfDplName(ServiceFunction serviceFunction) {
191         String sfDplName = null;
192         if (serviceFunction == null) {
193             LOG.warn("getSfDplName: Servicefunction is null");
194             return null;
195         }
196
197         sfDplName = serviceFunction.getSfDataPlaneLocator().get(0).getName().getValue();
198         return sfDplName;
199     }
200
201     public Ip getSffIp(ServiceFunctionForwarder serviceFunctionForwarder) {
202         if (serviceFunctionForwarder != null &&
203                 serviceFunctionForwarder.getSffDataPlaneLocator() != null &&
204                 serviceFunctionForwarder.getSffDataPlaneLocator().get(0) != null &&
205                 serviceFunctionForwarder.getSffDataPlaneLocator().get(0).getDataPlaneLocator() != null) {
206             return (Ip)serviceFunctionForwarder.getSffDataPlaneLocator().get(0)
207                     .getDataPlaneLocator().getLocatorType();
208         } else {
209             LOG.info("getSffIp: ServiceFunctionForwarder is null");
210             return null;
211         }
212     }
213
214     public Ip getSffIp(SffName sffName) {
215         ServiceFunctionForwarder serviceFunctionForwarder =
216                 SfcProviderServiceForwarderAPI.readServiceFunctionForwarder(sffName);
217         return getSffIp(serviceFunctionForwarder);
218     }
219
220     public RenderedServicePathHop getFirstHop(RenderedServicePath rsp) {
221         List<RenderedServicePathHop> pathHopList = rsp.getRenderedServicePathHop();
222         if (pathHopList.isEmpty()) {
223             LOG.warn("handleRenderedServicePath: RSP {} has empty hops!!", rsp.getName());
224             return null;
225         }
226
227         return pathHopList.get(0);
228     }
229
230     public RenderedServicePathHop getLastHop(RenderedServicePath rsp) {
231         List<RenderedServicePathHop> pathHopList = rsp.getRenderedServicePathHop();
232         if (pathHopList.isEmpty()) {
233             LOG.warn("handleRenderedServicePath: RSP {} has empty hops!!", rsp.getName());
234             return null;
235         }
236
237         return pathHopList.get(pathHopList.size()-1);
238     }
239 }