Enforce base64 encoding for netconf-keystore
[netconf.git] / keystore / keystore-legacy / src / main / java / org / opendaylight / netconf / keystore / legacy / impl / DefaultAddTrustedCertificate.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.nio.charset.StandardCharsets;
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.AddTrustedCertificate;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddTrustedCertificateInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddTrustedCertificateOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddTrustedCertificateOutputBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.Keystore;
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.TrustedCertificateBuilder;
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 DefaultAddTrustedCertificate extends AbstractRpc implements AddTrustedCertificate {
29     private static final Logger LOG = LoggerFactory.getLogger(DefaultAddTrustedCertificate.class);
30
31     DefaultAddTrustedCertificate(final DataBroker dataBroker) {
32         super(dataBroker);
33     }
34
35     @Override
36     public ListenableFuture<RpcResult<AddTrustedCertificateOutput>> invoke(final AddTrustedCertificateInput input) {
37         final var certs = input.getTrustedCertificate();
38         if (certs == null || certs.isEmpty()) {
39             return RpcResultBuilder.success(new AddTrustedCertificateOutputBuilder().build()).buildFuture();
40         }
41
42         LOG.debug("Updating trusted certificates: {}", certs);
43         final var tx = newTransaction();
44         for (var certificate : certs.values()) {
45             final var base64certificate = new TrustedCertificateBuilder()
46                 .setName(certificate.getName())
47                 .setCertificate(certificate.getCertificate().getBytes(StandardCharsets.UTF_8))
48                 .build();
49
50             tx.put(LogicalDatastoreType.CONFIGURATION,
51                 InstanceIdentifier.create(Keystore.class).child(TrustedCertificate.class, base64certificate.key()),
52                     base64certificate);
53         }
54
55         return tx.commit().transform(commitInfo -> {
56             LOG.debug("Updated trusted certificates: {}", certs.keySet());
57             return RpcResultBuilder.success(new AddTrustedCertificateOutputBuilder().build()).build();
58         }, MoreExecutors.directExecutor());
59     }
60 }