7ade059178390b1cf19836bc5c6bcf48bc044ab5
[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.rev150820.MappingDatabase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.db.instance.AuthenticationKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.db.instance.Mapping;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.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                 authenticationKey.getMaskLength()));
51
52         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
53                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer(),
54                         authenticationKey.getMaskLength());
55         writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
56                 "Adding authentication key to config datastrore failed");
57     }
58
59     public void addMapping(Mapping mapping) {
60         LOG.debug("MD-SAL: Adding mapping for {}",
61                 LispAddressStringifier.getString(mapping.getLispAddressContainer(), mapping.getMaskLength()));
62
63         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
64                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getMaskLength(), mapping.getOrigin());
65         writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
66                 "Adding mapping to config datastrore failed");
67     }
68
69     public void removeAuthenticationKey(AuthenticationKey authenticationKey) {
70         LOG.debug("MD-SAL: Removing authentication key for {}",
71                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer(),
72                 authenticationKey.getMaskLength()));
73
74         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
75                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer(),
76                         authenticationKey.getMaskLength());
77         deleteTransaction(path, LogicalDatastoreType.CONFIGURATION,
78                 "Deleting authentication key from config datastrore failed");
79     }
80
81     public void removeMapping(Mapping mapping) {
82         LOG.debug("MD-SAL: Removing mapping for {}",
83                 LispAddressStringifier.getString(mapping.getLispAddressContainer(), mapping.getMaskLength()));
84
85         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
86                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getMaskLength(), mapping.getOrigin());
87         deleteTransaction(path, LogicalDatastoreType.CONFIGURATION, "Deleting mapping from config datastrore failed");
88     }
89
90     public void updateAuthenticationKey(AuthenticationKey authenticationKey) {
91         LOG.debug("MD-SAL: Updating authentication key for {} with '{}'",
92                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer(),
93                 authenticationKey.getMaskLength()), authenticationKey.getAuthkey());
94
95         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
96                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer(),
97                         authenticationKey.getMaskLength());
98         writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
99                 "Updating authentication key in config datastrore failed");
100     }
101
102     public void updateMapping(Mapping mapping) {
103         LOG.debug("MD-SAL: Updating mapping for {}",
104                 LispAddressStringifier.getString(mapping.getLispAddressContainer(), mapping.getMaskLength()));
105
106         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
107                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getMaskLength(), mapping.getOrigin());
108         writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
109                 "Updating mapping in config datastrore failed");
110     }
111
112     public List<Mapping> getAllMappings() {
113         LOG.debug("MD-SAL: Get all mappings from datastore");
114         List<Mapping> mappings = new ArrayList<Mapping>();
115         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
116         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
117
118         if (mdb != null) {
119             for (InstanceId id : mdb.getInstanceId()) {
120                 List<Mapping> ms = id.getMapping();
121                 if (ms != null) {
122                     mappings.addAll(ms);
123                 }
124             }
125         }
126
127         return mappings;
128     }
129
130     public List<AuthenticationKey> getAllAuthenticationKeys() {
131         LOG.debug("MD-SAL: Get all authentication keys from datastore");
132         List<AuthenticationKey> authKeys = new ArrayList<AuthenticationKey>();
133         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
134         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
135
136         if (mdb != null) {
137             for (InstanceId id : mdb.getInstanceId()) {
138                 List<AuthenticationKey> keys = id.getAuthenticationKey();
139                 if (keys != null) {
140                     authKeys.addAll(keys);
141                 }
142             }
143         }
144
145         return authKeys;
146     }
147
148     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean writePutTransaction(
149             InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
150         boolean ret;
151         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
152         writeTx.put(logicalDatastoreType, addIID, data, true);
153         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
154         try {
155             submitFuture.checkedGet();
156             ret = true;
157         } catch (TransactionCommitFailedException e) {
158             LOG.error("{} : {}", errMsg, e.getMessage());
159             ret = false;
160         }
161         return ret;
162     }
163
164     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
165             InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
166         U ret = null;
167         ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
168         Optional<U> optionalDataObject;
169         CheckedFuture<Optional<U>, ReadFailedException> submitFuture = readTx.read(logicalDatastoreType, readIID);
170         try {
171             optionalDataObject = submitFuture.checkedGet();
172             if (optionalDataObject != null && optionalDataObject.isPresent()) {
173                 ret = optionalDataObject.get();
174             } else {
175                 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
176             }
177         } catch (ReadFailedException e) {
178             LOG.warn("Failed to ....", e);
179         }
180         return ret;
181     }
182
183     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteTransaction(
184             InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
185         boolean ret = false;
186
187         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
188         writeTx.delete(logicalDatastoreType, deleteIID);
189         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
190         try {
191             submitFuture.checkedGet();
192             ret = true;
193         } catch (TransactionCommitFailedException e) {
194             LOG.error("{} : {}", errMsg, e.getMessage());
195             ret = false;
196         }
197         return ret;
198     }
199 }