51ba251a2f6fa2b97d0602d25f9edfc2135d9ef9
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / mdsal / DataStoreBackEnd.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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.lispflowmapping.implementation.mdsal;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.lispflowmapping.implementation.util.InstanceIdentifierUtil;
20 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.InstanceId;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.common.base.Optional;
30 import com.google.common.util.concurrent.CheckedFuture;
31
32 /**
33  * Stores data coming from the mapping database RPCs into the config datastore.
34  *
35  * @author Lorand Jakab
36  *
37  */
38 public class DataStoreBackEnd {
39     protected static final Logger LOG = LoggerFactory.getLogger(DataStoreBackEnd.class);
40
41     private DataBroker broker;
42
43     public DataStoreBackEnd(DataBroker broker) {
44         this.broker = broker;
45     }
46
47     public void addAuthenticationKey(AuthenticationKey authenticationKey) {
48         LOG.debug("MD-SAL: Adding authentication key '{}' for {}", authenticationKey.getAuthkey(),
49                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()));
50
51         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
52                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
53         writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
54                 "Adding authentication key to config datastrore failed");
55     }
56
57     public void addMapping(Mapping mapping) {
58         LOG.debug("MD-SAL: Adding mapping for {}",
59                 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
60
61         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
62                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
63         writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
64                 "Adding mapping to config datastrore failed");
65     }
66
67     public void removeAuthenticationKey(AuthenticationKey authenticationKey) {
68         LOG.debug("MD-SAL: Removing authentication key for {}",
69                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()));
70
71         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
72                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
73         deleteTransaction(path, LogicalDatastoreType.CONFIGURATION,
74                 "Deleting authentication key from config datastrore failed");
75     }
76
77     public void removeMapping(Mapping mapping) {
78         LOG.debug("MD-SAL: Removing mapping for {}",
79                 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
80
81         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
82                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
83         deleteTransaction(path, LogicalDatastoreType.CONFIGURATION, "Deleting mapping from config datastrore failed");
84     }
85
86     public void removeAllMappings() {
87         LOG.debug("MD-SAL: Removing all mappings");
88         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
89         deleteTransaction(path, LogicalDatastoreType.CONFIGURATION,
90                 "Removing of all mappings in config datastore failed");
91     }
92
93     public void updateAuthenticationKey(AuthenticationKey authenticationKey) {
94         LOG.debug("MD-SAL: Updating authentication key for {} with '{}'",
95                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()),
96                 authenticationKey.getAuthkey());
97
98         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
99                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
100         writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
101                 "Updating authentication key in config datastrore failed");
102     }
103
104     public void updateMapping(Mapping mapping) {
105         LOG.debug("MD-SAL: Updating mapping for {}",
106                 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
107
108         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
109                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
110         writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
111                 "Updating mapping in config datastrore failed");
112     }
113
114     public List<Mapping> getAllMappings() {
115         LOG.debug("MD-SAL: Get all mappings from datastore");
116         List<Mapping> mappings = new ArrayList<Mapping>();
117         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
118         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
119
120         if (mdb != null) {
121             for (InstanceId id : mdb.getInstanceId()) {
122                 List<Mapping> ms = id.getMapping();
123                 if (ms != null) {
124                     mappings.addAll(ms);
125                 }
126             }
127         }
128
129         return mappings;
130     }
131
132     public List<AuthenticationKey> getAllAuthenticationKeys() {
133         LOG.debug("MD-SAL: Get all authentication keys from datastore");
134         List<AuthenticationKey> authKeys = new ArrayList<AuthenticationKey>();
135         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
136         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
137
138         if (mdb != null) {
139             for (InstanceId id : mdb.getInstanceId()) {
140                 List<AuthenticationKey> keys = id.getAuthenticationKey();
141                 if (keys != null) {
142                     authKeys.addAll(keys);
143                 }
144             }
145         }
146
147         return authKeys;
148     }
149
150     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean writePutTransaction(
151             InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
152         boolean ret;
153         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
154         writeTx.put(logicalDatastoreType, addIID, data, true);
155         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
156         try {
157             submitFuture.checkedGet();
158             ret = true;
159         } catch (TransactionCommitFailedException e) {
160             LOG.error("{} : {}", errMsg, e.getMessage());
161             ret = false;
162         }
163         return ret;
164     }
165
166     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
167             InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
168         U ret = null;
169         ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
170         Optional<U> optionalDataObject;
171         CheckedFuture<Optional<U>, ReadFailedException> submitFuture = readTx.read(logicalDatastoreType, readIID);
172         try {
173             optionalDataObject = submitFuture.checkedGet();
174             if (optionalDataObject != null && optionalDataObject.isPresent()) {
175                 ret = optionalDataObject.get();
176             } else {
177                 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
178             }
179         } catch (ReadFailedException e) {
180             LOG.warn("Failed to ....", e);
181         }
182         return ret;
183     }
184
185     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteTransaction(
186             InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
187         boolean ret = false;
188
189         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
190         writeTx.delete(logicalDatastoreType, deleteIID);
191         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
192         try {
193             submitFuture.checkedGet();
194             ret = true;
195         } catch (TransactionCommitFailedException e) {
196             LOG.error("{} : {}", errMsg, e.getMessage());
197             ret = false;
198         }
199         return ret;
200     }
201 }