BUG-731: JMX attributes are static now
[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                                 final ObjectName ribi = (ObjectName) srv.getAttribute(getRib(), "CurrentImplementation");
85
86                                 // FIXME: AbstractRIBImplModule.bgpDispatcherJmxAttribute.getAttributeName()
87                                 final ObjectName disp = (ObjectName) srv.getAttribute(ribi, "BgpDispatcher");
88
89                                 final ObjectName dispi = (ObjectName) srv.getAttribute(disp, "CurrentImplementation");
90
91                                 // FIXME: AbstractBGPDispatcherImplModule.md5ChannelFactoryJmxAttribute.getAttributeName()
92                                 final Object cf = srv.getAttribute(dispi, "Md5ChannelFactory");
93                                 JmxAttributeValidationException.checkCondition(cf != null, "Underlying dispatcher does not support MD5 clients", passwordJmxAttribute);
94                         } catch (AttributeNotFoundException | InstanceNotFoundException
95                                         | MBeanException | ReflectionException e) {
96                                 JmxAttributeValidationException.wrap(e, "support could not be validated", passwordJmxAttribute);
97                         }
98                 }
99         }
100
101         private InetSocketAddress createAddress() {
102                 final IpAddress ip = getHost();
103                 if (ip.getIpv4Address() != null) {
104                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv4Address().getValue()), getPort().getValue());
105                 } else if (ip.getIpv6Address() != null) {
106                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
107                 } else {
108                         throw new IllegalStateException("Failed to handle host " + getHost());
109                 }
110         }
111
112         private static String peerName(final IpAddress host) {
113                 if (host.getIpv4Address() != null) {
114                         return host.getIpv4Address().getValue();
115                 }
116                 if (host.getIpv6Address() != null) {
117                         return host.getIpv6Address().getValue();
118                 }
119
120                 return null;
121         }
122
123         @Override
124         public java.lang.AutoCloseable createInstance() {
125                 final RIB r = getRibDependency();
126
127                 final List<BgpParameters> tlvs = Lists.newArrayList();
128                 tlvs.add(new BgpParametersBuilder().setCParameters(
129                                 new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(r.getLocalAs()).build()).build()).build());
130
131                 for (final BgpTableType t : getAdvertizedTableDependency()) {
132                         if (!r.getLocalTables().contains(t)) {
133                                 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", t);
134                         }
135
136                         tlvs.add(new BgpParametersBuilder().setCParameters(
137                                         new MultiprotocolCaseBuilder().setMultiprotocolCapability(
138                                                         new MultiprotocolCapabilityBuilder(t).build()).build()).build());
139                 }
140
141                 // Remote AS number defaults to our local AS
142                 final AsNumber remoteAs;
143                 if (getRemoteAs() != null) {
144                         remoteAs = new AsNumber(getRemoteAs());
145                 } else {
146                         remoteAs = r.getLocalAs();
147                 }
148
149                 final String password;
150                 if (getPassword() != null) {
151                         password = getPassword().getValue();
152                 } else {
153                         password = null;
154                 }
155
156                 return new BGPPeer(peerName(getHost()), createAddress(), password,
157                                 new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), tlvs), remoteAs, r);
158         }
159 }