Update RIB-based constants
[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 import static org.opendaylight.protocol.bgp.rib.spi.RIBNodeIdentifiers.TABLES_NID;
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.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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 NodeIdentifier APPRIB = NodeIdentifier.create(ApplicationRib.QNAME);
42     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
43     @GuardedBy("this")
44     private Neighbor currentConfiguration;
45     @GuardedBy("this")
46     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
47     @GuardedBy("this")
48     private ServiceRegistration<?> serviceRegistration;
49
50     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
51         final Config config = neighbor.getConfig();
52         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
53             return new ApplicationRibId(config.getDescription());
54         }
55         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
56     }
57
58     @Override
59     public synchronized void start(final RIB rib, final Neighbor neighbor, final InstanceIdentifier<Bgp> bgpIid,
60             final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
61         Preconditions.checkState(this.bgpAppPeerSingletonService == null,
62                 "Previous peer instance was not closed.");
63         this.currentConfiguration = neighbor;
64         this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
65                 neighbor.getNeighborAddress().getIpv4Address(), tableTypeRegistry);
66     }
67
68     @Override
69     public synchronized void restart(final RIB rib, final InstanceIdentifier<Bgp> bgpIid,
70             final PeerGroupConfigLoader peerGroupLoader, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
71         Preconditions.checkState(this.currentConfiguration != null);
72         start(rib, this.currentConfiguration, bgpIid, peerGroupLoader, tableTypeRegistry);
73     }
74
75     @Override
76     public synchronized void close() {
77         if (this.bgpAppPeerSingletonService != null) {
78             this.bgpAppPeerSingletonService = null;
79         }
80         if (this.serviceRegistration != null) {
81             this.serviceRegistration.unregister();
82             this.serviceRegistration = null;
83         }
84     }
85
86     @Override
87     public synchronized void instantiateServiceInstance() {
88         if (this.bgpAppPeerSingletonService != null) {
89             this.bgpAppPeerSingletonService.instantiateServiceInstance();
90         }
91     }
92
93     @Override
94     public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
95         if (this.bgpAppPeerSingletonService != null) {
96             return this.bgpAppPeerSingletonService.closeServiceInstance();
97         }
98
99         return CommitInfo.emptyFluentFuture();
100     }
101
102     @Override
103     public synchronized Boolean containsEqualConfiguration(final Neighbor neighbor) {
104         return Objects.equals(this.currentConfiguration.key(), neighbor.key())
105                 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
106     }
107
108     @Override
109     public synchronized BGPPeerState getPeerState() {
110         return this.bgpAppPeerSingletonService.getPeerState();
111     }
112
113     synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
114         this.serviceRegistration = serviceRegistration;
115     }
116
117     private static final class BgpAppPeerSingletonService implements BGPPeerStateConsumer {
118         private final ApplicationPeer applicationPeer;
119         private final DOMDataTreeChangeService dataTreeChangeService;
120         private final ApplicationRibId appRibId;
121         @GuardedBy("this")
122         private boolean isServiceInstantiated;
123
124         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
125                 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
126             this.applicationPeer = new ApplicationPeer(tableTypeRegistry, appRibId, neighborAddress, rib);
127             this.appRibId = appRibId;
128             this.dataTreeChangeService = rib.getService();
129         }
130
131         public synchronized void instantiateServiceInstance() {
132             this.isServiceInstantiated = true;
133             final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(APPRIB)
134                     .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue())
135                     .node(TABLES_NID).node(TABLES_NID).build();
136             this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
137                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
138         }
139
140         public synchronized FluentFuture<? extends CommitInfo> closeServiceInstance() {
141             if (!this.isServiceInstantiated) {
142                 LOG.trace("Application peer already closed {}", this.appRibId.getValue());
143                 return CommitInfo.emptyFluentFuture();
144             }
145             LOG.info("Application peer instance closed {}", this.appRibId.getValue());
146             this.isServiceInstantiated = false;
147             return this.applicationPeer.close();
148         }
149
150         @Override
151         public BGPPeerState getPeerState() {
152             return this.applicationPeer.getPeerState();
153         }
154     }
155 }