c983e04eaa59f964517990c6e905c16577892089
[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.net.InetSocketAddress;
20 import java.util.List;
21
22 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
23 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.As4BytesCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.c.parameters.as4.bytes._case.As4BytesCapabilityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.MultiprotocolCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.collect.Lists;
39 import com.google.common.net.InetAddresses;
40
41 /**
42  *
43  */
44 public final class BGPPeerModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerModule
45 {
46         private static final Logger LOG = LoggerFactory.getLogger(BGPPeerModule.class);
47
48         public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
49                 super(identifier, dependencyResolver);
50         }
51
52         public BGPPeerModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
53                         final BGPPeerModule oldModule, final java.lang.AutoCloseable oldInstance) {
54
55                 super(identifier, dependencyResolver, oldModule, oldInstance);
56         }
57
58         @Override
59         protected void customValidation(){
60                 JmxAttributeValidationException.checkNotNull(getHost(),
61                                 "value is not set.", hostJmxAttribute);
62                 JmxAttributeValidationException.checkNotNull(getPort(),
63                                 "value is not set.", portJmxAttribute);
64         }
65
66         private InetSocketAddress createAddress() {
67                 final IpAddress ip = getHost();
68                 if (ip.getIpv4Address() != null) {
69                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv4Address().getValue()), getPort().getValue());
70                 } else if (ip.getIpv6Address() != null) {
71                         return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
72                 } else {
73                         throw new IllegalStateException("Failed to handle host " + getHost());
74                 }
75         }
76
77         private static String peerName(final IpAddress host) {
78                 if (host.getIpv4Address() != null) {
79                         return host.getIpv4Address().getValue();
80                 }
81                 if (host.getIpv6Address() != null) {
82                         return host.getIpv6Address().getValue();
83                 }
84
85                 return null;
86         }
87
88         @Override
89         public java.lang.AutoCloseable createInstance() {
90                 final RIB r = getRibDependency();
91
92                 final List<BgpParameters> tlvs = Lists.newArrayList();
93                 tlvs.add(new BgpParametersBuilder().setCParameters(
94                                 new As4BytesCaseBuilder().setAs4BytesCapability(new As4BytesCapabilityBuilder().setAsNumber(r.getLocalAs()).build()).build()).build());
95
96                 for (final BgpTableType t : getAdvertizedTableDependency()) {
97                         if (!r.getLocalTables().contains(t)) {
98                                 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", t);
99                         }
100
101                         tlvs.add(new BgpParametersBuilder().setCParameters(
102                                         new MultiprotocolCaseBuilder().setMultiprotocolCapability(
103                                                         new MultiprotocolCapabilityBuilder(t).build()).build()).build());
104                 }
105
106                 // Remote AS number defaults to our local AS
107                 final AsNumber remoteAs;
108                 if (getRemoteAs() != null) {
109                         remoteAs = new AsNumber(getRemoteAs());
110                 } else {
111                         remoteAs = r.getLocalAs();
112                 }
113
114                 return new BGPPeer(peerName(getHost()), createAddress(),
115                                 new BGPSessionPreferences(r.getLocalAs(), getHoldtimer(), r.getBgpIdentifier(), tlvs), remoteAs, r);
116         }
117 }