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.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 java.util.concurrent.ExecutionException;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
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;
44 public void instantiateServiceInstance() {
47 if (LOG.isDebugEnabled()) {
48 LOG.debug("Starting clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
49 LOG.debug("===============================================");
52 if (connectionInterrupted()) {
56 LOG.info("Starting device context cluster services for node {}", getIdentifier());
57 this.deviceContext.startupClusterServices();
59 if (connectionInterrupted()) {
63 LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
64 this.statContext.startupClusterServices();
66 if (connectionInterrupted()) {
70 LOG.info("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
71 this.deviceContext.initialSubmitTransaction();
73 if (connectionInterrupted()) {
77 LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
78 this.rpcContext.startupClusterServices();
80 if (connectionInterrupted()) {
84 LOG.info("Starting role context cluster services for node {}", getIdentifier());
85 this.roleContext.startupClusterServices();
87 if (connectionInterrupted()) {
91 LOG.info("Caching flows IDs ...");
92 fillDeviceFlowRegistry();
94 } catch (ExecutionException | InterruptedException e) {
95 LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
96 this.deviceContext.shutdownConnection();
100 private boolean connectionInterrupted() {
101 if (this.deviceContext.getPrimaryConnectionContext().getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
102 LOG.warn("Node {} was disconnected, will stop starting MASTER services.", this.deviceContext.getDeviceInfo().getLOGValue());
109 public ListenableFuture<Void> closeServiceInstance() {
110 if (LOG.isDebugEnabled()) {
111 LOG.debug("Stopping clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
112 LOG.debug("===============================================");
115 LOG.info("Stopping role context cluster services for node {}", getIdentifier());
116 roleContext.stopClusterServices();
118 LOG.info("Stopping statistics context cluster services for node {}", getIdentifier());
119 statContext.stopClusterServices();
121 LOG.info("Stopping rpc context cluster services for node {}", getIdentifier());
122 rpcContext.stopClusterServices();
124 LOG.info("Stopping device context cluster services for node {}", getIdentifier());
125 return deviceContext.stopClusterServices();
129 public ServiceGroupIdentifier getIdentifier() {
130 return deviceContext.getServiceIdentifier();
135 public void close() throws Exception {
136 if (registration != null) {
137 registration.close();
143 public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
144 this.registration = singletonServiceProvider.registerClusterSingletonService(this);
148 public void setDeviceContext(final DeviceContext deviceContext) {
149 this.deviceContext = deviceContext;
153 public void setRpcContext(final RpcContext rpcContext) {
154 this.rpcContext = rpcContext;
158 public void setRoleContext(final RoleContext roleContext) {
159 this.roleContext = roleContext;
163 public void setStatContext(final StatisticsContext statContext) {
164 this.statContext = statContext;
168 public DeviceContext getDeviceContext() {
169 return this.deviceContext;
173 public void closeConnection() {
174 this.deviceContext.shutdownConnection();
177 private void fillDeviceFlowRegistry() {
178 // Fill device flow registry with flows from datastore
179 final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
181 // Start statistics scheduling only after we finished initializing device flow registry
182 Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
184 public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
185 if (LOG.isDebugEnabled()) {
186 // Count all flows we read from datastore for debugging purposes.
187 // This number do not always represent how many flows were actually added
188 // to DeviceFlowRegistry, because of possible duplicates.
189 long flowCount = Optional.fromNullable(result).asSet().stream()
190 .flatMap(Collection::stream)
191 .filter(Objects::nonNull)
192 .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
193 .filter(Objects::nonNull)
194 .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
195 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
196 .filter(Objects::nonNull)
197 .filter(table -> Objects.nonNull(table.getFlow()))
198 .flatMap(table -> table.getFlow().stream())
199 .filter(Objects::nonNull)
202 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
207 public void onFailure(Throwable t) {
208 if (deviceFlowRegistryFill.isCancelled()) {
209 LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
211 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);