Convert PCEPDispatcher wiring to blueprint
[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.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.net.InetAddresses;
22 import io.netty.channel.epoll.Epoll;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.nio.charset.StandardCharsets;
26 import java.util.List;
27 import java.util.concurrent.ExecutionException;
28 import org.opendaylight.bgpcep.pcep.topology.provider.PCEPTopologyProvider;
29 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
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.protocol.concepts.KeyMapping;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.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 = KeyMapping.getKeyMapping();
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(StandardCharsets.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             JmxAttributeValidationException.checkCondition(Epoll.isAvailable(),
104                     "client is configured with password but native transport is not available", clientJmxAttribute);
105         }
106     }
107
108     private InetAddress listenAddress() {
109         final IpAddress a = getListenAddress();
110         Preconditions.checkArgument(a.getIpv4Address() != null || a.getIpv6Address() != null, "Address %s not supported", a);
111         if (a.getIpv4Address() != null) {
112             return InetAddresses.forString(a.getIpv4Address().getValue());
113         }
114         return InetAddresses.forString(a.getIpv6Address().getValue());
115     }
116
117     @Override
118     public java.lang.AutoCloseable createInstance() {
119         final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
120                 new TopologyKey(getTopologyId())).build();
121         final InetSocketAddress address = new InetSocketAddress(listenAddress(), getListenPort().getValue());
122
123         try {
124             return PCEPTopologyProvider.create(getDispatcherDependency(), address, contructKeys(), getSchedulerDependency(),
125                     getDataProviderDependency(), getRpcRegistryDependency(), topology, getStatefulPluginDependency(),
126                     Optional.of(getRootRuntimeBeanRegistratorWrapper()), getRpcTimeout());
127         } catch (InterruptedException | ExecutionException | TransactionCommitFailedException | ReadFailedException e) {
128             LOG.error("Failed to instantiate topology provider at {}", address, e);
129             throw new IllegalStateException("Failed to instantiate provider", e);
130         }
131     }
132 }