2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.keystore.legacy.impl;
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;
28 final class DefaultAddTrustedCertificate extends AbstractRpc implements AddTrustedCertificate {
29 private static final Logger LOG = LoggerFactory.getLogger(DefaultAddTrustedCertificate.class);
31 DefaultAddTrustedCertificate(final DataBroker dataBroker) {
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();
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))
50 tx.put(LogicalDatastoreType.CONFIGURATION,
51 InstanceIdentifier.create(Keystore.class).child(TrustedCertificate.class, base64certificate.key()),
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());