2 * Copyright (c) 2016 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
8 package org.opendaylight.netconf.topology.singleton.impl;
10 import static java.util.Objects.requireNonNull;
12 import akka.util.Timeout;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
18 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
19 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
20 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
21 import org.opendaylight.netconf.client.mdsal.api.SchemaResourceManager;
22 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
23 import org.opendaylight.netconf.topology.spi.NetconfClientConfigurationBuilderFactory;
24 import org.opendaylight.netconf.topology.spi.NetconfNodeUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev231121.NetconfNode;
26 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 class NetconfTopologyContext implements ClusterSingletonService, AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyContext.class);
33 private final @NonNull ServiceGroupIdentifier serviceGroupIdent;
34 private final NetconfNodeContext topologySingleton;
36 private volatile boolean closed;
37 private volatile boolean isMaster;
39 private RemoteDeviceId remoteDeviceId;
41 NetconfTopologyContext(final SchemaResourceManager schemaManager,
42 final DOMMountPointService mountPointService, final NetconfClientConfigurationBuilderFactory builderFactory,
43 final DeviceActionFactory deviceActionFactory, final Timeout actorResponseWaitTime,
44 final ServiceGroupIdentifier serviceGroupIdent, final NetconfTopologySetup setup) {
45 this.serviceGroupIdent = requireNonNull(serviceGroupIdent);
46 remoteDeviceId = NetconfNodeUtils.toRemoteDeviceId(setup.getNode().getNodeId(),
47 setup.getNode().augmentation(NetconfNode.class));
49 topologySingleton = new NetconfNodeContext(setup, schemaManager, mountPointService, builderFactory,
50 deviceActionFactory, remoteDeviceId, actorResponseWaitTime);
54 protected NetconfNodeContext getTopologySingleton() {
55 // FIXME we have to access topology singleton via this method because of mocking in MountPointEndToEndTest
56 return topologySingleton;
60 public void instantiateServiceInstance() {
61 LOG.info("Leader was selected: {}", remoteDeviceId.host().getIpAddress());
63 LOG.warn("Instance is already closed.");
67 getTopologySingleton().becomeTopologyLeader();
70 // called when master is down/changed to slave
72 public ListenableFuture<?> closeServiceInstance() {
73 // this method is also called when topology data are deleted
74 LOG.info("Follower was selected: {}", remoteDeviceId.host().getIpAddress());
76 LOG.warn("Instance is already closed.");
77 return FluentFutures.immediateNullFluentFuture();
79 getTopologySingleton().becomeTopologyFollower();
80 return FluentFutures.immediateNullFluentFuture();
83 void refresh(final @NonNull NetconfTopologySetup setup) {
84 final var node = requireNonNull(setup).getNode();
85 remoteDeviceId = NetconfNodeUtils.toRemoteDeviceId(node.getNodeId(), node.augmentation(NetconfNode.class));
87 final var singleton = getTopologySingleton();
89 singleton.refreshSetupConnection(setup, remoteDeviceId);
91 singleton.refreshDevice(setup, remoteDeviceId);
96 public ServiceGroupIdentifier getIdentifier() {
97 return serviceGroupIdent;
101 public void close() {
103 LOG.warn("Instance is already closed.");
106 getTopologySingleton().close();