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