Clustered sources resolution
[netconf.git] / opendaylight / 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.yangtools.yang.model.repo.api.SchemaRepository;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class MasterSourceProviderImpl extends RemoteYangTextSourceProviderImpl
27         implements MasterSourceProvider {
28
29     private static Logger LOG = LoggerFactory.getLogger(MasterSourceProviderImpl.class);
30
31     private final ActorSystem actorSystem;
32     private final String topologyId;
33     private final String nodeId;
34
35     public MasterSourceProviderImpl(SchemaRepository schemaRepo, Set<SourceIdentifier> providedSources, ActorSystem actorSystem, String topologyId, String nodeId) {
36         super(schemaRepo, providedSources);
37         this.actorSystem = actorSystem;
38         this.topologyId = topologyId;
39         this.nodeId = nodeId;
40     }
41
42     @Override
43     public void onReceive(Object o, ActorRef actorRef) {
44         if(o instanceof AnnounceClusteredDeviceSourcesResolverUp) {
45             LOG.debug("Received source resolver up");
46             actorRef.tell(new AnnounceMasterSourceProviderUp(), TypedActor.context().self());
47         }
48     }
49
50     @Override
51     public void preStart() {
52         Cluster cluster = Cluster.get(actorSystem);
53         cluster.join(cluster.selfAddress());
54         LOG.debug("Notifying members master schema source provider is up.");
55         for(Member node : cluster.state().getMembers()) {
56             final String path = node.address() + "/user/" + topologyId + "/" + nodeId + "/clusteredDeviceSourcesResolver";
57             if(node.address().equals(cluster.selfAddress())) {
58                 actorSystem.actorSelection(path).tell(new AnnounceMasterOnSameNodeUp(), TypedActor.context().self());
59                 actorSystem.actorSelection(path).tell(PoisonPill.getInstance(), TypedActor.context().self());
60             } else {
61                 //TODO extract string constant to util class
62                 actorSystem.actorSelection(path).tell(new AnnounceMasterSourceProviderUp(), TypedActor.context().self());
63             }
64         }
65     }
66 }