catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / controller / config / yang / config / ofoverlay_provider / impl / OFOverlayProviderInstance.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.ofoverlay_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.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
18 import org.opendaylight.groupbasedpolicy.api.PolicyValidatorRegistry;
19 import org.opendaylight.groupbasedpolicy.api.StatisticsManager;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OFOverlayRenderer;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
24 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class OFOverlayProviderInstance implements ClusterSingletonService, AutoCloseable {
31
32     private static final Logger LOG = LoggerFactory.getLogger(OFOverlayProviderInstance.class);
33
34     private static final ServiceGroupIdentifier IDENTIFIER =
35             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
36     private final DataBroker dataBroker;
37     private final PacketProcessingService packetProcessingService;
38     private final SalFlowService flowService;
39     private final NotificationService notificationService;
40     private final EpRendererAugmentationRegistry epRendererAugmentationRegistry;
41     private final PolicyValidatorRegistry policyValidatorRegistry;
42     private final StatisticsManager statisticsManager;
43     private final short tableOffset;
44     private final ClusterSingletonServiceProvider clusterSingletonService;
45     private ClusterSingletonServiceRegistration singletonServiceRegistration;
46     private OFOverlayRenderer renderer;
47
48     public OFOverlayProviderInstance(final DataBroker dataBroker,
49                                      final PacketProcessingService packetProcessingService,
50                                      final SalFlowService flowService,
51                                      final NotificationService notificationService,
52                                      final EpRendererAugmentationRegistry epRendererAugmentationRegistry,
53                                      final PolicyValidatorRegistry policyValidatorRegistry,
54                                      final StatisticsManager statisticsManager,
55                                      final short tableOffset,
56                                      final ClusterSingletonServiceProvider clusterSingletonService) {
57         this.dataBroker = Preconditions.checkNotNull(dataBroker);
58         this.packetProcessingService = Preconditions.checkNotNull(packetProcessingService);
59         this.flowService = Preconditions.checkNotNull(flowService);
60         this.notificationService = Preconditions.checkNotNull(notificationService);
61         this.epRendererAugmentationRegistry = Preconditions.checkNotNull(epRendererAugmentationRegistry);
62         this.policyValidatorRegistry = Preconditions.checkNotNull(policyValidatorRegistry);
63         this.statisticsManager = Preconditions.checkNotNull(statisticsManager);
64         this.tableOffset = Preconditions.checkNotNull(tableOffset);
65         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
66     }
67
68     public void initialize() {
69         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
70         try {
71             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
72         } catch (Exception e) {
73             LOG.warn("Exception thrown while registering cluster singleton service in {}", this.getClass(), e.getMessage());
74         }
75     }
76
77     @Override
78     public void instantiateServiceInstance() {
79         LOG.info("Instantiating {}", this.getClass().getSimpleName());
80         renderer = new OFOverlayRenderer(dataBroker, packetProcessingService, flowService, notificationService,
81                 epRendererAugmentationRegistry, policyValidatorRegistry, statisticsManager, tableOffset);
82     }
83
84     @Override
85     public ListenableFuture<Void> closeServiceInstance() {
86         LOG.info("Instance {} closed", this.getClass().getSimpleName());
87         try {
88             renderer.close();
89         } catch (Exception e) {
90             LOG.warn("Exception thrown when closing ... {}", e.getMessage());
91         }
92         return Futures.immediateFuture(null);
93     }
94
95     @Override
96     public void close() {
97         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
98         if (singletonServiceRegistration != null) {
99             try {
100                 singletonServiceRegistration.close();
101             } catch (Exception e) {
102                 LOG.warn("{} closed unexpectedly",this.getClass().getSimpleName(), e.getMessage());
103             }
104             singletonServiceRegistration = null;
105         }
106     }
107
108     @Override
109     public ServiceGroupIdentifier getIdentifier() {
110         return IDENTIFIER;
111     }
112 }