Fix NPE triggered after disabling SG on a port
[netvirt.git] / openstack / sfc-translator / impl / src / main / java / org / opendaylight / netvirt / openstack / sfc / translator / SfcMdsalHelper.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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 package org.opendaylight.netvirt.openstack.sfc.translator;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
12 import org.opendaylight.netvirt.utils.mdsal.utils.MdsalUtils;
13 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.ServiceFunctions;
14 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionKey;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.ServiceFunctionChains;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChain;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChainKey;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.ServiceFunctionForwarders;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarder;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sff.rev140701.service.function.forwarders.ServiceFunctionForwarderKey;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.AclKey;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.util.List;
35
36 /**
37  * Utility methods to read OpenDaylight SFC models.
38  */
39 public class SfcMdsalHelper {
40     private static final Logger LOG = LoggerFactory.getLogger(SfcMdsalHelper.class);
41     private static InstanceIdentifier<AccessLists> accessListIid = InstanceIdentifier.create(AccessLists.class);
42     private static InstanceIdentifier<ServiceFunctions> sfIid = InstanceIdentifier.create(ServiceFunctions.class);
43     private static InstanceIdentifier<ServiceFunctionForwarders> sffIid =
44             InstanceIdentifier.create(ServiceFunctionForwarders.class);
45     private static InstanceIdentifier<ServiceFunctionChains> sfcIid =
46             InstanceIdentifier.create(ServiceFunctionChains.class);
47     private static InstanceIdentifier<ServiceFunctionPaths> sfpIid
48             = InstanceIdentifier.create(ServiceFunctionPaths.class);
49
50     private final DataBroker dataBroker;
51     private final MdsalUtils mdsalUtils;
52
53     public SfcMdsalHelper(DataBroker dataBroker) {
54         this.dataBroker = dataBroker;
55         mdsalUtils = new MdsalUtils(this.dataBroker);
56     }
57
58     //ACL Flow Classifier data store utility methods
59     public void addAclFlowClassifier(Acl aclFlowClassifier) {
60         InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
61         LOG.info("Write ACL FlowClassifier {} to config data store at {}",aclFlowClassifier, aclIid);
62         mdsalPutWrapper(LogicalDatastoreType.CONFIGURATION, aclIid, aclFlowClassifier);
63     }
64
65     public void updateAclFlowClassifier(Acl aclFlowClassifier) {
66         InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
67         LOG.info("Update ACL FlowClassifier {} in config data store at {}",aclFlowClassifier, aclIid);
68         mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, aclIid, aclFlowClassifier);
69     }
70
71     public void removeAclFlowClassifier(Acl aclFlowClassifier) {
72         InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
73         LOG.info("Remove ACL FlowClassifier {} from config data store at {}",aclFlowClassifier, aclIid);
74         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, aclIid);
75     }
76
77     //Service Function
78     public ServiceFunction readServiceFunction(ServiceFunctionKey sfKey) {
79         InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sfKey);
80         LOG.info("Read Service Function {} from config data store at {}",sfKey, sfIid);
81         return mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, sfIid);
82     }
83
84     public void addServiceFunction(ServiceFunction sf) {
85         InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sf.getKey());
86         LOG.info("Write Service Function {} to config data store at {}",sf, sfIid);
87         mdsalPutWrapper(LogicalDatastoreType.CONFIGURATION, sfIid, sf);
88     }
89
90     public void updateServiceFunction(ServiceFunction sf) {
91         InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sf.getKey());
92         LOG.info("Update Service Function {} in config data store at {}",sf, sfIid);
93         mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, sfIid, sf);
94     }
95
96     public void removeServiceFunction(ServiceFunctionKey sfKey) {
97         InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sfKey);
98         LOG.info("Remove Service Function {} from config data store at {}",sfKey, sfIid);
99         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sfIid);
100     }
101
102     //Service Function Forwarder
103     public ServiceFunctionForwarder readServiceFunctionForwarder(ServiceFunctionForwarderKey sffKey) {
104         InstanceIdentifier<ServiceFunctionForwarder> sffIid = getSFFPath(sffKey);
105         LOG.info("Read Service Function Forwarder from config data store at {}", sffIid);
106         return mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, sffIid);
107     }
108
109     public void addServiceFunctionForwarder(ServiceFunctionForwarder sff) {
110         InstanceIdentifier<ServiceFunctionForwarder> sffIid = getSFFPath(sff.getKey());
111         LOG.info("Write Service Function Forwarder {} to config data store at {}",sff, sffIid);
112         mdsalPutWrapper(LogicalDatastoreType.CONFIGURATION, sffIid, sff);
113     }
114
115     public void deleteServiceFunctionForwarder(ServiceFunctionForwarderKey sffKey) {
116         InstanceIdentifier<ServiceFunctionForwarder> sffIid = getSFFPath(sffKey);
117         LOG.info("Delete Service Function Forwarder from config data store at {}", sffIid);
118         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sffIid);
119     }
120
121     public void addServiceFunctionChain(ServiceFunctionChain sfc) {
122         InstanceIdentifier<ServiceFunctionChain> sfcIid = getSFCPath(sfc.getKey());
123         LOG.info("Write Service Function Chain {} to config data store at {}",sfc, sfcIid);
124         mdsalPutWrapper(LogicalDatastoreType.CONFIGURATION, sfcIid, sfc);
125     }
126
127     public void deleteServiceFunctionChain(ServiceFunctionChainKey sfcKey) {
128         InstanceIdentifier<ServiceFunctionChain> sfcIid = getSFCPath(sfcKey);
129         LOG.info("Remove Service Function Chain {} from config data store at {}",sfcKey, sfcIid);
130         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sfcIid);
131     }
132
133     public void addServiceFunctionPath(ServiceFunctionPath sfp) {
134         InstanceIdentifier<ServiceFunctionPath> sfpIid = getSFPPath(sfp.getKey());
135         LOG.info("Write Service Function Path {} to config data store at {}",sfp, sfpIid);
136         mdsalPutWrapper(LogicalDatastoreType.CONFIGURATION, sfpIid, sfp);
137     }
138
139     public void deleteServiceFunctionPath(ServiceFunctionPathKey sfpKey) {
140         InstanceIdentifier<ServiceFunctionPath> sfpIid = getSFPPath(sfpKey);
141         LOG.info("Delete Service Function Path from config data store at {}", sfpIid);
142         mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sfpIid);
143     }
144
145     private InstanceIdentifier<Acl> getAclPath(AclKey aclKey) {
146         return accessListIid.builder().child(Acl.class, aclKey).build();
147     }
148
149     private InstanceIdentifier<ServiceFunction> getSFPath(ServiceFunctionKey key) {
150         return sfIid.builder().child(ServiceFunction.class, key).build();
151     }
152
153     private InstanceIdentifier<ServiceFunctionForwarder> getSFFPath(ServiceFunctionForwarderKey key) {
154         return sffIid.builder().child(ServiceFunctionForwarder.class, key).build();
155     }
156
157     private InstanceIdentifier<ServiceFunctionChain> getSFCPath(ServiceFunctionChainKey key) {
158         return sfcIid.builder().child(ServiceFunctionChain.class, key).build();
159     }
160
161     private InstanceIdentifier<ServiceFunctionPath> getSFPPath(ServiceFunctionPathKey key) {
162         return sfpIid.builder().child(ServiceFunctionPath.class, key).build();
163     }
164
165     public ServiceFunctionForwarder getExistingSFF(String ipAddress) {
166         ServiceFunctionForwarders existingSffs = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, sffIid);
167         if (existingSffs != null
168                 && existingSffs.getServiceFunctionForwarder() != null
169                 && !existingSffs.getServiceFunctionForwarder().isEmpty()) {
170
171             List<ServiceFunctionForwarder> existingSffList = existingSffs.getServiceFunctionForwarder();
172             for (ServiceFunctionForwarder sff : existingSffList) {
173                 if (sff.getIpMgmtAddress().getIpv4Address().equals(new Ipv4Address(ipAddress))) {
174                     return sff;
175                 }
176             }
177         }
178         return null;
179     }
180
181     private <D extends DataObject> void mdsalPutWrapper(LogicalDatastoreType dataStore, InstanceIdentifier<D> iid, D data) {
182         try {
183             mdsalUtils.put(dataStore, iid, data);
184         } catch (Exception e) {
185             LOG.error("Exception while putting data in data store {} : {}",iid, data, e);
186         }
187     }
188 }