2 * Copyright (c) 2016 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
8 package org.opendaylight.protocol.bgp.rib.impl.config;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
14 import com.google.common.base.Strings;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.Objects;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker.DataTreeChangeExtension;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
22 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
23 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
25 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
26 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateProvider;
27 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateProviderRegistry;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRib;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
35 import org.opendaylight.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 final class AppPeer extends PeerBean {
44 private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
45 private static final NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
46 private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
48 private final BGPStateProviderRegistry stateProviderRegistry;
50 private Neighbor currentConfiguration;
52 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
54 private Registration stateProviderRegistration;
56 AppPeer(final BGPStateProviderRegistry stateProviderRegistry) {
57 this.stateProviderRegistry = requireNonNull(stateProviderRegistry);
60 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
61 final Config config = neighbor.getConfig();
62 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
63 return new ApplicationRibId(config.getDescription());
65 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
69 synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
70 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
71 checkState(bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
72 LOG.info("Starting AppPeer instance {}", neighbor.getNeighborAddress());
73 currentConfiguration = neighbor;
74 bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
75 IetfInetUtil.ipv4AddressNoZoneFor(neighbor.getNeighborAddress().getIpv4Address()), tableTypeRegistry);
76 stateProviderRegistration = stateProviderRegistry.register(this);
80 synchronized ListenableFuture<?> stop() {
81 if (bgpAppPeerSingletonService == null) {
82 LOG.info("App Peer {} already closed, skipping", currentConfiguration.getNeighborAddress());
83 return Futures.immediateVoidFuture();
86 LOG.info("Closing App Peer {}", currentConfiguration.getNeighborAddress());
87 if (stateProviderRegistration != null) {
88 stateProviderRegistration.close();
89 stateProviderRegistration = null;
92 final var future = bgpAppPeerSingletonService.closeServiceInstance();
93 bgpAppPeerSingletonService = null;
98 synchronized void instantiateServiceInstance() {
99 if (bgpAppPeerSingletonService != null) {
100 bgpAppPeerSingletonService.instantiateServiceInstance();
105 synchronized ListenableFuture<?> closeServiceInstance() {
106 return bgpAppPeerSingletonService != null ? bgpAppPeerSingletonService.closeServiceInstance()
107 : Futures.immediateVoidFuture();
111 synchronized boolean containsEqualConfiguration(final Neighbor neighbor) {
112 return Objects.equals(currentConfiguration.key(), neighbor.key())
113 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
117 synchronized Neighbor getCurrentConfiguration() {
118 return currentConfiguration;
122 public synchronized BGPPeerState getPeerState() {
123 return bgpAppPeerSingletonService.getPeerState();
126 private static final class BgpAppPeerSingletonService implements BGPPeerStateProvider {
127 private final ApplicationPeer applicationPeer;
128 private final DataTreeChangeExtension dataTreeChangeService;
129 private final ApplicationRibId appRibId;
131 private boolean isServiceInstantiated;
133 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId,
134 final Ipv4AddressNoZone neighborAddress, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
135 applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
136 this.appRibId = appRibId;
137 dataTreeChangeService = rib.getService();
140 synchronized void instantiateServiceInstance() {
141 isServiceInstantiated = true;
142 applicationPeer.instantiateServiceInstance(dataTreeChangeService,
143 DOMDataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.builder()
145 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, appRibId.getValue())
151 synchronized ListenableFuture<?> closeServiceInstance() {
152 if (!isServiceInstantiated) {
153 LOG.info("Application peer already closed {}", appRibId.getValue());
154 return Futures.immediateVoidFuture();
156 LOG.info("Application peer instance closed {}", appRibId.getValue());
157 isServiceInstantiated = false;
158 return applicationPeer.close();
162 public BGPPeerState getPeerState() {
163 return applicationPeer.getPeerState();