Bump upstreams
[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 package org.opendaylight.protocol.bgp.rib.impl.config;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
13
14 import com.google.common.base.Strings;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.util.Objects;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker.DataTreeChangeExtension;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
22 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
23 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
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.BGPPeerStateProvider;
27 import org.opendaylight.protocol.bgp.rib.spi.state.BGPStateProviderRegistry;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRib;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
35 import org.opendaylight.yangtools.concepts.Registration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 final class AppPeer extends PeerBean {
44     private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
45     private static final NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
46     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
47
48     private final BGPStateProviderRegistry stateProviderRegistry;
49     @GuardedBy("this")
50     private Neighbor currentConfiguration;
51     @GuardedBy("this")
52     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
53     @GuardedBy("this")
54     private Registration stateProviderRegistration;
55
56     AppPeer(final BGPStateProviderRegistry stateProviderRegistry) {
57         this.stateProviderRegistry = requireNonNull(stateProviderRegistry);
58     }
59
60     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
61         final Config config = neighbor.getConfig();
62         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
63             return new ApplicationRibId(config.getDescription());
64         }
65         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
66     }
67
68     @Override
69     synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
70             final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
71         checkState(bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
72         LOG.info("Starting AppPeer instance {}", neighbor.getNeighborAddress());
73         currentConfiguration = neighbor;
74         bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
75             IetfInetUtil.ipv4AddressNoZoneFor(neighbor.getNeighborAddress().getIpv4Address()), tableTypeRegistry);
76         stateProviderRegistration = stateProviderRegistry.register(this);
77     }
78
79     @Override
80     synchronized ListenableFuture<?> stop() {
81         if (bgpAppPeerSingletonService == null) {
82             LOG.info("App Peer {} already closed, skipping", currentConfiguration.getNeighborAddress());
83             return Futures.immediateVoidFuture();
84         }
85
86         LOG.info("Closing App Peer {}", currentConfiguration.getNeighborAddress());
87         if (stateProviderRegistration != null) {
88             stateProviderRegistration.close();
89             stateProviderRegistration = null;
90         }
91
92         final var future = bgpAppPeerSingletonService.closeServiceInstance();
93         bgpAppPeerSingletonService = null;
94         return future;
95     }
96
97     @Override
98     synchronized void instantiateServiceInstance() {
99         if (bgpAppPeerSingletonService != null) {
100             bgpAppPeerSingletonService.instantiateServiceInstance();
101         }
102     }
103
104     @Override
105     synchronized ListenableFuture<?> closeServiceInstance() {
106         return bgpAppPeerSingletonService != null ? bgpAppPeerSingletonService.closeServiceInstance()
107             : Futures.immediateVoidFuture();
108     }
109
110     @Override
111     synchronized boolean containsEqualConfiguration(final Neighbor neighbor) {
112         return Objects.equals(currentConfiguration.key(), neighbor.key())
113                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
114     }
115
116     @Override
117     synchronized Neighbor getCurrentConfiguration() {
118         return currentConfiguration;
119     }
120
121     @Override
122     public synchronized BGPPeerState getPeerState() {
123         return bgpAppPeerSingletonService.getPeerState();
124     }
125
126     private static final class BgpAppPeerSingletonService implements BGPPeerStateProvider {
127         private final ApplicationPeer applicationPeer;
128         private final DataTreeChangeExtension dataTreeChangeService;
129         private final ApplicationRibId appRibId;
130         @GuardedBy("this")
131         private boolean isServiceInstantiated;
132
133         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId,
134                 final Ipv4AddressNoZone neighborAddress, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
135             applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
136             this.appRibId = appRibId;
137             dataTreeChangeService = rib.getService();
138         }
139
140         synchronized void instantiateServiceInstance() {
141             isServiceInstantiated = true;
142             applicationPeer.instantiateServiceInstance(dataTreeChangeService,
143                 DOMDataTreeIdentifier.of(LogicalDatastoreType.CONFIGURATION,  YangInstanceIdentifier.builder()
144                     .node(APPRIB)
145                     .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, appRibId.getValue())
146                     .node(TABLES_NID)
147                     .node(TABLES_NID)
148                     .build()));
149         }
150
151         synchronized ListenableFuture<?> closeServiceInstance() {
152             if (!isServiceInstantiated) {
153                 LOG.info("Application peer already closed {}", appRibId.getValue());
154                 return Futures.immediateVoidFuture();
155             }
156             LOG.info("Application peer instance closed {}", appRibId.getValue());
157             isServiceInstantiated = false;
158             return applicationPeer.close();
159         }
160
161         @Override
162         public BGPPeerState getPeerState() {
163             return applicationPeer.getPeerState();
164         }
165     }
166 }