29d345287a28e542799d48c8ddb1064a4d4441d1
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / util / NetconfSalKeystoreService.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.netconf.sal.connect.util;
10
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.List;
18 import java.util.stream.Collectors;
19 import javax.annotation.Nullable;
20 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddKeystoreEntryInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddKeystoreEntryOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddKeystoreEntryOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.KeystoreBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.NetconfKeystoreService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemovePrivateKeyInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemovePrivateKeyOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemovePrivateKeyOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveTrustedCertificateInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveTrustedCertificateOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveTrustedCertificateOutputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredential;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredentialBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredentialKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificate;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateKey;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class NetconfSalKeystoreService implements NetconfKeystoreService {
60
61     private static final Logger LOG = LoggerFactory.getLogger(NetconfSalKeystoreService.class);
62
63     private final DataBroker dataBroker;
64     private final AAAEncryptionService encryptionService;
65
66     private final InstanceIdentifier<Keystore> keystoreIid = InstanceIdentifier.create(Keystore.class);
67
68     public NetconfSalKeystoreService(final DataBroker dataBroker,
69                                      final AAAEncryptionService encryptionService) {
70         LOG.info("Starting NETCONF keystore service.");
71
72         this.dataBroker = dataBroker;
73         this.encryptionService = encryptionService;
74
75         initKeystore();
76     }
77
78     private void initKeystore() {
79         final Keystore keystore = new KeystoreBuilder().build();
80
81         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
82         writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, keystoreIid, keystore);
83
84         final CheckedFuture<Void, TransactionCommitFailedException> submit = writeTransaction.submit();
85
86         try {
87             submit.checkedGet();
88             LOG.debug("init keystore done");
89         } catch (TransactionCommitFailedException exception) {
90             LOG.error("Unable to initialize Netconf key-pair store.", exception);
91         }
92     }
93
94     @Override
95     public ListenableFuture<RpcResult<RemoveKeystoreEntryOutput>> removeKeystoreEntry(
96             final RemoveKeystoreEntryInput input) {
97         LOG.debug("Removing keypairs: {}", input);
98
99         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
100         final List<String> ids = input.getKeyId();
101
102         for (final String id : ids) {
103             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION,
104                     keystoreIid.child(KeyCredential.class, new KeyCredentialKey(id)));
105         }
106
107         final SettableFuture<RpcResult<RemoveKeystoreEntryOutput>> rpcResult = SettableFuture.create();
108
109         final ListenableFuture<Void> submit = writeTransaction.submit();
110         Futures.addCallback(submit, new FutureCallback<Void>() {
111             @Override
112             public void onSuccess(@Nullable final Void result) {
113                 LOG.debug("remove-key-pair success. Input: {}");
114                 rpcResult.set(RpcResultBuilder.success(new RemoveKeystoreEntryOutputBuilder().build()).build());
115             }
116
117             @Override
118             public void onFailure(final Throwable throwable) {
119                 LOG.warn("remove-key-pair failed. Input: {}", input, throwable);
120                 rpcResult.setException(throwable);
121             }
122         }, MoreExecutors.directExecutor());
123
124         return rpcResult;
125     }
126
127     @Override
128     public ListenableFuture<RpcResult<AddKeystoreEntryOutput>> addKeystoreEntry(final AddKeystoreEntryInput input) {
129         LOG.debug("Adding keypairs: {}", input);
130
131         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
132         final List<KeyCredential> keypairs = input.getKeyCredential().stream().map(keypair ->
133                 new KeyCredentialBuilder(keypair)
134                         .setPrivateKey(encryptionService.encrypt(keypair.getPrivateKey()))
135                         .setPassphrase(encryptionService.encrypt(keypair.getPassphrase()))
136                         .build()).collect(Collectors.toList());
137
138         for (KeyCredential keypair : keypairs) {
139             writeTransaction.merge(LogicalDatastoreType.CONFIGURATION,
140                     keystoreIid.child(KeyCredential.class, keypair.key()), keypair);
141         }
142
143         final SettableFuture<RpcResult<AddKeystoreEntryOutput>> rpcResult = SettableFuture.create();
144
145         Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {
146             @Override
147             public void onSuccess(@Nullable final Void result) {
148                 LOG.debug("add-key-pair success. Input: {}");
149                 rpcResult.set(RpcResultBuilder.success(new AddKeystoreEntryOutputBuilder().build()).build());
150             }
151
152             @Override
153             public void onFailure(final Throwable throwable) {
154                 LOG.warn("add-key-pair failed. Input: {}", input, throwable);
155                 rpcResult.setException(throwable);
156             }
157         }, MoreExecutors.directExecutor());
158
159         return rpcResult;
160     }
161
162     @Override
163     public ListenableFuture<RpcResult<AddTrustedCertificateOutput>> addTrustedCertificate(
164             final AddTrustedCertificateInput input) {
165         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
166
167         for (TrustedCertificate certificate : input.getTrustedCertificate()) {
168             writeTransaction.merge(LogicalDatastoreType.CONFIGURATION,
169                     keystoreIid.child(TrustedCertificate.class, certificate.key()), certificate);
170         }
171
172         final SettableFuture<RpcResult<AddTrustedCertificateOutput>> rpcResult = SettableFuture.create();
173
174         Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {
175             @Override
176             public void onSuccess(@Nullable final Void result) {
177                 LOG.debug("add-trusted-certificate success. Input: {}", input);
178                 rpcResult.set(RpcResultBuilder.success(new AddTrustedCertificateOutputBuilder().build()).build());
179             }
180
181             @Override
182             public void onFailure(final Throwable throwable) {
183                 LOG.warn("add-trusted-certificate failed. Input: {}", input, throwable);
184                 rpcResult.setException(throwable);
185             }
186         }, MoreExecutors.directExecutor());
187
188         return rpcResult;
189     }
190
191     @Override
192     public ListenableFuture<RpcResult<RemoveTrustedCertificateOutput>> removeTrustedCertificate(
193             final RemoveTrustedCertificateInput input) {
194         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
195         final List<String> names = input.getName();
196
197         for (final String name : names) {
198             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION,
199                     keystoreIid.child(TrustedCertificate.class, new TrustedCertificateKey(name)));
200         }
201
202         final SettableFuture<RpcResult<RemoveTrustedCertificateOutput>> rpcResult = SettableFuture.create();
203
204         Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {
205             @Override
206             public void onSuccess(@Nullable final Void result) {
207                 LOG.debug("remove-trusted-certificate success. Input: {}", input);
208                 rpcResult.set(RpcResultBuilder.success(new RemoveTrustedCertificateOutputBuilder().build()).build());
209             }
210
211             @Override
212             public void onFailure(final Throwable throwable) {
213                 LOG.warn("remove-trusted-certificate failed. Input: {}", input, throwable);
214                 rpcResult.setException(throwable);
215             }
216         }, MoreExecutors.directExecutor());
217
218         return rpcResult;
219     }
220
221     @Override
222     public ListenableFuture<RpcResult<AddPrivateKeyOutput>> addPrivateKey(final AddPrivateKeyInput input) {
223         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
224
225         for (PrivateKey key: input.getPrivateKey()) {
226             writeTransaction.merge(LogicalDatastoreType.CONFIGURATION,
227                     keystoreIid.child(PrivateKey.class, key.key()), key);
228         }
229
230         final SettableFuture<RpcResult<AddPrivateKeyOutput>> rpcResult = SettableFuture.create();
231
232         Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {
233             @Override
234             public void onSuccess(@Nullable final Void result) {
235                 LOG.debug("add-private-key success. Input: {}", input);
236                 rpcResult.set(RpcResultBuilder.success(new AddPrivateKeyOutputBuilder().build()).build());
237             }
238
239             @Override
240             public void onFailure(final Throwable throwable) {
241                 LOG.warn("add-private-key failed. Input: {}", input, throwable);
242                 rpcResult.setException(throwable);
243             }
244         }, MoreExecutors.directExecutor());
245
246         return rpcResult;
247     }
248
249     @Override
250     public ListenableFuture<RpcResult<RemovePrivateKeyOutput>> removePrivateKey(final RemovePrivateKeyInput input) {
251         final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
252         final List<String> names = input.getName();
253
254         for (final String name : names) {
255             writeTransaction.delete(LogicalDatastoreType.CONFIGURATION,
256                     keystoreIid.child(PrivateKey.class, new PrivateKeyKey(name)));
257         }
258
259         final SettableFuture<RpcResult<RemovePrivateKeyOutput>> rpcResult = SettableFuture.create();
260
261         Futures.addCallback(writeTransaction.submit(), new FutureCallback<Void>() {
262             @Override
263             public void onSuccess(@Nullable final Void result) {
264                 LOG.debug("remove-private-key success. Input: {}", input);
265                 rpcResult.set(RpcResultBuilder.success(new RemovePrivateKeyOutputBuilder().build()).build());
266             }
267
268             @Override
269             public void onFailure(final Throwable throwable) {
270                 LOG.warn("remove-private-key failed. Input: {}", input, throwable);
271                 rpcResult.setException(throwable);
272             }
273         }, MoreExecutors.directExecutor());
274
275         return rpcResult;
276     }
277 }