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