8cb741bb1a68e422096485cb38a885eb16b88cd3
[aaa.git] / aaa-password-service / impl / src / main / java / org / opendaylight / aaa / impl / password / service / OSGiPasswordServiceConfigBootstrap.java
1 /*
2  * Copyright (c) 2020 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.aaa.impl.password.service;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import org.checkerframework.checker.lock.qual.Holding;
14 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.password.service.config.rev170619.PasswordServiceConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.password.service.config.rev170619.PasswordServiceConfigBuilder;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.osgi.service.component.ComponentFactory;
24 import org.osgi.service.component.ComponentInstance;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Deactivate;
28 import org.osgi.service.component.annotations.Reference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Beta
33 @Component(immediate = true)
34 public final class OSGiPasswordServiceConfigBootstrap
35         implements ClusteredDataTreeChangeListener<PasswordServiceConfig> {
36     private static final Logger LOG = LoggerFactory.getLogger(OSGiPasswordServiceConfigBootstrap.class);
37
38     @Reference
39     DataBroker dataBroker = null;
40
41     @Reference(target = "(component.factory=" + OSGiPasswordServiceConfig.FACTORY_NAME + ")")
42     ComponentFactory configFactory = null;
43
44     private ListenerRegistration<?> registration;
45     private ComponentInstance instance;
46
47     @Activate
48     synchronized void activate() {
49         registration = dataBroker.registerDataTreeChangeListener(
50             DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
51                 InstanceIdentifier.create(PasswordServiceConfig.class)), this);
52         LOG.info("Listening for password service configuration");
53     }
54
55     @Deactivate
56     synchronized void deactivate() {
57         registration.close();
58         registration = null;
59         if (instance != null) {
60             instance.dispose();
61             instance = null;
62         }
63         LOG.info("No longer listening for password service configuration");
64     }
65
66     @Override
67     public synchronized void onInitialData() {
68         updateInstance(null);
69     }
70
71     @Override
72     public synchronized void onDataTreeChanged(final Collection<DataTreeModification<PasswordServiceConfig>> changes) {
73         // FIXME: at this point we need to populate default values -- from the XML file
74         updateInstance(Iterables.getLast(changes).getRootNode().getDataAfter());
75     }
76
77     @Holding("this")
78     private void updateInstance(final PasswordServiceConfig config) {
79         if (registration != null) {
80             final ComponentInstance newInstance = configFactory.newInstance(
81                 OSGiPasswordServiceConfig.props(config != null ? config : new PasswordServiceConfigBuilder().build()));
82             if (instance != null) {
83                 instance.dispose();
84             }
85             instance = newInstance;
86         }
87     }
88 }