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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Objects;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
21 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
23 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
24 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRib;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
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.osgi.framework.ServiceRegistration;
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 QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
43 private Neighbor currentConfiguration;
45 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
47 private ServiceRegistration<?> serviceRegistration;
49 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
50 final Config config = neighbor.getConfig();
51 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
52 return new ApplicationRibId(config.getDescription());
54 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
58 public synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
59 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
60 Preconditions.checkState(this.bgpAppPeerSingletonService == null,
61 "Previous peer instance was not closed.");
62 this.currentConfiguration = neighbor;
63 this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
64 neighbor.getNeighborAddress().getIpv4Address());
68 public synchronized void restart(final RIB rib, final InstanceIdentifier<Bgp> bgpIid,
69 final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
70 Preconditions.checkState(this.currentConfiguration != null);
71 start(rib, this.currentConfiguration, bgpIid, peerGroupLoader, tableTypeRegistry);
75 public synchronized void close() {
76 if (this.bgpAppPeerSingletonService != null) {
77 this.bgpAppPeerSingletonService = null;
79 if (this.serviceRegistration != null) {
80 this.serviceRegistration.unregister();
81 this.serviceRegistration = null;
86 public synchronized void instantiateServiceInstance() {
87 if (this.bgpAppPeerSingletonService != null) {
88 this.bgpAppPeerSingletonService.instantiateServiceInstance();
93 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
94 if (this.bgpAppPeerSingletonService != null) {
95 return this.bgpAppPeerSingletonService.closeServiceInstance();
98 return CommitInfo.emptyFluentFuture();
102 public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
103 return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
104 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
108 public synchronized BGPPeerState getPeerState() {
109 return this.bgpAppPeerSingletonService.getPeerState();
112 synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
113 this.serviceRegistration = serviceRegistration;
116 private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
117 private final ApplicationPeer applicationPeer;
118 private final DOMDataTreeChangeService dataTreeChangeService;
119 private final ApplicationRibId appRibId;
121 private boolean isServiceInstantiated;
123 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress) {
124 this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
125 this.appRibId = appRibId;
126 this.dataTreeChangeService = rib.getService();
129 public synchronized void instantiateServiceInstance() {
130 this.isServiceInstantiated = true;
131 final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
132 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
133 .node(Tables.QNAME).node(Tables.QNAME).build();
134 this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
135 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
138 public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
139 if (!this.isServiceInstantiated) {
140 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
141 return CommitInfo.emptyFluentFuture();
143 LOG.info("Application peer instance closed {}", this.appRibId.getValue());
144 this.isServiceInstantiated = false;
145 return this.applicationPeer.close();
149 public BGPPeerState getPeerState() {
150 return this.applicationPeer.getPeerState();