77ea3e975340452c5a772884b7fcb320dbf58366
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.lifecycle;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
13 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
14 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
17 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class LifecycleServiceImpl implements LifecycleService {
24
25     private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
26
27     private DeviceContext deviceContext;
28     private RpcContext rpcContext;
29     private RoleContext roleContext;
30     private StatisticsContext statContext;
31     private ClusterSingletonServiceRegistration registration;
32
33
34     @Override
35     public void instantiateServiceInstance() {
36         try {
37
38             LOG.info("Starting device context cluster services for node {}", getIdentifier());
39             this.deviceContext.startupClusterServices();
40
41             LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
42             this.statContext.startupClusterServices();
43
44             LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
45             this.rpcContext.startupClusterServices();
46
47             LOG.info("Starting role context cluster services for node {}", getIdentifier());
48             this.roleContext.startupClusterServices();
49
50         } catch (ExecutionException | InterruptedException e) {
51             LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
52         }
53     }
54
55     @Override
56     public ListenableFuture<Void> closeServiceInstance() {
57         statContext.stopClusterServices();
58         rpcContext.stopClusterServices();
59         return deviceContext.stopClusterServices();
60     }
61
62     @Override
63     public ServiceGroupIdentifier getIdentifier() {
64         return deviceContext.getServiceIdentifier();
65     }
66
67
68     @Override
69     public void close() throws Exception {
70         if (registration != null) {
71             registration.close();
72             registration = null;
73         }
74     }
75
76     @Override
77     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
78         this.registration = singletonServiceProvider.registerClusterSingletonService(this);
79     }
80
81     @Override
82     public void setDeviceContext(final DeviceContext deviceContext) {
83         this.deviceContext = deviceContext;
84     }
85
86     @Override
87     public void setRpcContext(final RpcContext rpcContext) {
88         this.rpcContext = rpcContext;
89     }
90
91     @Override
92     public void setRoleContext(final RoleContext roleContext) {
93         this.roleContext = roleContext;
94     }
95
96     @Override
97     public void setStatContext(final StatisticsContext statContext) {
98         this.statContext = statContext;
99     }
100 }