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