8e410759846ea3c48101c248ebdb05229ac72d2d
[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         try {
44             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
45         }
46         catch (Exception e) {
47             LOG.warn("Exception while registering candidate ... ", e);
48         }
49     }
50
51     @Override
52     public void instantiateServiceInstance() {
53         LOG.info("Instantiating {}", this.getClass().getSimpleName());
54         l2l3NetworkDomainAugmentor = new L2L3NetworkDomainAugmentor(domainSpecificRegistry.getNetworkDomainAugmentorRegistry());
55     }
56
57     @Override
58     public ListenableFuture<Void> closeServiceInstance() {
59         LOG.info("Instance {} closed", this.getClass().getSimpleName());
60         l2l3NetworkDomainAugmentor.close();
61         return Futures.immediateFuture(null);
62     }
63
64     @Override
65     public void close() {
66         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
67         if (singletonServiceRegistration != null) {
68             try {
69                 singletonServiceRegistration.close();
70             } catch (Exception e) {
71                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
72             }
73             singletonServiceRegistration = null;
74         }
75     }
76
77     @Override
78     public ServiceGroupIdentifier getIdentifier() {
79         return IDENTIFIER;
80     }
81 }