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