Bug 6396: Integrate l2-l3-domain-extension with clustering singleton service
[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 com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
15 import org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry;
16 import org.opendaylight.groupbasedpolicy.domain_extension.l2_l3.L2L3NetworkDomainAugmentor;
17 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
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.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class L2L3DomainExtensionInstance implements ClusterSingletonService, AutoCloseable {
25
26     private static final Logger LOG = LoggerFactory.getLogger(L2L3DomainExtensionInstance.class);
27
28     private static final ServiceGroupIdentifier IDENTIFIER =
29             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
30     private final DomainSpecificRegistry domainSpecificRegistry;
31     private ClusterSingletonServiceProvider clusterSingletonService;
32     private ClusterSingletonServiceRegistration singletonServiceRegistration;
33     private L2L3NetworkDomainAugmentor l2l3NetworkDomainAugmentor;
34
35     public L2L3DomainExtensionInstance(final DomainSpecificRegistry domainSpecificRegistry,
36                                        final ClusterSingletonServiceProvider clusterSingletonService) {
37         this.domainSpecificRegistry = Preconditions.checkNotNull(domainSpecificRegistry);
38         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
39     }
40
41     public void initialize() {
42         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
43         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
44     }
45
46     @Override
47     public void instantiateServiceInstance() {
48         LOG.info("Instantiating {}", this.getClass().getSimpleName());
49         l2l3NetworkDomainAugmentor = new L2L3NetworkDomainAugmentor(domainSpecificRegistry.getNetworkDomainAugmentorRegistry());
50     }
51
52     @Override
53     public ListenableFuture<Void> closeServiceInstance() {
54         LOG.info("Instance {} closed", this.getClass().getSimpleName());
55         l2l3NetworkDomainAugmentor.close();
56         return Futures.immediateFuture(null);
57     }
58
59     @Override
60     public void close() {
61         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
62         if (singletonServiceRegistration != null) {
63             try {
64                 singletonServiceRegistration.close();
65             } catch (Exception e) {
66                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
67             }
68             singletonServiceRegistration = null;
69         }
70     }
71
72     @Override
73     public ServiceGroupIdentifier getIdentifier() {
74         return IDENTIFIER;
75     }
76 }