Merge "L3: Add eth to br-ex"
[ovsdb.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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
13 import org.opendaylight.sfc.provider.api.SfcProviderServiceFunctionAPI;
14 import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.RspName;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfName;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.RenderedServicePaths;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePathKey;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.ServiceFunctions;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionKey;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
25 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
26 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sl.rev140701.data.plane.locator.locator.type.Ip;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.sfc.classifier.rev150105.Classifiers;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SfcUtils {
35     private static final Logger LOG = LoggerFactory.getLogger(SfcUtils.class);
36     private MdsalUtils mdsalUtils;
37
38     public SfcUtils(MdsalUtils mdsalUtils) {
39         this.mdsalUtils = mdsalUtils;
40     }
41
42     public InstanceIdentifier<Classifiers> getClassifierIid() {
43         return InstanceIdentifier.create(Classifiers.class);
44     }
45
46     public InstanceIdentifier<RenderedServicePaths> getRspsId() {
47         return InstanceIdentifier.builder(RenderedServicePaths.class).build();
48     }
49
50     public InstanceIdentifier<RenderedServicePath> getRspId(String rspName) {
51         return InstanceIdentifier.builder(RenderedServicePaths.class)
52                 .child(RenderedServicePath.class, new RenderedServicePathKey(new RspName(rspName))).build();
53     }
54
55     public InstanceIdentifier<ServiceFunction> getSfId(String sfName) {
56         return InstanceIdentifier.builder(ServiceFunctions.class)
57                 .child(ServiceFunction.class, new ServiceFunctionKey(SfName.getDefaultInstance(sfName))).build();
58     }
59
60     public RenderedServicePath getRsp(String rspName) {
61         return mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, getRspId(rspName));
62     }
63
64     public RenderedServicePath getRspforSfp(String sfpName) {
65         RenderedServicePath rspFound = null;
66         RenderedServicePaths rsps = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, this.getRspsId());
67         if (rsps != null) {
68             for (RenderedServicePath rsp : rsps.getRenderedServicePath()) {
69                 if (rsp.getParentServiceFunctionPath() != null) {
70                     if (rsp.getParentServiceFunctionPath().getValue().equals(sfpName)) {
71                         rspFound = rsp;
72                     }
73                 }
74             }
75         }
76         return rspFound;
77     }
78
79     public ServiceFunctionPath getSfp(String redirectSfc) {
80         ServiceFunctionPath sfpFound = null;
81         ServiceFunctionPaths sfps = SfcProviderServicePathAPI.readAllServiceFunctionPaths();
82         if (sfps != null) {
83             for (ServiceFunctionPath sfp: sfps.getServiceFunctionPath()) {
84                 if (sfp.getServiceChainName().getValue().equalsIgnoreCase(redirectSfc)) {
85                     sfpFound = sfp;
86                 }
87             }
88         }
89         return sfpFound;
90     }
91
92     public IpAddress getSfIpAddress(String sfname) {
93         ServiceFunction serviceFunction =
94                 SfcProviderServiceFunctionAPI.readServiceFunction(SfName.getDefaultInstance(sfname));
95
96         if (serviceFunction == null) {
97             LOG.info("Failed to read ServiceFunction: {}", sfname);
98             return null;
99         }
100
101         return getSfIpAddress(serviceFunction);
102     }
103
104     public IpAddress getSfIpAddress(ServiceFunction serviceFunction) {
105         if (serviceFunction == null) {
106             LOG.info("getSfIp: Servicefunction is null");
107             return null;
108         }
109
110         Ip ipLocator = (Ip) serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
111         return ipLocator.getIp();
112     }
113
114     public PortNumber getSfPort(ServiceFunction serviceFunction) {
115         if (serviceFunction == null) {
116             LOG.info("getSfIp: Servicefunction is null");
117             return null;
118         }
119
120         Ip ipLocator = (Ip) serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
121         return ipLocator.getPort();
122     }
123
124     public Ip getSfIp(ServiceFunction serviceFunction) {
125         if (serviceFunction == null) {
126             LOG.info("getSfIp: Servicefunction is null");
127             return null;
128         }
129
130         return (Ip)serviceFunction.getSfDataPlaneLocator().get(0).getLocatorType();
131     }
132
133     public String getSfDplName(ServiceFunction serviceFunction) {
134         String sfDplName = null;
135         if (serviceFunction == null) {
136             LOG.warn("getSfDplName: Servicefunction is null");
137             return null;
138         }
139
140         sfDplName = serviceFunction.getSfDataPlaneLocator().get(0).getName().getValue();
141         return sfDplName;
142     }
143
144     public Ip getSffIp(ServiceFunctionForwarder serviceFunctionForwarder) {
145         if (serviceFunctionForwarder == null) {
146             LOG.info("getSfIp: ServicefunctionForwarder is null");
147             return null;
148         }
149
150         return (Ip)serviceFunctionForwarder.getSffDataPlaneLocator().get(0).getDataPlaneLocator().getLocatorType();
151     }
152 }