Enforce base64 encoding for netconf-keystore
[netconf.git] / keystore / keystore-legacy / src / main / java / org / opendaylight / netconf / keystore / legacy / impl / DefaultRemovePrivateKey.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.impl;
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.rev231109.Keystore;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemovePrivateKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemovePrivateKeyInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemovePrivateKeyOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemovePrivateKeyOutputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109._private.keys.PrivateKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109._private.keys.PrivateKeyKey;
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 DefaultRemovePrivateKey extends AbstractRpc implements RemovePrivateKey {
29     private static final Logger LOG = LoggerFactory.getLogger(DefaultRemovePrivateKey.class);
30
31     DefaultRemovePrivateKey(final DataBroker dataBroker) {
32         super(dataBroker);
33     }
34
35     @Override
36     public ListenableFuture<RpcResult<RemovePrivateKeyOutput>> invoke(final RemovePrivateKeyInput input) {
37         final var names = input.getName();
38         if (names == null || names.isEmpty()) {
39             return RpcResultBuilder.success(new RemovePrivateKeyOutputBuilder().build()).buildFuture();
40         }
41
42         final var keys = names.stream().map(PrivateKeyKey::new).collect(Collectors.toSet());
43         LOG.debug("Removing private keys: {}", keys);
44
45
46         final var tx = newTransaction();
47         for (var key : keys) {
48             tx.delete(LogicalDatastoreType.CONFIGURATION,
49                 InstanceIdentifier.create(Keystore.class).child(PrivateKey.class, key));
50         }
51
52         return tx.commit().transform(commitInfo -> {
53             LOG.debug("Removed trusted keys: {}", keys);
54             return RpcResultBuilder.success(new RemovePrivateKeyOutputBuilder().build()).build();
55         }, MoreExecutors.directExecutor());
56     }
57 }