Merge "Add ovsdb/netvirt ui to vpnservice netvirt code."
[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.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.List;
34
35 /**
36  * Utility methods to read OpenDaylight SFC models.
37  */
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);
48
49     private final DataBroker dataBroker;
50     private final MdsalUtils mdsalUtils;
51
52     public SfcMdsalHelper(DataBroker dataBroker) {
53         this.dataBroker = dataBroker;
54         mdsalUtils = new MdsalUtils(this.dataBroker);
55     }
56
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);
62     }
63
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);
68     }
69
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);
74     }
75
76     //Service Function
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);
81     }
82
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);
87     }
88
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);
93     }
94     private InstanceIdentifier<Acl> getAclPath(AclKey aclKey) {
95         return accessListIid.builder().child(Acl.class, aclKey).build();
96     }
97
98     private InstanceIdentifier<ServiceFunction> getSFPath(ServiceFunctionKey key) {
99         return sfIid.builder().child(ServiceFunction.class, key).build();
100     }
101
102     private InstanceIdentifier<ServiceFunctionForwarder> getSFFPath(ServiceFunctionForwarderKey key) {
103         return sffIid.builder().child(ServiceFunctionForwarder.class, key).build();
104     }
105
106     private InstanceIdentifier<ServiceFunctionChain> getSFCPath(ServiceFunctionChainKey key) {
107         return sfcIid.builder().child(ServiceFunctionChain.class, key).build();
108     }
109
110     private InstanceIdentifier<ServiceFunctionPath> getSFPPath(ServiceFunctionPathKey key) {
111         return sfpIid.builder().child(ServiceFunctionPath.class, key).build();
112     }
113
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()) {
119
120             List<ServiceFunctionForwarder> existingSffList = existingSffs.getServiceFunctionForwarder();
121             for (ServiceFunctionForwarder sff : existingSffList) {
122                 if (sff.getIpMgmtAddress().getIpv4Address().equals(new Ipv4Address(ipAddress))) {
123                     return sff;
124                 }
125             }
126         }
127         return null;
128     }
129
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);
134     }
135
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);
140     }
141
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);
146     }
147
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);
152     }
153 }