apply checkstyle check during build for domain-extensions
[groupbasedpolicy.git] / domain-extensions / l2-l3 / src / main / java / org / opendaylight / controller / config / yang / config / domain_extension / l2_l3 / impl / L2L3DomainExtensionInstance.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
9 package org.opendaylight.controller.config.yang.config.domain_extension.l2_l3.impl;
10
11 import javax.annotation.Nonnull;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
17 import org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry;
18 import org.opendaylight.groupbasedpolicy.domain_extension.l2_l3.L2L3NetworkDomainAugmentor;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class L2L3DomainExtensionInstance implements ClusterSingletonService, AutoCloseable {
27
28     private static final Logger LOG = LoggerFactory.getLogger(L2L3DomainExtensionInstance.class);
29
30     private static final ServiceGroupIdentifier IDENTIFIER =
31             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
32     private final DomainSpecificRegistry domainSpecificRegistry;
33     private ClusterSingletonServiceProvider clusterSingletonService;
34     private ClusterSingletonServiceRegistration singletonServiceRegistration;
35     private L2L3NetworkDomainAugmentor l2l3NetworkDomainAugmentor;
36
37     public L2L3DomainExtensionInstance(final DomainSpecificRegistry domainSpecificRegistry,
38                                        final ClusterSingletonServiceProvider clusterSingletonService) {
39         this.domainSpecificRegistry = Preconditions.checkNotNull(domainSpecificRegistry);
40         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
41     }
42
43     public void initialize() {
44         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
45         try {
46             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
47         }
48         catch (Exception e) {
49             LOG.warn("Exception while registering candidate ... ", e);
50         }
51     }
52
53     @Override
54     public void instantiateServiceInstance() {
55         LOG.info("Instantiating {}", this.getClass().getSimpleName());
56         l2l3NetworkDomainAugmentor =
57             new L2L3NetworkDomainAugmentor(domainSpecificRegistry.getNetworkDomainAugmentorRegistry());
58     }
59
60     @Override
61     public ListenableFuture<Void> closeServiceInstance() {
62         LOG.info("Instance {} closed", this.getClass().getSimpleName());
63         l2l3NetworkDomainAugmentor.close();
64         return Futures.immediateFuture(null);
65     }
66
67     @Override
68     public void close() {
69         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
70         if (singletonServiceRegistration != null) {
71             try {
72                 singletonServiceRegistration.close();
73             } catch (Exception e) {
74                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
75             }
76             singletonServiceRegistration = null;
77         }
78     }
79
80     @Override
81     @Nonnull
82     public ServiceGroupIdentifier getIdentifier() {
83         return IDENTIFIER;
84     }
85 }