catch and log cluster singleton service registration exceptions
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / controller / config / yang / config / neutron_mapper / impl / NeutronMapperInstance.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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_mapper.impl;
10
11 import java.net.Inet4Address;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14
15 import com.google.common.base.Preconditions;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import org.opendaylight.controller.config.yang.config.groupbasedpolicy.GroupbasedpolicyInstance;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
21 import org.opendaylight.groupbasedpolicy.neutron.mapper.NeutronMapper;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
25 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class NeutronMapperInstance implements ClusterSingletonService, AutoCloseable {
35
36     private static final Logger LOG = LoggerFactory.getLogger(NeutronMapperInstance.class);
37
38     private static final ServiceGroupIdentifier IDENTIFIER =
39             ServiceGroupIdentifier.create(GroupbasedpolicyInstance.GBP_SERVICE_GROUP_IDENTIFIER);
40     private final DataBroker dataBroker;
41     private final ClusterSingletonServiceProvider clusterSingletonService;
42     private final RpcProviderRegistry rpcBroker;
43     private IpPrefix metadataIpPrefix;
44     private int metadataPort;
45     private ClusterSingletonServiceRegistration singletonServiceRegistration;
46     private NeutronMapper mapper;
47
48     public NeutronMapperInstance(final DataBroker dataBroker,
49                                  final RpcProviderRegistry rpcBroker,
50                                  final ClusterSingletonServiceProvider clusterSingletonService,
51                                  final String metadataIp,
52                                  final String metadataPort) {
53         this.dataBroker = Preconditions.checkNotNull(dataBroker);
54         this.rpcBroker = Preconditions.checkNotNull(rpcBroker);
55         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService);
56         try {
57             InetAddress inetAddr = InetAddress.getByName(metadataIp);
58             if (inetAddr instanceof Inet4Address) {
59                 this.metadataIpPrefix = new IpPrefix(new Ipv4Prefix(Preconditions.checkNotNull(metadataIp) + "/32"));
60             } else if (inetAddr instanceof Inet6Address) {
61                 this.metadataIpPrefix = new IpPrefix(new Ipv6Prefix(Preconditions.checkNotNull(metadataIp) + "/128"));
62             }
63             this.metadataPort = Integer.parseInt(Preconditions.checkNotNull(metadataPort));
64             LOG.info("Resolved Metadata CIDR: {} and port {}.", metadataIpPrefix, metadataPort);
65         } catch (Exception ex) {
66             if (ex instanceof NumberFormatException) {
67                 LOG.warn("Metadata port cannot be resolved. Provided value: {}. Continue without support for metadata.",
68                     metadataPort);
69             } else {
70                 LOG.warn("MetadataIP could not be resolved. Provided value: {}. Continue without support for metadata.",
71                     metadataIp);
72             }
73             this.metadataIpPrefix = null;
74         }
75     }
76
77     public void instantiate() {
78         LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
79         try {
80             singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
81         }
82         catch (Exception e) {
83             LOG.warn("Exception while registering candidate ... ", e);
84         }
85     }
86
87     @Override
88     public void instantiateServiceInstance() {
89         LOG.info("Instantiating {}", this.getClass().getSimpleName());
90         final EndpointService epService = rpcBroker.getRpcService(EndpointService.class);
91         final BaseEndpointService baseEndpointService = rpcBroker.getRpcService(BaseEndpointService.class);
92         mapper = new NeutronMapper(dataBroker, epService, baseEndpointService, metadataIpPrefix, metadataPort);
93     }
94
95     @Override
96     public ListenableFuture<Void> closeServiceInstance() {
97         LOG.info("Instance {} closed", this.getClass().getSimpleName());
98         mapper.close();
99         return Futures.immediateFuture(null);
100     }
101
102     @Override
103     public void close() throws Exception {
104         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
105         if (singletonServiceRegistration != null) {
106             try {
107                 singletonServiceRegistration.close();
108             } catch (Exception e) {
109                 LOG.warn("{} closed unexpectedly", e.getMessage());
110             }
111             singletonServiceRegistration = null;
112         }
113     }
114
115     @Override
116     public ServiceGroupIdentifier getIdentifier() {
117         return IDENTIFIER;
118     }
119
120 }