Expose NetconfKeystoreService
[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 org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificate;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateOutput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateOutputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificate;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 final class DefaultAddTrustedCertificate extends AbstractRpc implements AddTrustedCertificate {
27     private static final Logger LOG = LoggerFactory.getLogger(DefaultAddTrustedCertificate.class);
28
29     DefaultAddTrustedCertificate(final DataBroker dataBroker) {
30         super(dataBroker);
31     }
32
33     @Override
34     public ListenableFuture<RpcResult<AddTrustedCertificateOutput>> invoke(final AddTrustedCertificateInput input) {
35         final var certs = input.getTrustedCertificate();
36         if (certs == null || certs.isEmpty()) {
37             return RpcResultBuilder.success(new AddTrustedCertificateOutputBuilder().build()).buildFuture();
38         }
39
40         LOG.debug("Updating trusted certificates: {}", certs);
41         final var tx = newTransaction();
42         for (var certificate : certs.values()) {
43             tx.put(LogicalDatastoreType.CONFIGURATION,
44                 InstanceIdentifier.create(Keystore.class).child(TrustedCertificate.class, certificate.key()),
45                 certificate);
46         }
47
48         return tx.commit().transform(commitInfo -> {
49             LOG.debug("Updated trusted certificates: {}", certs.keySet());
50             return RpcResultBuilder.success(new AddTrustedCertificateOutputBuilder().build()).build();
51         }, MoreExecutors.directExecutor());
52     }
53 }