BUG-635: implement MD5 auth option for BGP peers
[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 java.lang.management.ManagementFactory;
20 import java.net.InetSocketAddress;
21 import java.util.List;
22
23 import javax.management.AttributeNotFoundException;
24 import javax.management.InstanceNotFoundException;
25 import javax.management.MBeanException;
26 import javax.management.MBeanServer;
27 import javax.management.ObjectName;
28 import javax.management.ReflectionException;
29
30 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
31 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.As4BytesCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.as4.bytes._case.As4BytesCapabilityBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.common.collect.Lists;
47 import com.google.common.net.InetAddresses;
48
49 /**
50  *
51  */
52 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule
53 {
54         private static final Logger LOG = LoggerFactory.getLogger(BGPPeerModule.class);
55
56         public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
57                 super(identifier, dependencyResolver);
58         }
59
60         public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
61                         final BGPPeerModule oldModule, final java.lang.AutoCloseable oldInstance) {
62
63                 super(identifier, dependencyResolver, oldModule, oldInstance);
64         }
65
66         @Override
67         protected void customValidation(){
68                 JmxAttributeValidationException.checkNotNull(getHost(),
69                                 "value is not set.", hostJmxAttribute);
70                 JmxAttributeValidationException.checkNotNull(getPort(),
71                                 "value is not set.", portJmxAttribute);
72
73                 if (getPassword() != null) {
74                         /*
75                          *  This is a nasty hack, but we don't have another clean solution. We cannot allow
76                          *  password being set if the injected dispatcher does not have the optional
77                          *  md5-server-channel-factory set.
78                          *
79                          *  FIXME: this is a use case for Module interfaces, e.g. RibImplModule
80                          *         should something like isMd5ServerSupported()
81                          */
82                         final MBeanServer srv = ManagementFactory.getPlatformMBeanServer();
83                         try {
84                                 // FIXME: AbstractRIBImplModule.bgpDispatcherJmxAttribute.getAttributeName()
85                                 final ObjectName disp = (ObjectName) srv.getAttribute(getRib(), "BgpDispatcher");
86
87                                 // FIXME: AbstractBGPDispatcherImplModule.md5ChannelFactoryJmxAttribute.getAttributeName()
88                                 final Object cf = srv.getAttribute(disp, "Md5ChannelFactory");
89                                 JmxAttributeValidationException.checkCondition(cf != null, "Underlying dispatcher does not support MD5 clients", this.passwordJmxAttribute);
90                         } catch (AttributeNotFoundException | InstanceNotFoundException
91                                         | MBeanException | ReflectionException e) {
92                                 JmxAttributeValidationException.wrap(e, passwordJmxAttribute);
93                         }
94                 }
95         }
96
97         private InetSocketAddress createAddress() {
98                 final IpAddress ip = getHost();
99                 if (ip.getIpv4Address() != null) {
100                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv4Address().getValue()), getPort().getValue());
101                 } else if (ip.getIpv6Address() != null) {
102                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
103                 } else {
104                         throw new IllegalStateException("Failed to handle host " + getHost());
105                 }
106         }
107
108         private static String peerName(final IpAddress host) {
109                 if (host.getIpv4Address() != null) {
110                         return host.getIpv4Address().getValue();
111                 }
112                 if (host.getIpv6Address() != null) {
113                         return host.getIpv6Address().getValue();
114                 }
115
116                 return null;
117         }
118
119         @Override
120         public java.lang.AutoCloseable createInstance() {
121                 final RIB r = getRibDependency();
122
123                 final List<BgpParameters> tlvs = Lists.newArrayList();
124                 tlvs.add(new BgpParametersBuilder().setCParameters(
125                                 new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(r.getLocalAs()).build()).build()).build());
126
127                 for (final BgpTableType t : getAdvertizedTableDependency()) {
128                         if (!r.getLocalTables().contains(t)) {
129                                 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", t);
130                         }
131
132                         tlvs.add(new BgpParametersBuilder().setCParameters(
133                                         new MultiprotocolCaseBuilder().setMultiprotocolCapability(
134                                                         new MultiprotocolCapabilityBuilder(t).build()).build()).build());
135                 }
136
137                 // Remote AS number defaults to our local AS
138                 final AsNumber remoteAs;
139                 if (getRemoteAs() != null) {
140                         remoteAs = new AsNumber(getRemoteAs());
141                 } else {
142                         remoteAs = r.getLocalAs();
143                 }
144
145                 final String password;
146                 if (getPassword() != null) {
147                         password = getPassword().getValue();
148                 } else {
149                         password = null;
150                 }
151
152                 return new BGPPeer(peerName(getHost()), createAddress(), password,
153                                 new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), tlvs), remoteAs, r);
154         }
155 }