cb4568aa41c40822cbaaec057000b7deb87f4dc0
[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 org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.util.concurrent.FluentFuture;
15 import java.util.Objects;
16 import org.checkerframework.checker.lock.qual.GuardedBy;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
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.RIB;
24 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
25 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp;
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.rev180329.ApplicationRib;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.ApplicationRibId;
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.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
36 import org.osgi.framework.ServiceRegistration;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
41     private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
42     private static final NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
43     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
44     @GuardedBy("this")
45     private Neighbor currentConfiguration;
46     @GuardedBy("this")
47     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
48     @GuardedBy("this")
49     private ServiceRegistration<?> serviceRegistration;
50
51     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
52         final Config config = neighbor.getConfig();
53         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
54             return new ApplicationRibId(config.getDescription());
55         }
56         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
57     }
58
59     @Override
60     public synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
61             final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
62         Preconditions.checkState(this.bgpAppPeerSingletonService == null,
63                 "Previous peer instance was not closed.");
64         this.currentConfiguration = neighbor;
65         this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
66                 neighbor.getNeighborAddress().getIpv4Address(), tableTypeRegistry);
67     }
68
69     @Override
70     public synchronized void restart(final RIB rib, final InstanceIdentifier<Bgp> bgpIid,
71             final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
72         Preconditions.checkState(this.currentConfiguration != null);
73         start(rib, this.currentConfiguration, bgpIid, peerGroupLoader, tableTypeRegistry);
74     }
75
76     @Override
77     public synchronized void close() {
78         if (this.bgpAppPeerSingletonService != null) {
79             this.bgpAppPeerSingletonService = null;
80         }
81         if (this.serviceRegistration != null) {
82             this.serviceRegistration.unregister();
83             this.serviceRegistration = null;
84         }
85     }
86
87     @Override
88     public synchronized void instantiateServiceInstance() {
89         if (this.bgpAppPeerSingletonService != null) {
90             this.bgpAppPeerSingletonService.instantiateServiceInstance();
91         }
92     }
93
94     @Override
95     public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
96         if (this.bgpAppPeerSingletonService != null) {
97             return this.bgpAppPeerSingletonService.closeServiceInstance();
98         }
99
100         return CommitInfo.emptyFluentFuture();
101     }
102
103     @Override
104     public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
105         return Objects.equals(this.currentConfiguration.key(), neighbor.key())
106                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
107     }
108
109     @Override
110     public synchronized BGPPeerState getPeerState() {
111         return this.bgpAppPeerSingletonService.getPeerState();
112     }
113
114     synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
115         this.serviceRegistration = serviceRegistration;
116     }
117
118     private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
119         private final ApplicationPeer applicationPeer;
120         private final DOMDataTreeChangeService dataTreeChangeService;
121         private final ApplicationRibId appRibId;
122         @GuardedBy("this")
123         private boolean isServiceInstantiated;
124
125         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
126                 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
127             this.applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
128             this.appRibId = appRibId;
129             this.dataTreeChangeService = rib.getService();
130         }
131
132         public synchronized void instantiateServiceInstance() {
133             this.isServiceInstantiated = true;
134             final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(APPRIB)
135                     .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
136                     .node(TABLES_NID).node(TABLES_NID).build();
137             this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
138                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
139         }
140
141         public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
142             if (!this.isServiceInstantiated) {
143                 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
144                 return CommitInfo.emptyFluentFuture();
145             }
146             LOG.info("Application peer instance closed {}", this.appRibId.getValue());
147             this.isServiceInstantiated = false;
148             return this.applicationPeer.close();
149         }
150
151         @Override
152         public BGPPeerState getPeerState() {
153             return this.applicationPeer.getPeerState();
154         }
155     }
156 }