2 * Copyright (c) 2015 Cisco Systems, Inc. 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
9 package org.opendaylight.netconf.topology;
11 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
18 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class TestingEntityOwnershipService implements EntityOwnershipService{
30 private static final Logger LOG = LoggerFactory.getLogger(TestingEntityOwnershipService.class);
32 private final List<EntityOwnershipListener> listeners = new ArrayList<>();
33 private final ExecutorService executorService = Executors.newFixedThreadPool(1);
35 private Entity entity;
36 private boolean masterSet = false;
39 public EntityOwnershipCandidateRegistration registerCandidate(final Entity entity) throws CandidateAlreadyRegisteredException {
40 LOG.warn("Registering Candidate");
42 return new EntityOwnershipCandidateRegistration() {
45 LOG.debug("Closing candidate registration");
49 public Entity getInstance() {
56 public EntityOwnershipListenerRegistration registerListener(final String entityType, final EntityOwnershipListener listener) {
57 listeners.add(listener);
58 if (listeners.size() == 3) {
59 distributeOwnership();
61 return new EntityOwnershipListenerRegistration() {
64 public String getEntityType() {
70 listeners.remove(listener);
74 public EntityOwnershipListener getInstance() {
81 public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
86 public boolean isCandidateRegistered(@Nonnull Entity entity) {
90 public void distributeOwnership() {
91 LOG.debug("Distributing ownership");
92 executorService.submit(new Runnable() {
96 LOG.debug("Distributing ownership for {} listeners", listeners.size());
97 for (final EntityOwnershipListener listener : listeners) {
99 listener.ownershipChanged(new EntityOwnershipChange(entity, false, true, true));
102 listener.ownershipChanged(new EntityOwnershipChange(entity, false, false, true));