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