Migrate deprecated submit() to commit() for BGP/BMP
[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.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;
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     @GuardedBy("this")
43     private Neighbor currentConfiguration;
44     @GuardedBy("this")
45     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
46     @GuardedBy("this")
47     private ServiceRegistration<?> serviceRegistration;
48
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());
53         }
54         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
55     }
56
57     @Override
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());
65     }
66
67     @Override
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);
72     }
73
74     @Override
75     public synchronized void close() {
76         if (this.bgpAppPeerSingletonService != null) {
77             this.bgpAppPeerSingletonService = null;
78         }
79         if (this.serviceRegistration != null) {
80             this.serviceRegistration.unregister();
81             this.serviceRegistration = null;
82         }
83     }
84
85     @Override
86     public synchronized void instantiateServiceInstance() {
87         if (this.bgpAppPeerSingletonService != null) {
88             this.bgpAppPeerSingletonService.instantiateServiceInstance();
89         }
90     }
91
92     @Override
93     public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
94         if (this.bgpAppPeerSingletonService != null) {
95             return this.bgpAppPeerSingletonService.closeServiceInstance();
96         }
97
98         return CommitInfo.emptyFluentFuture();
99     }
100
101     @Override
102     public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
103         return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
104                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
105     }
106
107     @Override
108     public synchronized BGPPeerState getPeerState() {
109         return this.bgpAppPeerSingletonService.getPeerState();
110     }
111
112     synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
113         this.serviceRegistration = serviceRegistration;
114     }
115
116     private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
117         private final ApplicationPeer applicationPeer;
118         private final DOMDataTreeChangeService dataTreeChangeService;
119         private final ApplicationRibId appRibId;
120         @GuardedBy("this")
121         private boolean isServiceInstantiated;
122
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();
127         }
128
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));
136         }
137
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();
142             }
143             LOG.info("Application peer instance closed {}", this.appRibId.getValue());
144             this.isServiceInstantiated = false;
145             return this.applicationPeer.close();
146         }
147
148         @Override
149         public BGPPeerState getPeerState() {
150             return this.applicationPeer.getPeerState();
151         }
152     }
153 }