Bug 5596 Cleaning part 1
[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("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
45             this.deviceContext.initialSubmitTransaction();
46
47             LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
48             this.rpcContext.startupClusterServices();
49
50             LOG.info("Starting role context cluster services for node {}", getIdentifier());
51             this.roleContext.startupClusterServices();
52
53         } catch (ExecutionException | InterruptedException e) {
54             LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
55         }
56     }
57
58     @Override
59     public ListenableFuture<Void> closeServiceInstance() {
60         statContext.stopClusterServices();
61         rpcContext.stopClusterServices();
62         return deviceContext.stopClusterServices();
63     }
64
65     @Override
66     public ServiceGroupIdentifier getIdentifier() {
67         return deviceContext.getServiceIdentifier();
68     }
69
70
71     @Override
72     public void close() throws Exception {
73         if (registration != null) {
74             registration.close();
75             registration = null;
76         }
77     }
78
79     @Override
80     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
81         this.registration = singletonServiceProvider.registerClusterSingletonService(this);
82     }
83
84     @Override
85     public void setDeviceContext(final DeviceContext deviceContext) {
86         this.deviceContext = deviceContext;
87     }
88
89     @Override
90     public void setRpcContext(final RpcContext rpcContext) {
91         this.rpcContext = rpcContext;
92     }
93
94     @Override
95     public void setRoleContext(final RoleContext roleContext) {
96         this.roleContext = roleContext;
97     }
98
99     @Override
100     public void setStatContext(final StatisticsContext statContext) {
101         this.statContext = statContext;
102     }
103 }