BGPCEP-730: Fix ModifiedNodeDoesNotExistException
[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.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Objects;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
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.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.ApplicationRib;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.ApplicationRibId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.Tables;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.osgi.framework.ServiceRegistration;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
38     private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
39     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
40     @GuardedBy("this")
41     private Neighbor currentConfiguration;
42     @GuardedBy("this")
43     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
44     @GuardedBy("this")
45     private ServiceRegistration<?> serviceRegistration;
46
47     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
48         final Config config = neighbor.getConfig();
49         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
50             return new ApplicationRibId(config.getDescription());
51         }
52         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
53     }
54
55     @Override
56     public synchronized void start(final RIB rib, final Neighbor neighbor,
57             final BGPTableTypeRegistryConsumer tableTypeRegistry) {
58         Preconditions.checkState(this.bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
59         this.currentConfiguration = neighbor;
60         this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
61                 neighbor.getNeighborAddress().getIpv4Address());
62     }
63
64     @Override
65     public synchronized void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
66         Preconditions.checkState(this.currentConfiguration != null);
67         start(rib, this.currentConfiguration, tableTypeRegistry);
68     }
69
70     @Override
71     public synchronized void close() {
72         if (this.bgpAppPeerSingletonService != null) {
73             this.bgpAppPeerSingletonService = null;
74         }
75         if (this.serviceRegistration != null) {
76             this.serviceRegistration.unregister();
77             this.serviceRegistration = null;
78         }
79     }
80
81     @Override
82     public synchronized void instantiateServiceInstance() {
83         if (this.bgpAppPeerSingletonService != null) {
84             this.bgpAppPeerSingletonService.instantiateServiceInstance();
85         }
86     }
87
88     @Override
89     public synchronized ListenableFuture<Void> closeServiceInstance() {
90         if (this.bgpAppPeerSingletonService != null) {
91             return this.bgpAppPeerSingletonService.closeServiceInstance();
92         }
93
94         return Futures.immediateFuture(null);
95     }
96
97     @Override
98     public Boolean containsEqualConfiguration(final Neighbor neighbor) {
99         return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
100                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
101     }
102
103     @Override
104     public BGPPeerState getPeerState() {
105         return this.bgpAppPeerSingletonService.getPeerState();
106     }
107
108     synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
109         this.serviceRegistration = serviceRegistration;
110     }
111
112     private final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
113         private final ApplicationPeer applicationPeer;
114         private final DOMDataTreeChangeService dataTreeChangeService;
115         private final ApplicationRibId appRibId;
116         @GuardedBy("this")
117         private boolean isServiceInstantiated;
118
119         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress) {
120             this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
121             this.appRibId = appRibId;
122             this.dataTreeChangeService = rib.getService();
123         }
124
125         public synchronized void instantiateServiceInstance() {
126             this.isServiceInstantiated = true;
127             final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
128                     .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
129                     .node(Tables.QNAME).node(Tables.QNAME).build();
130             this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
131                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
132         }
133
134         public synchronized ListenableFuture<Void> closeServiceInstance() {
135             if (!this.isServiceInstantiated) {
136                 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
137                 return Futures.immediateFuture(null);
138             }
139             LOG.info("Application peer instance closed {}", this.appRibId.getValue());
140             this.isServiceInstantiated = false;
141             return this.applicationPeer.close();
142         }
143
144         @Override
145         public BGPPeerState getPeerState() {
146             return this.applicationPeer.getPeerState();
147         }
148     }
149 }