Merge "Repair bgp-rib-impl-config to work with MD5 wiring"
[bgpcep.git] / bgp / rib-impl-config / src / main / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerModule.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: bgp-rib-impl  yang module local name: bgp-peer
12  * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
13  * Generated at: Sat Jan 25 11:00:14 CET 2014
14  *
15  * Do not modify this file unless it is present under src/main directory
16  */
17 package org.opendaylight.controller.config.yang.bgp.rib.impl;
18
19 import com.google.common.collect.Lists;
20 import com.google.common.net.InetAddresses;
21 import java.net.InetSocketAddress;
22 import java.util.List;
23 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
24 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
26 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
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.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.As4BytesCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.as4.bytes._case.As4BytesCapabilityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  *
41  */
42 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule {
43     private static final Logger LOG = LoggerFactory.getLogger(BGPPeerModule.class);
44
45     public BGPPeerModule(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 BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
51             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final BGPPeerModule oldModule,
52             final java.lang.AutoCloseable oldInstance) {
53
54         super(identifier, dependencyResolver, oldModule, oldInstance);
55     }
56
57     @Override
58     protected void customValidation() {
59         JmxAttributeValidationException.checkNotNull(getHost(), "value is not set.", hostJmxAttribute);
60         JmxAttributeValidationException.checkNotNull(getPort(), "value is not set.", portJmxAttribute);
61
62         if (getPassword() != null) {
63             /*
64              *  This is a nasty hack, but we don't have another clean solution. We cannot allow
65              *  password being set if the injected dispatcher does not have the optional
66              *  md5-server-channel-factory set.
67              *
68              *  FIXME: this is a use case for Module interfaces, e.g. RibImplModule
69              *         should something like isMd5ServerSupported()
70              */
71
72             RIBImplModuleMXBean ribProxy = dependencyResolver.newMXBeanProxy(getRib(), RIBImplModuleMXBean.class);
73             BGPDispatcherImplModuleMXBean bgpDispatcherProxy = dependencyResolver.newMXBeanProxy(
74                     ribProxy.getBgpDispatcher(), BGPDispatcherImplModuleMXBean.class);
75             boolean isMd5Supported = bgpDispatcherProxy.getMd5ChannelFactory() != null;
76
77             JmxAttributeValidationException.checkCondition(isMd5Supported,
78                     "Underlying dispatcher does not support MD5 clients", passwordJmxAttribute);
79
80         }
81     }
82
83     private InetSocketAddress createAddress() {
84         final IpAddress ip = getHost();
85         if (ip.getIpv4Address() != null) {
86             return new InetSocketAddress(InetAddresses.forString(ip.getIpv4Address().getValue()), getPort().getValue());
87         } else if (ip.getIpv6Address() != null) {
88             return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
89         } else {
90             throw new IllegalStateException("Failed to handle host " + getHost());
91         }
92     }
93
94     private static String peerName(final IpAddress host) {
95         if (host.getIpv4Address() != null) {
96             return host.getIpv4Address().getValue();
97         }
98         if (host.getIpv6Address() != null) {
99             return host.getIpv6Address().getValue();
100         }
101
102         return null;
103     }
104
105     @Override
106     public java.lang.AutoCloseable createInstance() {
107         final RIB r = getRibDependency();
108
109         final List<BgpParameters> tlvs = Lists.newArrayList();
110         tlvs.add(new BgpParametersBuilder().setCParameters(
111                 new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(r.getLocalAs()).build()).build()).build());
112
113         for (final BgpTableType t : getAdvertizedTableDependency()) {
114             if (!r.getLocalTables().contains(t)) {
115                 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", t);
116             }
117
118             tlvs.add(new BgpParametersBuilder().setCParameters(
119                     new MultiprotocolCaseBuilder().setMultiprotocolCapability(new MultiprotocolCapabilityBuilder(t).build()).build()).build());
120         }
121
122         // Remote AS number defaults to our local AS
123         final AsNumber remoteAs;
124         if (getRemoteAs() != null) {
125             remoteAs = new AsNumber(getRemoteAs());
126         } else {
127             remoteAs = r.getLocalAs();
128         }
129
130         final String password;
131         if (getPassword() != null) {
132             password = getPassword().getValue();
133         } else {
134             password = null;
135         }
136
137         return new BGPPeer(peerName(getHost()), createAddress(), password, new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), tlvs), remoteAs, r);
138     }
139 }