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.IetfInetUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRib;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.osgi.framework.ServiceRegistration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
42 private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
43 private static final NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
44 private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
46 private Neighbor currentConfiguration;
48 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
50 private ServiceRegistration<?> serviceRegistration;
52 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
53 final Config config = neighbor.getConfig();
54 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
55 return new ApplicationRibId(config.getDescription());
57 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
61 public synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
62 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
63 Preconditions.checkState(this.bgpAppPeerSingletonService == null,
64 "Previous peer instance was not closed.");
65 this.currentConfiguration = neighbor;
66 this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
67 IetfInetUtil.INSTANCE.ipv4AddressNoZoneFor(neighbor.getNeighborAddress().getIpv4Address()),
72 public synchronized void restart(final RIB rib, final InstanceIdentifier<Bgp> bgpIid,
73 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
74 Preconditions.checkState(this.currentConfiguration != null);
75 start(rib, this.currentConfiguration, bgpIid, peerGroupLoader, tableTypeRegistry);
79 public synchronized void close() {
80 if (this.bgpAppPeerSingletonService != null) {
81 this.bgpAppPeerSingletonService = null;
83 if (this.serviceRegistration != null) {
84 this.serviceRegistration.unregister();
85 this.serviceRegistration = null;
90 public synchronized void instantiateServiceInstance() {
91 if (this.bgpAppPeerSingletonService != null) {
92 this.bgpAppPeerSingletonService.instantiateServiceInstance();
97 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
98 if (this.bgpAppPeerSingletonService != null) {
99 return this.bgpAppPeerSingletonService.closeServiceInstance();
102 return CommitInfo.emptyFluentFuture();
106 public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
107 return Objects.equals(this.currentConfiguration.key(), neighbor.key())
108 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
112 public synchronized BGPPeerState getPeerState() {
113 return this.bgpAppPeerSingletonService.getPeerState();
116 synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
117 this.serviceRegistration = serviceRegistration;
120 private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
121 private final ApplicationPeer applicationPeer;
122 private final DOMDataTreeChangeService dataTreeChangeService;
123 private final ApplicationRibId appRibId;
125 private boolean isServiceInstantiated;
127 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId,
128 final Ipv4AddressNoZone neighborAddress, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
129 this.applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
130 this.appRibId = appRibId;
131 this.dataTreeChangeService = rib.getService();
134 public synchronized void instantiateServiceInstance() {
135 this.isServiceInstantiated = true;
136 final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(APPRIB)
137 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
138 .node(TABLES_NID).node(TABLES_NID).build();
139 this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
140 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
143 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
144 if (!this.isServiceInstantiated) {
145 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
146 return CommitInfo.emptyFluentFuture();
148 LOG.info("Application peer instance closed {}", this.appRibId.getValue());
149 this.isServiceInstantiated = false;
150 return this.applicationPeer.close();
154 public BGPPeerState getPeerState() {
155 return this.applicationPeer.getPeerState();