544ece9b460311d27535387187782e8d2fbbe60b
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / controller / config / yang / config / faas_provider / impl / FaasProviderInstance.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.faas_provider.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.api.EpRendererAugmentationRegistry;
17 import org.opendaylight.groupbasedpolicy.renderer.faas.FaasRenderer;
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.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class FaasProviderInstance implements ClusterSingletonService, AutoCloseable {
26
27     private static final Logger LOG = LoggerFactory.getLogger(FaasProviderInstance.class);
28
29     private static final ServiceGroupIdentifier IDENTIFIER =
30             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
31     private final DataBroker dataBroker;
32     private final EpRendererAugmentationRegistry epRegistry;
33     private final ClusterSingletonServiceProvider clusterSingletonService;
34     private ClusterSingletonServiceRegistration singletonServiceRegistration;
35     private FaasRenderer renderer;
36
37     public FaasProviderInstance(final DataBroker dataBroker,
38                                 final EpRendererAugmentationRegistry epRegistry,
39                                 final ClusterSingletonServiceProvider clusteringServiceProvider) {
40         this.dataBroker = Preconditions.checkNotNull(dataBroker);
41         this.epRegistry = Preconditions.checkNotNull(epRegistry);
42         this.clusterSingletonService = Preconditions.checkNotNull(clusteringServiceProvider);
43     }
44
45     public void initialize() {
46         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
47         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
48     }
49
50     @Override
51     public void instantiateServiceInstance() {
52         LOG.info("Instantiating {}", this.getClass().getSimpleName());
53         renderer = new FaasRenderer(dataBroker, epRegistry);
54     }
55
56     @Override
57     public ListenableFuture<Void> closeServiceInstance() {
58         LOG.info("Instance {} closed", this.getClass().getSimpleName());
59         try {
60             renderer.close();
61         } catch (Exception e) {
62             LOG.warn("Exception while closing ... ", e);
63         }
64         return Futures.immediateFuture(null);
65     }
66
67     @Override
68     public void close() throws Exception {
69         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
70         if (singletonServiceRegistration != null) {
71             try {
72                 singletonServiceRegistration.close();
73             } catch (Exception e) {
74                 LOG.warn("{} closed unexpectedly", this.getClass().getSimpleName(), e.getMessage());
75             }
76             singletonServiceRegistration = null;
77         }
78     }
79
80     @Override
81     public ServiceGroupIdentifier getIdentifier() {
82         return IDENTIFIER;
83     }
84 }