Refactor NetconfSalKeystoreRpcs
[netconf.git] / keystore / keystore-legacy / src / main / java / org / opendaylight / netconf / keystore / legacy / DefaultRemoveKeystoreEntry.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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 package org.opendaylight.netconf.keystore.legacy;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.stream.Collectors;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.RemoveKeystoreEntryOutputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredential;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.keystore.entry.KeyCredentialKey;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class DefaultRemoveKeystoreEntry extends AbstractRpc implements RemoveKeystoreEntry {
29     private static final Logger LOG = LoggerFactory.getLogger(DefaultRemoveKeystoreEntry.class);
30
31     DefaultRemoveKeystoreEntry(final DataBroker dataBroker) {
32         super(dataBroker);
33     }
34
35     @Override
36     public ListenableFuture<RpcResult<RemoveKeystoreEntryOutput>> invoke(final RemoveKeystoreEntryInput input) {
37         final var keyIds = input.getKeyId();
38         if (keyIds == null || keyIds.isEmpty()) {
39             return RpcResultBuilder.success(new RemoveKeystoreEntryOutputBuilder().build()).buildFuture();
40         }
41
42         final var keys = keyIds.stream().map(KeyCredentialKey::new).collect(Collectors.toSet());
43         LOG.debug("Removing keypairs: {}", keys);
44         final var tx = newTransaction();
45         for (var key : keys) {
46             tx.delete(LogicalDatastoreType.CONFIGURATION,
47                 InstanceIdentifier.create(Keystore.class).child(KeyCredential.class, key));
48         }
49
50         return tx.commit().transform(commitInfo -> {
51             LOG.debug("Removed keypairs: {}", keys);
52             return RpcResultBuilder.success(new RemoveKeystoreEntryOutputBuilder().build()).build();
53         }, MoreExecutors.directExecutor());
54     }
55 }