Bug-5612: ODL(PCEP) infinitely waits for the response from peer for addlsp
[bgpcep.git] / pcep / topology-provider / src / main / java / org / opendaylight / controller / config / yang / pcep / topology / provider / PCEPTopologyProviderModule.java
1 /*
2  * Copyright (c) 2013 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  * Generated file
10
11  * Generated from: yang module name: config-pcep-topology-provider  yang module local name: pcep-topology-impl
12  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
13  * Generated at: Mon Nov 18 21:08:29 CET 2013
14  *
15  * Do not modify this file unless it is present under src/main directory
16  */
17 package org.opendaylight.controller.config.yang.pcep.topology.provider;
18
19 import com.google.common.base.Charsets;
20 import com.google.common.base.Optional;
21 import com.google.common.base.Preconditions;
22 import com.google.common.net.InetAddresses;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.util.List;
26 import java.util.concurrent.ExecutionException;
27 import org.opendaylight.bgpcep.pcep.topology.provider.PCEPTopologyProvider;
28 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
29 import org.opendaylight.controller.config.yang.pcep.impl.PCEPDispatcherImplModuleMXBean;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.tcpmd5.api.KeyMapping;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.tcpmd5.cfg.rev140427.Rfc2385Key;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  *
44  */
45 public final class PCEPTopologyProviderModule extends
46         org.opendaylight.controller.config.yang.pcep.topology.provider.AbstractPCEPTopologyProviderModule {
47     private static final Logger LOG = LoggerFactory.getLogger(PCEPTopologyProviderModule.class);
48
49     private static final String IS_NOT_SET = "is not set.";
50
51     public PCEPTopologyProviderModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
52             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
53         super(identifier, dependencyResolver);
54     }
55
56     public PCEPTopologyProviderModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
57             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final PCEPTopologyProviderModule oldModule,
58             final java.lang.AutoCloseable oldInstance) {
59         super(identifier, dependencyResolver, oldModule, oldInstance);
60     }
61
62     private Optional<KeyMapping> contructKeys() {
63         KeyMapping ret = null;
64         final List<Client> clients = getClient();
65
66         if (clients != null && !clients.isEmpty()) {
67             ret = new KeyMapping();
68             for (final Client c : clients) {
69                 if (c.getAddress() == null) {
70                     LOG.warn("Client {} does not have an address skipping it", c);
71                     continue;
72                 }
73                 final Rfc2385Key rfc2385KeyPassword = c.getPassword();
74                 if (rfc2385KeyPassword != null && !rfc2385KeyPassword.getValue().isEmpty()) {
75                     final String s = getAddressString(c.getAddress());
76                     ret.put(InetAddresses.forString(s), rfc2385KeyPassword.getValue().getBytes(Charsets.US_ASCII));
77                 }
78             }
79         }
80         return Optional.fromNullable(ret);
81     }
82
83
84     private String getAddressString(final IpAddress address) {
85         Preconditions.checkArgument(address.getIpv4Address() != null || address.getIpv6Address() != null, "Address %s is invalid", address);
86         if (address.getIpv4Address() != null) {
87             return address.getIpv4Address().getValue();
88         }
89         return address.getIpv6Address().getValue();
90     }
91
92
93     @Override
94     public void customValidation() {
95         JmxAttributeValidationException.checkNotNull(getTopologyId(), IS_NOT_SET, topologyIdJmxAttribute);
96         JmxAttributeValidationException.checkNotNull(getListenAddress(), IS_NOT_SET, listenAddressJmxAttribute);
97         JmxAttributeValidationException.checkNotNull(getListenPort(), IS_NOT_SET, listenPortJmxAttribute);
98         JmxAttributeValidationException.checkNotNull(getStatefulPlugin(), IS_NOT_SET, statefulPluginJmxAttribute);
99         JmxAttributeValidationException.checkNotNull(getRpcTimeout(), IS_NOT_SET, rpcTimeoutJmxAttribute);
100
101         final Optional<KeyMapping> keys = contructKeys();
102         if (keys.isPresent()) {
103             /*
104              *  This is a nasty hack, but we don't have another clean solution. We cannot allow
105              *  password being set if the injected dispatcher does not have the optional
106              *  md5-server-channel-factory set.
107              *
108              *  FIXME: this is a use case for Module interfaces, e.g. PCEPDispatcherImplModule
109              *         should something like isMd5ServerSupported()
110              */
111
112             final PCEPDispatcherImplModuleMXBean dispatcherProxy = this.dependencyResolver.newMXBeanProxy(getDispatcher(),
113                     PCEPDispatcherImplModuleMXBean.class);
114             final boolean md5ServerSupported = dispatcherProxy.getMd5ServerChannelFactory() != null;
115             JmxAttributeValidationException.checkCondition(md5ServerSupported,
116                     "password is not compatible with selected dispatcher", clientJmxAttribute);
117
118         }
119     }
120
121     private InetAddress listenAddress() {
122         final IpAddress a = getListenAddress();
123         Preconditions.checkArgument(a.getIpv4Address() != null || a.getIpv6Address() != null, "Address %s not supported", a);
124         if (a.getIpv4Address() != null) {
125             return InetAddresses.forString(a.getIpv4Address().getValue());
126         }
127         return InetAddresses.forString(a.getIpv6Address().getValue());
128     }
129
130     @Override
131     public java.lang.AutoCloseable createInstance() {
132         final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
133                 new TopologyKey(getTopologyId())).build();
134         final InetSocketAddress address = new InetSocketAddress(listenAddress(), getListenPort().getValue());
135
136         try {
137             return PCEPTopologyProvider.create(getDispatcherDependency(), address, contructKeys(), getSchedulerDependency(),
138                     getDataProviderDependency(), getRpcRegistryDependency(), topology, getStatefulPluginDependency(),
139                     Optional.of(getRootRuntimeBeanRegistratorWrapper()), getRpcTimeout());
140         } catch (InterruptedException | ExecutionException | TransactionCommitFailedException | ReadFailedException e) {
141             LOG.error("Failed to instantiate topology provider at {}", address, e);
142             throw new IllegalStateException("Failed to instantiate provider", e);
143         }
144     }
145 }