Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / pipeline / MasterSourceProviderImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.topology.pipeline;
9
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.PoisonPill;
13 import akka.actor.TypedActor;
14 import akka.cluster.Cluster;
15 import akka.cluster.Member;
16 import java.util.Set;
17 import org.opendaylight.controller.cluster.schema.provider.impl.RemoteYangTextSourceProviderImpl;
18 import org.opendaylight.netconf.topology.pipeline.messages.AnnounceClusteredDeviceSourcesResolverUp;
19 import org.opendaylight.netconf.topology.pipeline.messages.AnnounceMasterOnSameNodeUp;
20 import org.opendaylight.netconf.topology.pipeline.messages.AnnounceMasterSourceProviderUp;
21 import org.opendaylight.netconf.util.NetconfTopologyPathCreator;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class MasterSourceProviderImpl extends RemoteYangTextSourceProviderImpl
28         implements MasterSourceProvider {
29
30     private static Logger LOG = LoggerFactory.getLogger(MasterSourceProviderImpl.class);
31
32     private final ActorSystem actorSystem;
33     private final String topologyId;
34     private final String nodeId;
35
36     public MasterSourceProviderImpl(SchemaRepository schemaRepo, Set<SourceIdentifier> providedSources, ActorSystem actorSystem, String topologyId, String nodeId) {
37         super(schemaRepo, providedSources);
38         this.actorSystem = actorSystem;
39         this.topologyId = topologyId;
40         this.nodeId = nodeId;
41     }
42
43     @Override
44     public void onReceive(Object o, ActorRef actorRef) {
45         if(o instanceof AnnounceClusteredDeviceSourcesResolverUp) {
46             LOG.debug("Received source resolver up");
47             actorRef.tell(new AnnounceMasterSourceProviderUp(), TypedActor.context().self());
48         }
49     }
50
51     @Override
52     public void preStart() {
53         Cluster cluster = Cluster.get(actorSystem);
54         cluster.join(cluster.selfAddress());
55         LOG.debug("Notifying members master schema source provider is up.");
56         for(Member node : cluster.state().getMembers()) {
57             final NetconfTopologyPathCreator pathCreator = new NetconfTopologyPathCreator(node.address().toString(),topologyId);
58             final String path = pathCreator.withSuffix(nodeId).withSuffix(NetconfTopologyPathCreator.CLUSTERED_DEVICE_SOURCES_RESOLVER).build();
59             if(node.address().equals(cluster.selfAddress())) {
60                 actorSystem.actorSelection(path).tell(new AnnounceMasterOnSameNodeUp(), TypedActor.context().self());
61                 actorSystem.actorSelection(path).tell(PoisonPill.getInstance(), TypedActor.context().self());
62             } else {
63                 //TODO extract string constant to util class
64                 actorSystem.actorSelection(path).tell(new AnnounceMasterSourceProviderUp(), TypedActor.context().self());
65             }
66         }
67     }
68 }