96d9c8a95d8cb576987fa85473f961ecb9dbbc59
[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 String routingNode;
34     private final ClusterSingletonServiceProvider clusterSingletonService;
35     private ClusterSingletonServiceRegistration singletonServiceRegistration;
36     private NeutronVppMapper mapper;
37
38     public NeutronVppMapperInstance(final DataBroker dataBroker,
39                                     final String socketPath,
40                                     final String socketPrefix,
41                                     final String routingNode,
42                                     final ClusterSingletonServiceProvider clusterSingletonService) {
43         this.dataBroker = Preconditions.checkNotNull(dataBroker);
44         this.socketPath = Preconditions.checkNotNull(socketPath);
45         this.socketPrefix = Preconditions.checkNotNull(socketPrefix);
46         this.routingNode = Preconditions.checkNotNull(routingNode);
47         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
48     }
49
50     public void initialize() {
51         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
52         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
53     }
54
55     @Override
56     public void instantiateServiceInstance() {
57         LOG.info("Instantiating {}", this.getClass().getSimpleName());
58         mapper = new NeutronVppMapper(socketPath, socketPrefix, routingNode, dataBroker);
59     }
60
61     @Override
62     public ListenableFuture<Void> closeServiceInstance() {
63         LOG.info("Instance {} closed", this.getClass().getSimpleName());
64         mapper.close();
65         return Futures.immediateFuture(null);
66     }
67
68     @Override
69     public void close() throws Exception {
70         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
71         if (singletonServiceRegistration != null) {
72             try {
73                 singletonServiceRegistration.close();
74             } catch (Exception e) {
75                 LOG.warn("{} closed unexpectedly.", this.getClass().getSimpleName(), e.getMessage());
76             }
77             singletonServiceRegistration = null;
78         }
79     }
80
81     @Override
82     public ServiceGroupIdentifier getIdentifier() {
83         return IDENTIFIER;
84     }
85 }