2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.openstack.sfc.translator;
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;
34 import java.util.List;
37 * Utility methods to read OpenDaylight SFC models.
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);
50 private final DataBroker dataBroker;
51 private final MdsalUtils mdsalUtils;
53 public SfcMdsalHelper(DataBroker dataBroker) {
54 this.dataBroker = dataBroker;
55 mdsalUtils = new MdsalUtils(this.dataBroker);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
145 private InstanceIdentifier<Acl> getAclPath(AclKey aclKey) {
146 return accessListIid.builder().child(Acl.class, aclKey).build();
149 private InstanceIdentifier<ServiceFunction> getSFPath(ServiceFunctionKey key) {
150 return sfIid.builder().child(ServiceFunction.class, key).build();
153 private InstanceIdentifier<ServiceFunctionForwarder> getSFFPath(ServiceFunctionForwarderKey key) {
154 return sffIid.builder().child(ServiceFunctionForwarder.class, key).build();
157 private InstanceIdentifier<ServiceFunctionChain> getSFCPath(ServiceFunctionChainKey key) {
158 return sfcIid.builder().child(ServiceFunctionChain.class, key).build();
161 private InstanceIdentifier<ServiceFunctionPath> getSFPPath(ServiceFunctionPathKey key) {
162 return sfpIid.builder().child(ServiceFunctionPath.class, key).build();
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()) {
171 List<ServiceFunctionForwarder> existingSffList = existingSffs.getServiceFunctionForwarder();
172 for (ServiceFunctionForwarder sff : existingSffList) {
173 if (sff.getIpMgmtAddress().getIpv4Address().equals(new Ipv4Address(ipAddress))) {
181 private <D extends DataObject> void mdsalPutWrapper(LogicalDatastoreType dataStore, InstanceIdentifier<D> iid, D data) {
183 mdsalUtils.put(dataStore, iid, data);
184 } catch (Exception e) {
185 LOG.error("Exception while putting data in data store {} : {}",iid, data, e);