2 * Copyright (c) 2016 Pantheon Technologies s.r.o. 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.lifecycle;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Objects;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
24 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
25 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
26 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class LifecycleServiceImpl implements LifecycleService {
34 private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
36 private DeviceContext deviceContext;
37 private RpcContext rpcContext;
38 private RoleContext roleContext;
39 private StatisticsContext statContext;
40 private ClusterSingletonServiceRegistration registration;
41 private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
45 public void instantiateServiceInstance() {
47 LOG.info("Starting clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
49 if (!this.clusterInitializationPhaseHandler.onContextInstantiateService(null)) {
50 this.closeConnection();
56 public ListenableFuture<Void> closeServiceInstance() {
58 LOG.info("Stopping clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
60 final boolean connectionInterrupted =
62 .getPrimaryConnectionContext()
64 .equals(ConnectionContext.CONNECTION_STATE.RIP);
66 roleContext.stopClusterServices(connectionInterrupted);
67 statContext.stopClusterServices(connectionInterrupted);
68 rpcContext.stopClusterServices(connectionInterrupted);
69 return deviceContext.stopClusterServices(connectionInterrupted);
74 public ServiceGroupIdentifier getIdentifier() {
75 return deviceContext.getServiceIdentifier();
80 public void close() throws Exception {
81 if (registration != null) {
82 LOG.info("Unregistering clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
89 public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
90 LOG.info("Registering clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
92 //lifecycle service -> device context -> statistics context -> rpc context -> role context -> lifecycle service
93 this.clusterInitializationPhaseHandler = deviceContext;
94 this.deviceContext.setLifecycleInitializationPhaseHandler(this.statContext);
95 this.statContext.setLifecycleInitializationPhaseHandler(this.rpcContext);
96 this.rpcContext.setLifecycleInitializationPhaseHandler(this.roleContext);
97 this.roleContext.setLifecycleInitializationPhaseHandler(this);
98 //Set initial submit handler
99 this.statContext.setInitialSubmitHandler(this.deviceContext);
100 //Register cluster singleton service
101 this.registration = singletonServiceProvider.registerClusterSingletonService(this);
105 public void setDeviceContext(final DeviceContext deviceContext) {
106 this.deviceContext = deviceContext;
110 public void setRpcContext(final RpcContext rpcContext) {
111 this.rpcContext = rpcContext;
115 public void setRoleContext(final RoleContext roleContext) {
116 this.roleContext = roleContext;
120 public void setStatContext(final StatisticsContext statContext) {
121 this.statContext = statContext;
125 public DeviceContext getDeviceContext() {
126 return this.deviceContext;
130 public void closeConnection() {
131 this.deviceContext.shutdownConnection();
134 private void fillDeviceFlowRegistry() {
136 final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
137 Futures.addCallback(deviceFlowRegistryFill, new DeviceFlowRegistryCallback(deviceFlowRegistryFill));
141 public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
142 this.clusterInitializationPhaseHandler = handler;
146 public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
148 if (ConnectionContext.CONNECTION_STATE.RIP.equals(connectionContext.getConnectionState())) {
149 if (LOG.isDebugEnabled()) {
150 LOG.debug("Connection to the device {} was interrupted.", this.deviceContext.getDeviceInfo().getLOGValue());
155 fillDeviceFlowRegistry();
159 private class DeviceFlowRegistryCallback implements FutureCallback<List<Optional<FlowCapableNode>>> {
160 private final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill;
162 public DeviceFlowRegistryCallback(ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill) {
163 this.deviceFlowRegistryFill = deviceFlowRegistryFill;
167 public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
168 if (LOG.isDebugEnabled()) {
169 // Count all flows we read from datastore for debugging purposes.
170 // This number do not always represent how many flows were actually added
171 // to DeviceFlowRegistry, because of possible duplicates.
172 long flowCount = Optional.fromNullable(result).asSet().stream()
173 .flatMap(Collection::stream)
174 .filter(Objects::nonNull)
175 .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
176 .filter(Objects::nonNull)
177 .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
178 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
179 .filter(Objects::nonNull)
180 .filter(table -> Objects.nonNull(table.getFlow()))
181 .flatMap(table -> table.getFlow().stream())
182 .filter(Objects::nonNull)
185 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
190 public void onFailure(Throwable t) {
191 if (deviceFlowRegistryFill.isCancelled()) {
192 if (LOG.isDebugEnabled()) {
193 LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
196 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);