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