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.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;
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 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");
93 public void updateAuthenticationKey(AuthenticationKey authenticationKey) {
94 LOG.debug("MD-SAL: Updating authentication key for {} with '{}'",
95 LispAddressStringifier.getString(authenticationKey.getLispAddressContainer()),
96 authenticationKey.getAuthkey());
98 InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
99 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
100 writePutTransaction(path, authenticationKey, LogicalDatastoreType.CONFIGURATION,
101 "Updating authentication key in config datastrore failed");
104 public void updateMapping(Mapping mapping) {
105 LOG.debug("MD-SAL: Updating mapping for {}",
106 LispAddressStringifier.getString(mapping.getLispAddressContainer()));
108 InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
109 .createMappingIid(mapping.getLispAddressContainer(), mapping.getOrigin());
110 writePutTransaction(path, mapping, LogicalDatastoreType.CONFIGURATION,
111 "Updating mapping in config datastrore failed");
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);
121 for (InstanceId id : mdb.getInstanceId()) {
122 List<Mapping> ms = id.getMapping();
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);
139 for (InstanceId id : mdb.getInstanceId()) {
140 List<AuthenticationKey> keys = id.getAuthenticationKey();
142 authKeys.addAll(keys);
150 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean writePutTransaction(
151 InstanceIdentifier<U> addIID, U data, LogicalDatastoreType logicalDatastoreType, String errMsg) {
153 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
154 writeTx.put(logicalDatastoreType, addIID, data, true);
155 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
157 submitFuture.checkedGet();
159 } catch (TransactionCommitFailedException e) {
160 LOG.error("{} : {}", errMsg, e.getMessage());
166 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> U readTransaction(
167 InstanceIdentifier<U> readIID, LogicalDatastoreType logicalDatastoreType) {
169 ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
170 Optional<U> optionalDataObject;
171 CheckedFuture<Optional<U>, ReadFailedException> submitFuture = readTx.read(logicalDatastoreType, readIID);
173 optionalDataObject = submitFuture.checkedGet();
174 if (optionalDataObject != null && optionalDataObject.isPresent()) {
175 ret = optionalDataObject.get();
177 LOG.debug("{}: Failed to read", Thread.currentThread().getStackTrace()[1]);
179 } catch (ReadFailedException e) {
180 LOG.warn("Failed to ....", e);
185 private <U extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteTransaction(
186 InstanceIdentifier<U> deleteIID, LogicalDatastoreType logicalDatastoreType, String errMsg) {
189 WriteTransaction writeTx = broker.newWriteOnlyTransaction();
190 writeTx.delete(logicalDatastoreType, deleteIID);
191 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = writeTx.submit();
193 submitFuture.checkedGet();
195 } catch (TransactionCommitFailedException e) {
196 LOG.error("{} : {}", errMsg, e.getMessage());