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.concurrent.ExecutionException;
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.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
23 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class LifecycleServiceImpl implements LifecycleService {
32 private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
34 private DeviceContext deviceContext;
35 private RpcContext rpcContext;
36 private RoleContext roleContext;
37 private StatisticsContext statContext;
38 private ClusterSingletonServiceRegistration registration;
42 public void instantiateServiceInstance() {
45 LOG.info("Starting device context cluster services for node {}", getIdentifier());
46 this.deviceContext.startupClusterServices();
48 LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
49 this.statContext.startupClusterServices();
51 LOG.info("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
52 this.deviceContext.initialSubmitTransaction();
54 LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
55 this.rpcContext.startupClusterServices();
57 LOG.info("Starting role context cluster services for node {}", getIdentifier());
58 this.roleContext.startupClusterServices();
60 LOG.info("Caching flows IDs ...");
61 fillDeviceFlowRegistry();
63 } catch (ExecutionException | InterruptedException e) {
64 LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
69 public ListenableFuture<Void> closeServiceInstance() {
70 statContext.stopClusterServices();
71 rpcContext.stopClusterServices();
72 return deviceContext.stopClusterServices();
76 public ServiceGroupIdentifier getIdentifier() {
77 return deviceContext.getServiceIdentifier();
82 public void close() throws Exception {
83 if (registration != null) {
90 public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
91 this.registration = singletonServiceProvider.registerClusterSingletonService(this);
95 public void setDeviceContext(final DeviceContext deviceContext) {
96 this.deviceContext = deviceContext;
100 public void setRpcContext(final RpcContext rpcContext) {
101 this.rpcContext = rpcContext;
105 public void setRoleContext(final RoleContext roleContext) {
106 this.roleContext = roleContext;
110 public void setStatContext(final StatisticsContext statContext) {
111 this.statContext = statContext;
115 public DeviceContext getDeviceContext() {
116 return this.deviceContext;
120 public void closeConnection() {
121 this.deviceContext.shutdownConnection();
124 private void fillDeviceFlowRegistry() {
125 // Fill device flow registry with flows from datastore
126 final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
128 // Start statistics scheduling only after we finished initializing device flow registry
129 Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
131 public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
132 if (LOG.isDebugEnabled()) {
133 // Count all flows we read from datastore for debugging purposes.
134 // This number do not always represent how many flows were actually added
135 // to DeviceFlowRegistry, because of possible duplicates.
136 long flowCount = Optional.fromNullable(result).asSet().stream()
137 .flatMap(Collection::stream)
138 .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
139 .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
140 .flatMap(table -> table.getFlow().stream())
143 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, getIdentifier());
148 public void onFailure(Throwable t) {
149 if (deviceFlowRegistryFill.isCancelled()) {
150 LOG.debug("Cancelled filling flow registry with flows for node: {}", getIdentifier());
152 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", getIdentifier(), t);