BUG-4197: BGP OpenConfig Implementation
[bgpcep.git] / bgp / openconfig-impl / src / main / java / org / opendaylight / protocol / bgp / openconfig / impl / moduleconfig / BGPAppPeerProvider.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.bgp.openconfig.impl.moduleconfig;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
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.rev150515.bgp.Global;
21 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev150515.bgp.neighbors.Neighbor;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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;
34
35 final class BGPAppPeerProvider {
36
37     private static final String APPLICATION_RIB = "application-rib_";
38
39     private static final String APPLICATION_PEER = "application-peer_";
40
41     private static final Logger LOG = LoggerFactory.getLogger(BGPAppPeerProvider.class);
42
43     private static final Function<String, TargetRib> TO_RIB_FUNCTION = new Function<String, TargetRib>() {
44         @Override
45         public TargetRib apply(final String name) {
46             return new TargetRibBuilder().setName(name).setType(RibInstance.class).build();
47         }
48     };
49
50     private final BGPConfigHolder<Neighbor> neighborState;
51     private final BGPConfigHolder<Global> globalState;
52     private final BGPConfigModuleProvider configModuleOp;
53
54     public BGPAppPeerProvider(final BGPConfigStateStore configHolders, final BGPConfigModuleProvider configModuleWriter) {
55         this.configModuleOp = Preconditions.checkNotNull(configModuleWriter);
56         this.globalState = Preconditions.checkNotNull(configHolders.getBGPConfigHolder(Global.class));
57         this.neighborState = Preconditions.checkNotNull(configHolders.getBGPConfigHolder(Neighbor.class));
58     }
59
60     public void onNeighborRemoved(final Neighbor removedNeighbor, final DataBroker dataBroker) {
61         final ModuleKey moduleKey = neighborState.getModuleKey(removedNeighbor.getKey());
62         if (moduleKey != null) {
63             try {
64                 globalState.remove(moduleKey);
65                 final Optional<Module> maybeModule = configModuleOp.readModuleConfiguration(moduleKey, dataBroker.newReadOnlyTransaction()).get();
66                 if (maybeModule.isPresent()) {
67                     configModuleOp.removeModuleConfiguration(moduleKey, dataBroker.newWriteOnlyTransaction());
68                 }
69             } catch (final Exception e) {
70                 LOG.error("Failed to remove a configuration module: {}", moduleKey, e);
71                 throw new IllegalStateException(e);
72             }
73         }
74     }
75
76     public void onNeighborModified(final Neighbor modifiedAppNeighbor, final DataBroker dataBroker) {
77         final ModuleKey moduleKey = neighborState.getModuleKey(modifiedAppNeighbor.getKey());
78         final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
79         if (moduleKey != null) {
80             //update an existing peer configuration
81             try {
82                 if (neighborState.addOrUpdate(moduleKey, modifiedAppNeighbor.getKey(), modifiedAppNeighbor)) {
83                     final Optional<Module> maybeModule = configModuleOp.readModuleConfiguration(moduleKey, rTx).get();
84                     if (maybeModule.isPresent()) {
85                         final Module peerConfigModule = toPeerConfigModule(modifiedAppNeighbor, maybeModule.get());
86                         configModuleOp.putModuleConfiguration(peerConfigModule, dataBroker.newWriteOnlyTransaction());
87                     }
88                 }
89             } catch (final Exception e) {
90                 LOG.error("Failed to update a configuration module: {}", moduleKey, e);
91                 throw new IllegalStateException(e);
92             }
93         } else {
94             //create new application peer configuration
95             final ModuleKey ribImplKey = globalState.getModuleKey(GlobalIdentifier.GLOBAL_IDENTIFIER);
96             if (ribImplKey != null) {
97                 try {
98                     final ListenableFuture<TargetRib> ribFuture = new RibInstanceFunction<>(rTx, configModuleOp, TO_RIB_FUNCTION).apply(ribImplKey.getName());
99                     final Module peerConfigModule = toPeerConfigModule(modifiedAppNeighbor, ribFuture.get());
100                     configModuleOp.putModuleConfiguration(peerConfigModule, dataBroker.newWriteOnlyTransaction());
101                     neighborState.addOrUpdate(peerConfigModule.getKey(), modifiedAppNeighbor.getKey(), modifiedAppNeighbor);
102                 } catch (final Exception e) {
103                     LOG.error("Failed to create a configuration module: {}", moduleKey, e);
104                     throw new IllegalStateException(e);
105                 }
106             }
107         }
108     }
109
110     private static Module toPeerConfigModule(final Neighbor neighbor, final Module currentModule) {
111         final BgpApplicationPeer appPeerConfig = (BgpApplicationPeer) currentModule.getConfiguration();
112         final BgpApplicationPeerBuilder bgpPeerConfigBuilder = toBgpPeerConfig(neighbor, appPeerConfig.getTargetRib());
113         bgpPeerConfigBuilder.setApplicationRibId(appPeerConfig.getApplicationRibId());
114         final ModuleBuilder mBuilder = new ModuleBuilder();
115         mBuilder.setConfiguration(bgpPeerConfigBuilder.build());
116         return mBuilder.build();
117     }
118
119     private static Module toPeerConfigModule(final Neighbor neighbor, final TargetRib rib) {
120         final ModuleBuilder mBuilder = new ModuleBuilder();
121         mBuilder.setName(createAppPeerName(neighbor.getNeighborAddress().getIpv4Address()));
122         mBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev130409.BgpApplicationPeer.class);
123         mBuilder.setConfiguration(toBgpPeerConfig(neighbor, rib).build());
124         mBuilder.setKey(new ModuleKey(mBuilder.getName(), mBuilder.getType()));
125         return mBuilder.build();
126     }
127
128     private static BgpApplicationPeerBuilder toBgpPeerConfig(final Neighbor neighbor, final TargetRib rib) {
129         final BgpApplicationPeerBuilder bgpAppPeerBuilder = new BgpApplicationPeerBuilder();
130         bgpAppPeerBuilder.setTargetRib(rib);
131         final Ipv4Address address = neighbor.getNeighborAddress().getIpv4Address();
132         bgpAppPeerBuilder.setBgpPeerId(address);
133         bgpAppPeerBuilder.setApplicationRibId(new ApplicationRibId(createAppRibName(address)));
134         return bgpAppPeerBuilder;
135     }
136
137     private static String createAppPeerName(final Ipv4Address ipAddress) {
138         return APPLICATION_PEER + ipAddress.getValue();
139     }
140
141     private static String createAppRibName(final Ipv4Address ipAddress) {
142         return APPLICATION_RIB + ipAddress.getValue();
143     }
144 }