Convert sal-distributed-eos to OSGi DS
[controller.git] / opendaylight / md-sal / sal-distributed-eos / src / main / java / org / opendaylight / controller / cluster / entityownership / OSGiDistributedEntityOwnershipService.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.controller.cluster.entityownership;
9
10 import com.google.common.annotations.Beta;
11 import java.util.Map;
12 import java.util.Optional;
13 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
14 import org.opendaylight.controller.cluster.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfigReader;
15 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
16 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
17 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
18 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
19 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
20 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
21 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
22 import org.osgi.service.component.annotations.Activate;
23 import org.osgi.service.component.annotations.Component;
24 import org.osgi.service.component.annotations.Deactivate;
25 import org.osgi.service.component.annotations.Reference;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 @Beta
30 @Component(immediate = true, configurationPid = "org.opendaylight.controller.cluster.entity.owner.selection.strategies",
31     property = "type=default")
32 public final class OSGiDistributedEntityOwnershipService implements DOMEntityOwnershipService {
33     private static final Logger LOG = LoggerFactory.getLogger(OSGiDistributedEntityOwnershipService.class);
34
35     @Reference(target = "(type=distributed-operational)")
36     DistributedDataStoreInterface operDatastore = null;
37
38     private DistributedEntityOwnershipService delegate;
39
40     @Override
41     public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
42             throws CandidateAlreadyRegisteredException {
43         return delegate.registerCandidate(entity);
44     }
45
46     @Override
47     public DOMEntityOwnershipListenerRegistration registerListener(final String entityType,
48             final DOMEntityOwnershipListener listener) {
49         return delegate.registerListener(entityType, listener);
50     }
51
52     @Override
53     public Optional<EntityOwnershipState> getOwnershipState(final DOMEntity forEntity) {
54         return delegate.getOwnershipState(forEntity);
55     }
56
57     @Override
58     public boolean isCandidateRegistered(final DOMEntity forEntity) {
59         return delegate.isCandidateRegistered(forEntity);
60     }
61
62     @Activate
63     // FIXME: 3.0.0: properties are keyed by String, this should be Map<String, Object>
64     void activate(final Map<Object, Object> properties) {
65         LOG.info("Distributed Entity Ownership Service starting");
66         delegate = DistributedEntityOwnershipService.start(operDatastore.getActorUtils(),
67             EntityOwnerSelectionStrategyConfigReader.loadStrategyWithConfig(properties));
68         LOG.info("Distributed Entity Ownership Service started");
69     }
70
71     @Deactivate
72     void deactivate() {
73         LOG.info("Distributed Entity Ownership Service stopping");
74         delegate.close();
75         LOG.info("Distributed Entity Ownership Service stopped");
76     }
77 }