Enforce base64 encoding for netconf-keystore
[netconf.git] / keystore / keystore-legacy / src / main / java / org / opendaylight / netconf / keystore / legacy / impl / RpcSingleton.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableClassToInstanceMap;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.RpcProviderService;
19 import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.api.ServiceGroupIdentifier;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddKeystoreEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddPrivateKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.AddTrustedCertificate;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemoveKeystoreEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemovePrivateKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev231109.RemoveTrustedCertificate;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.opendaylight.yangtools.yang.binding.Rpc;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class RpcSingleton implements ClusterSingletonService {
33     private static final Logger LOG = LoggerFactory.getLogger(RpcSingleton.class);
34     private static final @NonNull ServiceGroupIdentifier SGI = new ServiceGroupIdentifier("netconf-keystore-rpc");
35
36     private final AAAEncryptionService encryptionService;
37     private final RpcProviderService rpcProvider;
38     private final DataBroker dataBroker;
39
40     private Registration reg;
41
42     RpcSingleton(final DataBroker dataBroker, final RpcProviderService rpcProvider,
43             final AAAEncryptionService encryptionService) {
44         this.dataBroker = requireNonNull(dataBroker);
45         this.rpcProvider = requireNonNull(rpcProvider);
46         this.encryptionService = requireNonNull(encryptionService);
47     }
48
49     @Override
50     public ServiceGroupIdentifier getIdentifier() {
51         return SGI;
52     }
53
54     @Override
55     public void instantiateServiceInstance() {
56         if (reg != null) {
57             LOG.warn("Instiatiating while already running, very weird", new Throwable());
58             return;
59         }
60
61         LOG.debug("Registering RPC implementations");
62         reg = rpcProvider.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
63             .put(AddKeystoreEntry.class, new DefaultAddKeystoreEntry(dataBroker, encryptionService))
64             .put(RemoveKeystoreEntry.class, new DefaultRemoveKeystoreEntry(dataBroker))
65             .put(AddPrivateKey.class, new DefaultAddPrivateKey(dataBroker))
66             .put(RemovePrivateKey.class, new DefaultRemovePrivateKey(dataBroker))
67             .put(AddTrustedCertificate.class, new DefaultAddTrustedCertificate(dataBroker))
68             .put(RemoveTrustedCertificate.class, new DefaultRemoveTrustedCertificate(dataBroker))
69             .build());
70         LOG.info("This node is now owning NETCONF keystore configuration");
71     }
72
73     @Override
74     public ListenableFuture<?> closeServiceInstance() {
75         if (reg != null) {
76             LOG.debug("Unregistering RPC implementations");
77             reg.close();
78             reg = null;
79             LOG.info("This node is no longer owning NETCONF keystore configuration");
80         }
81         return Futures.immediateVoidFuture();
82     }
83 }