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