Bug 3223: Fix adding DistinguishedName from RPC
[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.concurrent.ExecutionException;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.lispflowmapping.implementation.util.InstanceIdentifierUtil;
17 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.db.instance.AuthenticationKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.db.instance.Mapping;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.common.util.concurrent.CheckedFuture;
25
26 /**
27  * Stores data coming from the mapping database RPCs into the config datastore.
28  *
29  * @author Lorand Jakab
30  *
31  */
32 public class DataStoreBackEnd {
33     protected static final Logger LOG = LoggerFactory.getLogger(DataStoreBackEnd.class);
34
35     private DataBroker broker;
36
37     public DataStoreBackEnd(DataBroker broker) {
38         this.broker = broker;
39     }
40
41     public void addAuthenticationKey(AuthenticationKey authenticationKey) {
42         LOG.debug("MD-SAL: Adding authentication key '{}' for {}/{}", authenticationKey.getAuthkey(),
43                 LispAFIConvertor.toString(authenticationKey.getLispAddressContainer()),
44                 authenticationKey.getMaskLength());
45
46         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
47                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
48         WriteTransaction transaction = broker.newWriteOnlyTransaction();
49         transaction.put(LogicalDatastoreType.CONFIGURATION, path, authenticationKey, true);
50         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
51         checkTransaction(future, "Adding authentication key to config datastrore failed");
52     }
53
54     public void addMapping(Mapping mapping) {
55         LOG.debug("MD-SAL: Adding mapping for {}/{}",
56                 LispAFIConvertor.toString(mapping.getLispAddressContainer()), mapping.getMaskLength());
57
58         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
59                 .createMappingIid(mapping.getLispAddressContainer());
60         WriteTransaction transaction = broker.newWriteOnlyTransaction();
61         transaction.put(LogicalDatastoreType.CONFIGURATION, path, mapping, true);
62         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
63         checkTransaction(future, "Adding mapping to config datastrore failed");
64     }
65
66     public void removeAuthenticationKey(AuthenticationKey authenticationKey) {
67         LOG.debug("MD-SAL: Removing authentication key for {}/{}",
68                 LispAFIConvertor.toString(authenticationKey.getLispAddressContainer()),
69                 authenticationKey.getMaskLength());
70
71         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
72                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
73         WriteTransaction transaction = broker.newWriteOnlyTransaction();
74         transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
75         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
76         checkTransaction(future, "Deleting authentication key from config datastrore failed");
77     }
78
79     public void removeMapping(Mapping mapping) {
80         LOG.debug("MD-SAL: Removing mapping for {}/{}",
81                 LispAFIConvertor.toString(mapping.getLispAddressContainer()), mapping.getMaskLength());
82
83         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
84                 .createMappingIid(mapping.getLispAddressContainer());
85         WriteTransaction transaction = broker.newWriteOnlyTransaction();
86         transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
87         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
88         checkTransaction(future, "Deleting mapping from config datastrore failed");
89     }
90
91     public void updateAuthenticationKey(AuthenticationKey authenticationKey) {
92         LOG.debug("MD-SAL: Updating authentication key for {}/{} with '{}'",
93                 LispAFIConvertor.toString(authenticationKey.getLispAddressContainer()),
94                 authenticationKey.getMaskLength(), authenticationKey.getAuthkey());
95
96         InstanceIdentifier<AuthenticationKey> path = InstanceIdentifierUtil
97                 .createAuthenticationKeyIid(authenticationKey.getLispAddressContainer());
98         WriteTransaction transaction = broker.newWriteOnlyTransaction();
99         transaction.put(LogicalDatastoreType.CONFIGURATION, path, authenticationKey, true);
100         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
101         checkTransaction(future, "Updating authentication key in config datastrore failed");
102     }
103
104     public void updateMapping(Mapping mapping) {
105         LOG.debug("MD-SAL: Updating mapping for {}/{}",
106                 LispAFIConvertor.toString(mapping.getLispAddressContainer()), mapping.getMaskLength());
107
108         InstanceIdentifier<Mapping> path = InstanceIdentifierUtil
109                 .createMappingIid(mapping.getLispAddressContainer());
110         WriteTransaction transaction = broker.newWriteOnlyTransaction();
111         transaction.put(LogicalDatastoreType.CONFIGURATION, path, mapping, true);
112         CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
113         checkTransaction(future, "Updating mapping in config datastrore failed");
114     }
115
116     void checkTransaction(CheckedFuture<Void, TransactionCommitFailedException> future, String errMsg) {
117         try {
118             future.get();
119         } catch (InterruptedException | ExecutionException e) {
120             LOG.warn(errMsg + e);
121         }
122     }
123 }