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.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.List;
36 * Utility methods to read OpenDaylight SFC models.
38 public class SfcMdsalHelper {
39 private static final Logger LOG = LoggerFactory.getLogger(SfcMdsalHelper.class);
40 private static InstanceIdentifier<AccessLists> accessListIid = InstanceIdentifier.create(AccessLists.class);
41 private static InstanceIdentifier<ServiceFunctions> sfIid = InstanceIdentifier.create(ServiceFunctions.class);
42 private static InstanceIdentifier<ServiceFunctionForwarders> sffIid =
43 InstanceIdentifier.create(ServiceFunctionForwarders.class);
44 private static InstanceIdentifier<ServiceFunctionChains> sfcIid =
45 InstanceIdentifier.create(ServiceFunctionChains.class);
46 private static InstanceIdentifier<ServiceFunctionPaths> sfpIid
47 = InstanceIdentifier.create(ServiceFunctionPaths.class);
49 private final DataBroker dataBroker;
50 private final MdsalUtils mdsalUtils;
52 public SfcMdsalHelper(DataBroker dataBroker) {
53 this.dataBroker = dataBroker;
54 mdsalUtils = new MdsalUtils(this.dataBroker);
57 //ACL Flow Classifier data store utility methods
58 public void addAclFlowClassifier(Acl aclFlowClassifier) {
59 InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
60 LOG.info("Write ACL FlowClassifier {} to config data store at {}",aclFlowClassifier, aclIid);
61 mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, aclIid, aclFlowClassifier);
64 public void updateAclFlowClassifier(Acl aclFlowClassifier) {
65 InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
66 LOG.info("Update ACL FlowClassifier {} in config data store at {}",aclFlowClassifier, aclIid);
67 mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, aclIid, aclFlowClassifier);
70 public void removeAclFlowClassifier(Acl aclFlowClassifier) {
71 InstanceIdentifier<Acl> aclIid = getAclPath(aclFlowClassifier.getKey());
72 LOG.info("Remove ACL FlowClassifier {} from config data store at {}",aclFlowClassifier, aclIid);
73 mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, aclIid);
77 public void addServiceFunction(ServiceFunction sf) {
78 InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sf.getKey());
79 LOG.info("Write Service Function {} to config data store at {}",sf, sfIid);
80 mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, sfIid, sf);
83 public void updateServiceFunction(ServiceFunction sf) {
84 InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sf.getKey());
85 LOG.info("Update Service Function {} in config data store at {}",sf, sfIid);
86 mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, sfIid, sf);
89 public void removeServiceFunction(ServiceFunctionKey sfKey) {
90 InstanceIdentifier<ServiceFunction> sfIid = getSFPath(sfKey);
91 LOG.info("Remove Service Function {} from config data store at {}",sfKey, sfIid);
92 mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sfIid);
94 private InstanceIdentifier<Acl> getAclPath(AclKey aclKey) {
95 return accessListIid.builder().child(Acl.class, aclKey).build();
98 private InstanceIdentifier<ServiceFunction> getSFPath(ServiceFunctionKey key) {
99 return sfIid.builder().child(ServiceFunction.class, key).build();
102 private InstanceIdentifier<ServiceFunctionForwarder> getSFFPath(ServiceFunctionForwarderKey key) {
103 return sffIid.builder().child(ServiceFunctionForwarder.class, key).build();
106 private InstanceIdentifier<ServiceFunctionChain> getSFCPath(ServiceFunctionChainKey key) {
107 return sfcIid.builder().child(ServiceFunctionChain.class, key).build();
110 private InstanceIdentifier<ServiceFunctionPath> getSFPPath(ServiceFunctionPathKey key) {
111 return sfpIid.builder().child(ServiceFunctionPath.class, key).build();
114 public ServiceFunctionForwarder getExistingSFF(String ipAddress) {
115 ServiceFunctionForwarders existingSffs = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, sffIid);
116 if (existingSffs != null
117 && existingSffs.getServiceFunctionForwarder() != null
118 && !existingSffs.getServiceFunctionForwarder().isEmpty()) {
120 List<ServiceFunctionForwarder> existingSffList = existingSffs.getServiceFunctionForwarder();
121 for (ServiceFunctionForwarder sff : existingSffList) {
122 if (sff.getIpMgmtAddress().getIpv4Address().equals(new Ipv4Address(ipAddress))) {
130 public void addServiceFunctionForwarder(ServiceFunctionForwarder sff) {
131 InstanceIdentifier<ServiceFunctionForwarder> sffIid = getSFFPath(sff.getKey());
132 LOG.info("Write Service Function Forwarder {} to config data store at {}",sff, sffIid);
133 mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, sffIid, sff);
136 public void addServiceFunctionChain(ServiceFunctionChain sfc) {
137 InstanceIdentifier<ServiceFunctionChain> sfcIid = getSFCPath(sfc.getKey());
138 LOG.info("Write Service Function Chain {} to config data store at {}",sfc, sfcIid);
139 mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, sfcIid, sfc);
142 public void removeServiceFunctionChain(ServiceFunctionChainKey sfcKey) {
143 InstanceIdentifier<ServiceFunctionChain> sfcIid = getSFCPath(sfcKey);
144 LOG.info("Remove Service Function Chain {} from config data store at {}",sfcKey, sfcIid);
145 mdsalUtils.delete(LogicalDatastoreType.CONFIGURATION, sfcIid);
148 public void addServiceFunctionPath(ServiceFunctionPath sfp) {
149 InstanceIdentifier<ServiceFunctionPath> sfpIid = getSFPPath(sfp.getKey());
150 LOG.info("Write Service Function Path {} to config data store at {}",sfp, sfpIid);
151 mdsalUtils.put(LogicalDatastoreType.CONFIGURATION, sfpIid, sfp);