Update DAO API
[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
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 updateAuthenticationKey(AuthenticationKey authenticationKey) {
87         LOG.debug("MD-SAL: Updating authentication key for {} with '{}'",
88                 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()),
89                 authenticationKey.getAuthkey());
90
91         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
92                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
93         writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
94                 "Updating authentication key in config datastrore failed");
95     }
96
97     public void updateMapping(Mapping mapping) {
98         LOG.debug("MD-SAL: Updating mapping for {}",
99                 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
100
101         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
102                 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
103         writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
104                 "Updating mapping in config datastrore failed");
105     }
106
107     public List<Mapping> getAllMappings() {
108         LOG.debug("MD-SAL: Get all mappings from datastore");
109         List<Mapping> mappings = new ArrayList<Mapping>();
110         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
111         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
112
113         if (mdb != null) {
114             for (InstanceId id : mdb.getInstanceId()) {
115                 List<Mapping> ms = id.getMapping();
116                 if (ms != null) {
117                     mappings.addAll(ms);
118                 }
119             }
120         }
121
122         return mappings;
123     }
124
125     public List<AuthenticationKey> getAllAuthenticationKeys() {
126         LOG.debug("MD-SAL: Get all authentication keys from datastore");
127         List<AuthenticationKey> authKeys = new ArrayList<AuthenticationKey>();
128         InstanceIdentifier<MappingDatabase> path = InstanceIdentifier.create(MappingDatabase.class);
129         MappingDatabase mdb = readTransaction(path, LogicalDatastoreType.CONFIGURATION);
130
131         if (mdb != null) {
132             for (InstanceId id : mdb.getInstanceId()) {
133                 List<AuthenticationKey> keys = id.getAuthenticationKey();
134                 if (keys != null) {
135                     authKeys.addAll(keys);
136                 }
137             }
138         }
139
140         return authKeys;
141     }
142
143     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean writePutTransaction(
144             InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
145         boolean ret;
146         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
147         writeTx.put(logicalDatastoreType, addIID, data, true);
148         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
149         try {
150             submitFuture.checkedGet();
151             ret = true;
152         } catch (TransactionCommitFailedException e) {
153             LOG.error("{} : {}", errMsg, e.getMessage());
154             ret = false;
155         }
156         return ret;
157     }
158
159     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
160             InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
161         U ret = null;
162         ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
163         Optional<U> optionalDataObject;
164         CheckedFuture<Optional<U>, ReadFailedException> submitFuture = readTx.read(logicalDatastoreType, readIID);
165         try {
166             optionalDataObject = submitFuture.checkedGet();
167             if (optionalDataObject != null && optionalDataObject.isPresent()) {
168                 ret = optionalDataObject.get();
169             } else {
170                 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
171             }
172         } catch (ReadFailedException e) {
173             LOG.warn("Failed to ....", e);
174         }
175         return ret;
176     }
177
178     private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteTransaction(
179             InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
180         boolean ret = false;
181
182         WriteTransaction writeTx = broker.newWriteOnlyTransaction();
183         writeTx.delete(logicalDatastoreType, deleteIID);
184         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
185         try {
186             submitFuture.checkedGet();
187             ret = true;
188         } catch (TransactionCommitFailedException e) {
189             LOG.error("{} : {}", errMsg, e.getMessage());
190             ret = false;
191         }
192         return ret;
193     }
194 }