2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.openconfig.impl.moduleconfig;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.protocol.bgp.openconfig.impl.spi.BGPConfigHolder;
18 import org.opendaylight.protocol.bgp.openconfig.impl.spi.BGPConfigStateStore;
19 import org.opendaylight.protocol.bgp.openconfig.impl.util.GlobalIdentifier;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.RibInstance;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.modules.module.configuration.BgpApplicationPeer;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.modules.module.configuration.BgpApplicationPeerBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.modules.module.configuration.bgp.application.peer.TargetRib;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.modules.module.configuration.bgp.application.peer.TargetRibBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleKey;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 final class BGPAppPeerProvider {
37 private static final String APPLICATION_RIB = "application-rib_";
39 private static final String APPLICATION_PEER = "application-peer_";
41 private static final Logger LOG = LoggerFactory.getLogger(BGPAppPeerProvider.class);
43 private static final Function<String, TargetRib> TO_RIB_FUNCTION = new Function<String, TargetRib>() {
45 public TargetRib apply(final String name) {
46 return new TargetRibBuilder().setName(name).setType(RibInstance.class).build();
50 private final BGPConfigHolder<Neighbor> neighborState;
51 private final BGPConfigHolder<Bgp> globalState;
52 private final BGPConfigModuleProvider configModuleOp;
53 private final DataBroker dataBroker;
55 public BGPAppPeerProvider(final BGPConfigStateStore configHolders, final BGPConfigModuleProvider configModuleWriter, final DataBroker dataBroker) {
56 this.dataBroker = dataBroker;
57 this.configModuleOp = Preconditions.checkNotNull(configModuleWriter);
58 this.globalState = Preconditions.checkNotNull(configHolders.getBGPConfigHolder(Bgp.class));
59 this.neighborState = Preconditions.checkNotNull(configHolders.getBGPConfigHolder(Neighbor.class));
62 public void onNeighborRemoved(final Neighbor removedNeighbor) {
63 final ModuleKey moduleKey = neighborState.getModuleKey(removedNeighbor.getKey());
64 if (moduleKey != null) {
66 final ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
67 final Optional<Module> maybeModule = configModuleOp.readModuleConfiguration(moduleKey, rwTx);
68 if (maybeModule.isPresent() && neighborState.remove(moduleKey, removedNeighbor)) {
69 configModuleOp.removeModuleConfiguration(moduleKey, rwTx);
71 } catch (final Exception e) {
72 LOG.error("Failed to remove a configuration module: {}", moduleKey, e);
73 throw new IllegalStateException(e);
78 public void onNeighborModified(final Neighbor modifiedAppNeighbor) {
79 final ModuleKey moduleKey = neighborState.getModuleKey(modifiedAppNeighbor.getKey());
80 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
81 if (moduleKey != null) {
82 //update an existing peer configuration
84 if (neighborState.addOrUpdate(moduleKey, modifiedAppNeighbor.getKey(), modifiedAppNeighbor)) {
85 final Optional<Module> maybeModule = configModuleOp.readModuleConfiguration(moduleKey, rTx);
86 if (maybeModule.isPresent()) {
87 final Module peerConfigModule = toPeerConfigModule(modifiedAppNeighbor, maybeModule.get());
88 configModuleOp.putModuleConfiguration(peerConfigModule, dataBroker.newWriteOnlyTransaction());
91 } catch (final Exception e) {
92 LOG.error("Failed to update a configuration module: {}", moduleKey, e);
93 throw new IllegalStateException(e);
96 //create new application peer configuration
97 final ModuleKey ribImplKey = globalState.getModuleKey(GlobalIdentifier.GLOBAL_IDENTIFIER);
98 if (ribImplKey != null) {
100 final TargetRib rib = RibInstanceFunction.getRibInstance(this.configModuleOp, this.TO_RIB_FUNCTION, ribImplKey.getName(), rTx);
101 final Module peerConfigModule = toPeerConfigModule(modifiedAppNeighbor, rib);
102 configModuleOp.putModuleConfiguration(peerConfigModule, dataBroker.newWriteOnlyTransaction());
103 neighborState.addOrUpdate(peerConfigModule.getKey(), modifiedAppNeighbor.getKey(), modifiedAppNeighbor);
104 } catch (final Exception e) {
105 LOG.error("Failed to create a configuration module: {}", moduleKey, e);
106 throw new IllegalStateException(e);
112 private static Module toPeerConfigModule(final Neighbor neighbor, final Module currentModule) {
113 final BgpApplicationPeer appPeerConfig = (BgpApplicationPeer) currentModule.getConfiguration();
114 final BgpApplicationPeerBuilder bgpPeerConfigBuilder = toBgpPeerConfig(neighbor, appPeerConfig.getTargetRib());
115 bgpPeerConfigBuilder.setApplicationRibId(appPeerConfig.getApplicationRibId());
116 final ModuleBuilder mBuilder = new ModuleBuilder();
117 mBuilder.setConfiguration(bgpPeerConfigBuilder.build());
118 return mBuilder.build();
121 private static Module toPeerConfigModule(final Neighbor neighbor, final TargetRib rib) {
122 final ModuleBuilder mBuilder = new ModuleBuilder();
123 mBuilder.setName(createAppPeerName(neighbor.getNeighborAddress().getIpv4Address()));
124 mBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.BgpApplicationPeer.class);
125 mBuilder.setConfiguration(toBgpPeerConfig(neighbor, rib).build());
126 mBuilder.setKey(new ModuleKey(mBuilder.getName(), mBuilder.getType()));
127 return mBuilder.build();
130 private static BgpApplicationPeerBuilder toBgpPeerConfig(final Neighbor neighbor, final TargetRib rib) {
131 final BgpApplicationPeerBuilder bgpAppPeerBuilder = new BgpApplicationPeerBuilder();
132 bgpAppPeerBuilder.setTargetRib(rib);
133 final Ipv4Address address = neighbor.getNeighborAddress().getIpv4Address();
134 bgpAppPeerBuilder.setBgpPeerId(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address(address.getValue()));
135 bgpAppPeerBuilder.setApplicationRibId(new ApplicationRibId(createAppRibName(address)));
136 return bgpAppPeerBuilder;
139 private static String createAppPeerName(final Ipv4Address ipAddress) {
140 return APPLICATION_PEER + ipAddress.getValue();
143 private static String createAppRibName(final Ipv4Address ipAddress) {
144 return APPLICATION_RIB + ipAddress.getValue();