BUG-7222: Make BGP DS clean up asynchronous
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / AppPeer.java
1 /*
2  * Copyright (c) 2016 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.rib.impl.config;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Objects;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
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.BgpDeployer.WriteConfiguration;
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.BGPPeerStateConsumer;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
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.rev130925.ApplicationRib;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
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;
38
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();
42     private Neighbor currentConfiguration;
43     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
44     private ServiceRegistration<?> serviceRegistration;
45
46     @Override
47     public void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
48         final WriteConfiguration configurationWriter) {
49         Preconditions.checkState(this.bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
50         this.currentConfiguration = neighbor;
51         this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
52             neighbor.getNeighborAddress().getIpv4Address(), configurationWriter);
53     }
54
55     @Override
56     public void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
57         Preconditions.checkState(this.currentConfiguration != null);
58         start(rib, this.currentConfiguration, tableTypeRegistry, null);
59     }
60
61     @Override
62     public void close() {
63         try {
64             this.bgpAppPeerSingletonService.close();
65             this.bgpAppPeerSingletonService = null;
66         } catch (final Exception e) {
67             LOG.warn("Failed to close application peer instance", e);
68         }
69         if (this.serviceRegistration != null) {
70             this.serviceRegistration.unregister();
71             this.serviceRegistration = null;
72         }
73     }
74
75     @Override
76     public Boolean containsEqualConfiguration(final Neighbor neighbor) {
77         return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
78                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
79     }
80
81     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
82         final Config config = neighbor.getConfig();
83         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
84             return new ApplicationRibId(config.getDescription());
85         }
86         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
87     }
88
89     @Override
90     public BGPPeerState getPeerState() {
91         return this.bgpAppPeerSingletonService.getPeerState();
92     }
93
94     void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
95         this.serviceRegistration = serviceRegistration;
96     }
97
98     private final class BgpAppPeerSingletonService implements ClusterSingletonService, BGPPeerStateConsumer,
99         AutoCloseable {
100         private final ApplicationPeer applicationPeer;
101         private final DOMDataTreeChangeService dataTreeChangeService;
102         private final ApplicationRibId appRibId;
103         private ClusterSingletonServiceRegistration singletonServiceRegistration;
104         private final ServiceGroupIdentifier serviceGroupIdentifier;
105         private final WriteConfiguration configurationWriter;
106
107         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
108             final WriteConfiguration configurationWriter) {
109             this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
110             this.appRibId = appRibId;
111             this.dataTreeChangeService = rib.getService();
112             this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
113             this.configurationWriter = configurationWriter;
114             LOG.info("Application Peer Singleton Service {} registered", getIdentifier());
115             //this need to be always the last step
116             this.singletonServiceRegistration = rib.registerClusterSingletonService(this);
117         }
118
119         @Override
120         public void close() throws Exception {
121             if (this.singletonServiceRegistration != null) {
122                 this.singletonServiceRegistration.close();
123                 this.singletonServiceRegistration = null;
124             }
125         }
126
127         @Override
128         public void instantiateServiceInstance() {
129             if(this.configurationWriter != null) {
130                 this.configurationWriter.apply();
131             }
132             LOG.info("Application Peer Singleton Service {} instantiated", getIdentifier());
133             final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
134                 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue()).node(Tables.QNAME).node(Tables.QNAME).build();
135             this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
136                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
137         }
138
139         @Override
140         public ListenableFuture<Void> closeServiceInstance() {
141             LOG.info("Application Peer Singleton Service {} instance closed", getIdentifier());
142             return this.applicationPeer.close();
143         }
144
145         @Override
146         public ServiceGroupIdentifier getIdentifier() {
147             return this.serviceGroupIdentifier;
148         }
149
150         @Override
151         public BGPPeerState getPeerState() {
152             return this.applicationPeer.getPeerState();
153         }
154     }
155 }