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
8 package org.opendaylight.openflowplugin.impl.role;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.Iterators;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Objects;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import javax.annotation.CheckForNull;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
33 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
34 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
35 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
36 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
37 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
38 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
39 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
40 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Gets invoked from RpcManagerInitial, registers a candidate with EntityOwnershipService.
47 * On receipt of the ownership notification, makes an rpc call to SalRoleService.
49 * Hands over to StatisticsManager at the end.
51 public class RoleManagerImpl implements RoleManager {
52 private static final Logger LOG = LoggerFactory.getLogger(RoleManagerImpl.class);
54 // Maximum limit of timeout retries when cleaning DS, to prevent infinite recursive loops
55 private static final int MAX_CLEAN_DS_RETRIES = 3;
57 private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
58 private DeviceTerminationPhaseHandler deviceTerminationPhaseHandler;
59 private final DataBroker dataBroker;
60 private final ConcurrentMap<DeviceInfo, RoleContext> contexts = new ConcurrentHashMap<>();
61 private List<RoleChangeListener> listeners = new ArrayList<>();
63 private final LifecycleConductor conductor;
65 public RoleManagerImpl(final DataBroker dataBroker, final LifecycleConductor lifecycleConductor) {
66 this.dataBroker = Preconditions.checkNotNull(dataBroker);
67 this.conductor = lifecycleConductor;
71 public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
72 deviceInitializationPhaseHandler = handler;
76 public void onDeviceContextLevelUp(@CheckForNull final DeviceInfo deviceInfo, final LifecycleService lifecycleService) throws Exception {
77 final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
78 final RoleContext roleContext = new RoleContextImpl(deviceInfo, conductor, this);
79 roleContext.setSalRoleService(new SalRoleServiceImpl(roleContext, deviceContext));
80 Verify.verify(contexts.putIfAbsent(deviceInfo, roleContext) == null, "Role context for master Node %s is still not closed.", deviceInfo.getNodeId());
81 lifecycleService.setRoleContext(roleContext);
82 deviceInitializationPhaseHandler.onDeviceContextLevelUp(deviceInfo, lifecycleService);
87 LOG.debug("Close method on role manager was called.");
88 for (final Iterator<RoleContext> iterator = Iterators.consumingIterator(contexts.values().iterator()); iterator.hasNext();) {
89 // got here because last known role is LEADER and DS might need clearing up
90 final RoleContext roleContext = iterator.next();
91 contexts.remove(roleContext.getDeviceInfo());
92 removeDeviceFromOperationalDS(roleContext.getDeviceInfo(), MAX_CLEAN_DS_RETRIES);
97 public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
98 LOG.trace("onDeviceContextLevelDown for node {}", deviceInfo.getNodeId());
99 final RoleContext roleContext = contexts.remove(deviceInfo);
100 if (roleContext != null) {
101 LOG.debug("Found roleContext associated to deviceContext: {}, now trying close the roleContext", deviceInfo.getNodeId());
102 contexts.remove(deviceInfo.getNodeId(), roleContext);
104 deviceTerminationPhaseHandler.onDeviceContextLevelDown(deviceInfo);
108 public CheckedFuture<Void, TransactionCommitFailedException> removeDeviceFromOperationalDS(final DeviceInfo deviceInfo, final int numRetries) {
109 final WriteTransaction delWtx = dataBroker.newWriteOnlyTransaction();
110 delWtx.delete(LogicalDatastoreType.OPERATIONAL, DeviceStateUtil.createNodeInstanceIdentifier(deviceInfo.getNodeId()));
111 final CheckedFuture<Void, TransactionCommitFailedException> delFuture = delWtx.submit();
113 Futures.addCallback(delFuture, new FutureCallback<Void>() {
115 public void onSuccess(final Void result) {
116 LOG.debug("Delete Node {} was successful", deviceInfo);
117 contexts.remove(deviceInfo);
121 public void onFailure(@Nonnull final Throwable t) {
122 // If we have any retries left, we will try to clean the datastore again
123 if (numRetries > 0) {
124 // We "used" one retry here, so decrement it
125 final int curRetries = numRetries - 1;
126 LOG.debug("Delete node {} failed with exception {}. Trying again (retries left: {})", deviceInfo.getNodeId(), t, curRetries);
127 // Recursive call to this method with "one less" retry
128 removeDeviceFromOperationalDS(deviceInfo, curRetries);
132 // No retries left, so we will just close the role context, and ignore datastore cleanup
133 LOG.warn("Delete node {} failed with exception {}. No retries left, aborting", deviceInfo.getNodeId(), t);
134 contexts.remove(deviceInfo);
142 public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
143 deviceTerminationPhaseHandler = handler;
147 RoleContext getRoleContext(final DeviceInfo deviceInfo){
148 return contexts.get(deviceInfo);
152 public <T extends OFPContext> T gainContext(final DeviceInfo deviceInfo) {
153 return (T) contexts.get(deviceInfo);