BUG-6650: ep-ip/sgt, update/rename models and yangs for sxp-ise-adapter
[groupbasedpolicy.git] / sxp-mapper / src / main / java / org / opendaylight / controller / config / yang / config / groupbasedpolicy / sxp_mapper / SxpMapperProviderInstance.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.groupbasedpolicy.sxp_mapper;
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.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.groupbasedpolicy.api.DomainSpecificRegistry;
17 import org.opendaylight.groupbasedpolicy.sxp.mapper.impl.SxpMapperProviderImpl;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class SxpMapperProviderInstance implements ClusterSingletonService, AutoCloseable {
27
28     private static final Logger LOG = LoggerFactory.getLogger(SxpMapperProviderInstance.class);
29
30     private static final ServiceGroupIdentifier IDENTIFIER =
31             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
32     private final DataBroker dataBroker;
33     private final BaseEndpointService endpointService;
34     private final DomainSpecificRegistry registry;
35     private final ClusterSingletonServiceProvider clusterSingletonService;
36     private ClusterSingletonServiceRegistration singletonServiceRegistration;
37     private SxpMapperProviderImpl sxpMapperProviderImpl;
38
39     public SxpMapperProviderInstance(final DataBroker dataBroker,
40                                      final BaseEndpointService endpointService,
41                                      final DomainSpecificRegistry registry,
42                                      final ClusterSingletonServiceProvider clusterSingletonService) {
43         this.dataBroker = Preconditions.checkNotNull(dataBroker);
44         this.endpointService = Preconditions.checkNotNull(endpointService);
45         this.registry = Preconditions.checkNotNull(registry);
46         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
47     }
48
49     public void initialize() {
50         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
51         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
52     }
53
54     @Override
55     public void instantiateServiceInstance() {
56         LOG.info("Instantiating {}", this.getClass().getSimpleName());
57         sxpMapperProviderImpl = new SxpMapperProviderImpl(dataBroker, endpointService, registry);
58     }
59
60     @Override
61     public ListenableFuture<Void> closeServiceInstance() {
62         LOG.info("Instance {} closed", this.getClass().getSimpleName());
63         try {
64             sxpMapperProviderImpl.close();
65         } catch (Exception e) {
66             LOG.warn("Exception while closing ... {}", e.getMessage());
67         }
68         return Futures.immediateFuture(null);
69     }
70
71     @Override
72     public void close() throws Exception {
73         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
74         if (singletonServiceRegistration != null) {
75             try {
76                 singletonServiceRegistration.close();
77             } catch (Exception e) {
78                 LOG.warn("{} closed unexpectedly. Cause: {}", e.getMessage());
79             }
80             singletonServiceRegistration = null;
81         }
82     }
83
84     @Override
85     public ServiceGroupIdentifier getIdentifier() {
86         return IDENTIFIER;
87     }
88 }