2 * Copyright (c) 2015 Cisco Systems, Inc. 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.lispflowmapping.implementation.mdsal;
10 import java.util.ArrayList;
11 import java.util.List;
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;
29 import com.google.common.base.Optional;
30 import com.google.common.util.concurrent.CheckedFuture;
33 * Stores data coming from the mapping database RPCs into the config datastore.
35 * @author Lorand Jakab
38 public class DataStoreBackEnd {
39 protected static final Logger LOG = LoggerFactory.getLogger(DataStoreBackEnd.class);
41 private DataBroker broker;
43 public DataStoreBackEnd(DataBroker broker) {
47 public void addAuthenticationKey(AuthenticationKey authenticationKey) {
48 LOG.debug("MD-SAL: Adding authentication key '{}' for {}", authenticationKey.getAuthkey(),
49 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()));
51 InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
52 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
53 writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
54 "Adding authentication key to config datastrore failed");
57 public void addMapping(Mapping mapping) {
58 LOG.debug("MD-SAL: Adding mapping for {}",
59 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
61 InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
62 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
63 writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
64 "Adding mapping to config datastrore failed");
67 public void removeAuthenticationKey(AuthenticationKey authenticationKey) {
68 LOG.debug("MD-SAL: Removing authentication key for {}",
69 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()));
71 InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
72 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
73 deleteTransaction(path, LogicalDatastoreType.CONFIGURATION,
74 "Deleting authentication key from config datastrore failed");
77 public void removeMapping(Mapping mapping) {
78 LOG.debug("MD-SAL: Removing mapping for {}",
79 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
81 InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
82 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
83 deleteTransaction(path, LogicalDatastoreType.CONFIGURATION, "Deleting mapping from config datastrore failed");
86 public void updateAuthenticationKey(AuthenticationKey authenticationKey) {
87 LOG.debug("MD-SAL: Updating authentication key for {} with '{}'",
88 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()),
89 authenticationKey.getAuthkey());
91 InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
92 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
93 writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
94 "Updating authentication key in config datastrore failed");
97 public void updateMapping(Mapping mapping) {
98 LOG.debug("MD-SAL: Updating mapping for {}",
99 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
101 InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
102 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
103 writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
104 "Updating mapping in config datastrore failed");
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);
114 for (InstanceId id : mdb.getInstanceId()) {
115 List<Mapping> ms = id.getMapping();
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);
132 for (InstanceId id : mdb.getInstanceId()) {
133 List<AuthenticationKey> keys = id.getAuthenticationKey();
135 authKeys.addAll(keys);
143 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean writePutTransaction(
144 InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
146 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
147 writeTx.put(logicalDatastoreType, addIID, data, true);
148 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
150 submitFuture.checkedGet();
152 } catch (TransactionCommitFailedException e) {
153 LOG.error("{} : {}", errMsg, e.getMessage());
159 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
160 InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
162 ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
163 Optional<U> optionalDataObject;
164 CheckedFuture<Optional<U>, ReadFailedException> submitFuture = readTx.read(logicalDatastoreType, readIID);
166 optionalDataObject = submitFuture.checkedGet();
167 if (optionalDataObject != null && optionalDataObject.isPresent()) {
168 ret = optionalDataObject.get();
170 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
172 } catch (ReadFailedException e) {
173 LOG.warn("Failed to ....", e);
178 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteTransaction(
179 InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
182 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
183 writeTx.delete(logicalDatastoreType, deleteIID);
184 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
186 submitFuture.checkedGet();
188 } catch (TransactionCommitFailedException e) {
189 LOG.error("{} : {}", errMsg, e.getMessage());