2 * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.controller.config.yang.config.neutron_mapper.impl;
11 import java.util.regex.Pattern;
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;
31 public class NeutronMapperInstance implements ClusterSingletonService, AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(NeutronMapperInstance.class);
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;
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);
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) {
59 if (ex instanceof NumberFormatException) {
60 LOG.warn("Metadata port cannot be resolved. Provided value: {}. Continue without support for metadata.",
63 LOG.warn("MetadataIP could not be resolved. Provided value: {}. Continue without support for metadata.",
66 this.metadataIpPrefix = null;
71 public void instantiate() {
72 LOG.info("Clustering session initiated for {}", this.getClass().getSimpleName());
73 singletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
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);
85 public ListenableFuture<Void> closeServiceInstance() {
86 LOG.info("Instance {} closed", this.getClass().getSimpleName());
88 return Futures.immediateFuture(null);
92 public void close() throws Exception {
93 LOG.info("Clustering provider closed for {}", this.getClass().getSimpleName());
94 if (singletonServiceRegistration != null) {
96 singletonServiceRegistration.close();
97 } catch (Exception e) {
98 LOG.warn("{} closed unexpectedly", e.getMessage());
100 singletonServiceRegistration = null;
105 public ServiceGroupIdentifier getIdentifier() {