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 LOG.info("========== Start-up clustering MASTER services for node {} was SUCCESSFUL ==========", this.deviceContext.getDeviceInfo().getLOGValue());
52 LOG.warn("========== Start-up clustering MASTER services for node {} was UN-SUCCESSFUL ==========", this.deviceContext.getDeviceInfo().getLOGValue());
53 this.closeConnection();
59 public ListenableFuture<Void> closeServiceInstance() {
60 if (LOG.isDebugEnabled()) {
61 LOG.debug("========== Stopping clustering MASTER services for node {} ==========", this.deviceContext.getDeviceInfo().getLOGValue());
64 final boolean connectionInterrupted =
66 .getPrimaryConnectionContext()
68 .equals(ConnectionContext.CONNECTION_STATE.RIP);
70 LOG.info("Stopping role context cluster services for node {}", getIdentifier());
71 roleContext.stopClusterServices(connectionInterrupted);
73 LOG.info("Stopping statistics context cluster services for node {}", getIdentifier());
74 statContext.stopClusterServices(connectionInterrupted);
76 LOG.info("Stopping rpc context cluster services for node {}", getIdentifier());
77 rpcContext.stopClusterServices(connectionInterrupted);
79 LOG.info("Stopping device context cluster services for node {}", getIdentifier());
80 return deviceContext.stopClusterServices(connectionInterrupted);
84 public ServiceGroupIdentifier getIdentifier() {
85 return deviceContext.getServiceIdentifier();
90 public void close() throws Exception {
91 if (registration != null) {
98 public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
99 //lifecycle service -> device context -> statistics context -> rpc context -> role context -> lifecycle service
100 this.clusterInitializationPhaseHandler = deviceContext;
101 this.deviceContext.setLifecycleInitializationPhaseHandler(this.statContext);
102 this.statContext.setLifecycleInitializationPhaseHandler(this.rpcContext);
103 this.rpcContext.setLifecycleInitializationPhaseHandler(this.roleContext);
104 this.roleContext.setLifecycleInitializationPhaseHandler(this);
105 //Set initial submit handler
106 this.statContext.setInitialSubmitHandler(this.deviceContext);
107 //Register cluster singleton service
108 this.registration = singletonServiceProvider.registerClusterSingletonService(this);
112 public void setDeviceContext(final DeviceContext deviceContext) {
113 this.deviceContext = deviceContext;
117 public void setRpcContext(final RpcContext rpcContext) {
118 this.rpcContext = rpcContext;
122 public void setRoleContext(final RoleContext roleContext) {
123 this.roleContext = roleContext;
127 public void setStatContext(final StatisticsContext statContext) {
128 this.statContext = statContext;
132 public DeviceContext getDeviceContext() {
133 return this.deviceContext;
137 public void closeConnection() {
138 this.deviceContext.shutdownConnection();
141 private void fillDeviceFlowRegistry() {
142 // Fill device flow registry with flows from datastore
143 final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
145 // Start statistics scheduling only after we finished initializing device flow registry
146 Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
148 public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
149 if (LOG.isDebugEnabled()) {
150 // Count all flows we read from datastore for debugging purposes.
151 // This number do not always represent how many flows were actually added
152 // to DeviceFlowRegistry, because of possible duplicates.
153 long flowCount = Optional.fromNullable(result).asSet().stream()
154 .flatMap(Collection::stream)
155 .filter(Objects::nonNull)
156 .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
157 .filter(Objects::nonNull)
158 .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
159 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
160 .filter(Objects::nonNull)
161 .filter(table -> Objects.nonNull(table.getFlow()))
162 .flatMap(table -> table.getFlow().stream())
163 .filter(Objects::nonNull)
166 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
171 public void onFailure(Throwable t) {
172 if (deviceFlowRegistryFill.isCancelled()) {
173 LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
175 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);
182 public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
183 this.clusterInitializationPhaseHandler = handler;
187 public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
189 if (ConnectionContext.CONNECTION_STATE.RIP.equals(connectionContext.getConnectionState())) {
190 if (LOG.isDebugEnabled()) {
191 LOG.debug("Connection to the device {} was interrupted.", this.deviceContext.getDeviceInfo().getLOGValue());
196 LOG.info("Caching flows IDs ...");
197 fillDeviceFlowRegistry();