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 org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.util.concurrent.FluentFuture;
15 import java.util.Objects;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
22 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
24 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
25 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRib;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
40 private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
41 private static final NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
42 private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
44 private Neighbor currentConfiguration;
46 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
48 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
49 final Config config = neighbor.getConfig();
50 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
51 return new ApplicationRibId(config.getDescription());
53 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
57 public synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
58 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
59 Preconditions.checkState(this.bgpAppPeerSingletonService == null,
60 "Previous peer instance was not closed.");
61 this.currentConfiguration = neighbor;
62 this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
63 neighbor.getNeighborAddress().getIpv4Address(), tableTypeRegistry);
67 public synchronized void restart(final RIB rib, final InstanceIdentifier<Bgp> bgpIid,
68 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
69 Preconditions.checkState(this.currentConfiguration != null);
70 start(rib, this.currentConfiguration, bgpIid, peerGroupLoader, tableTypeRegistry);
74 public synchronized void close() {
75 if (this.bgpAppPeerSingletonService != null) {
76 this.bgpAppPeerSingletonService = null;
81 public synchronized void instantiateServiceInstance() {
82 if (this.bgpAppPeerSingletonService != null) {
83 this.bgpAppPeerSingletonService.instantiateServiceInstance();
88 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
89 if (this.bgpAppPeerSingletonService != null) {
90 return this.bgpAppPeerSingletonService.closeServiceInstance();
93 return CommitInfo.emptyFluentFuture();
97 public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
98 return Objects.equals(this.currentConfiguration.key(), neighbor.key())
99 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
103 public synchronized BGPPeerState getPeerState() {
104 return this.bgpAppPeerSingletonService.getPeerState();
107 private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
108 private final ApplicationPeer applicationPeer;
109 private final DOMDataTreeChangeService dataTreeChangeService;
110 private final ApplicationRibId appRibId;
112 private boolean isServiceInstantiated;
114 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
115 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
116 this.applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
117 this.appRibId = appRibId;
118 this.dataTreeChangeService = rib.getService();
121 public synchronized void instantiateServiceInstance() {
122 this.isServiceInstantiated = true;
123 final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(APPRIB)
124 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
125 .node(TABLES_NID).node(TABLES_NID).build();
126 this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
127 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
130 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
131 if (!this.isServiceInstantiated) {
132 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
133 return CommitInfo.emptyFluentFuture();
135 LOG.info("Application peer instance closed {}", this.appRibId.getValue());
136 this.isServiceInstantiated = false;
137 return this.applicationPeer.close();
141 public BGPPeerState getPeerState() {
142 return this.applicationPeer.getPeerState();