b761246edc3a15312775cacbed0e4c4869321333
[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         singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
80     }
81
82     @Override
83     public void instantiateServiceInstance() {
84         LOG.info("Instantiating {}", this.getClass().getSimpleName());
85         final EndpointService epService = rpcBroker.getRpcService(EndpointService.class);
86         final BaseEndpointService baseEndpointService = rpcBroker.getRpcService(BaseEndpointService.class);
87         mapper = new NeutronMapper(dataBroker, epService, baseEndpointService, metadataIpPrefix, metadataPort);
88     }
89
90     @Override
91     public ListenableFuture<Void> closeServiceInstance() {
92         LOG.info("Instance {} closed", this.getClass().getSimpleName());
93         mapper.close();
94         return Futures.immediateFuture(null);
95     }
96
97     @Override
98     public void close() throws Exception {
99         LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
100         if (singletonServiceRegistration != null) {
101             try {
102                 singletonServiceRegistration.close();
103             } catch (Exception e) {
104                 LOG.warn("{} closed unexpectedly", e.getMessage());
105             }
106             singletonServiceRegistration = null;
107         }
108     }
109
110     @Override
111     public ServiceGroupIdentifier getIdentifier() {
112         return IDENTIFIER;
113     }
114
115 }