Bug 6743: Service group identifier set for neutron-vpp-mapper and vpp-renderer
[groupbasedpolicy.git] / neutron-vpp-mapper / src / main / java / org / opendaylight / controller / config / yang / config / neutron_vpp_mapper / impl / NeutronVppMapperInstance.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.neutron_vpp_mapper.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.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.NeutronVppMapper;
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 NeutronVppMapperInstance implements ClusterSingletonService, AutoCloseable {
25
26     private static final Logger LOG = LoggerFactory.getLogger(NeutronVppMapperInstance.class);
27
28     private static final ServiceGroupIdentifier IDENTIFIER =
29             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
30     private final DataBroker dataBroker;
31     private final String socketPath;
32     private final String socketPrefix;
33     private final ClusterSingletonServiceProvider clusterSingletonService;
34     private ClusterSingletonServiceRegistration singletonServiceRegistration;
35     private NeutronVppMapper mapper;
36
37     public NeutronVppMapperInstance(final DataBroker dataBroker,
38                                     final String socketPath,
39                                     final String socketPrefix,
40                                     final ClusterSingletonServiceProvider clusterSingletonService) {
41         this.dataBroker = Preconditions.checkNotNull(dataBroker);
42         this.socketPath = Preconditions.checkNotNull(socketPath);
43         this.socketPrefix = Preconditions.checkNotNull(socketPrefix);
44         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
45     }
46
47     public void initialize() {
48         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
49         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
50     }
51
52     @Override
53     public void instantiateServiceInstance() {
54         LOG.info("Instantiating {}", this.getClass().getSimpleName());
55         mapper = new NeutronVppMapper(socketPath, socketPrefix, dataBroker);
56     }
57
58     @Override
59     public ListenableFuture<Void> closeServiceInstance() {
60         LOG.info("Instance {} closed", this.getClass().getSimpleName());
61         mapper.close();
62         return Futures.immediateFuture(null);
63     }
64
65     @Override
66     public void close() throws Exception {
67         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
68         if (singletonServiceRegistration != null) {
69             try {
70                 singletonServiceRegistration.close();
71             } catch (Exception e) {
72                 LOG.warn("{} closed unexpectedly.", this.getClass().getSimpleName(), e.getMessage());
73             }
74             singletonServiceRegistration = null;
75         }
76     }
77
78     @Override
79     public ServiceGroupIdentifier getIdentifier() {
80         return IDENTIFIER;
81     }
82 }