edcc322755e76a2d8bb9915c6ace0e54b90fc29b
[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.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Objects;
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.singleton.common.api.ClusterSingletonService;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
23 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer.WriteConfiguration;
25 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
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.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.rev130925.ApplicationRib;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class AppPeer implements PeerBean {
39     private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
40     private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
41     private Neighbor currentConfiguration;
42     private BgpAppPeerSingletonService bgpAppPeerSingletonService;
43     private BGPOpenConfigMappingService mappingService;
44
45     @Override
46     public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService, final WriteConfiguration configurationWriter) {
47         this.currentConfiguration = neighbor;
48         this.mappingService = mappingService;
49         this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor), neighbor.getNeighborAddress().getIpv4Address(),
50                 configurationWriter);
51     }
52
53     @Override
54     public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
55         Preconditions.checkState(this.currentConfiguration != null);
56         start(rib, this.currentConfiguration, mappingService, null);
57     }
58
59     @Override
60     public void close() {
61         try {
62             this.bgpAppPeerSingletonService.close();
63         } catch (final Exception e) {
64             LOG.warn("Failed to close application peer instance", e);
65         }
66     }
67
68     @Override
69     public Boolean containsEqualConfiguration(final Neighbor neighbor) {
70         return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
71                 && this.mappingService.isApplicationPeer(neighbor);
72     }
73
74     private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
75         final Config config = neighbor.getConfig();
76         if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
77             return new ApplicationRibId(config.getDescription());
78         }
79         return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
80     }
81
82     private final class BgpAppPeerSingletonService implements ClusterSingletonService, AutoCloseable {
83         private final ApplicationPeer applicationPeer;
84         private final DOMDataTreeChangeService dataTreeChangeService;
85         private final ApplicationRibId appRibId;
86         private ClusterSingletonServiceRegistration singletonServiceRegistration;
87         private ListenerRegistration<ApplicationPeer> registration;
88         private final ServiceGroupIdentifier serviceGroupIdentifier;
89         private final WriteConfiguration configurationWriter;
90
91         BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress, final WriteConfiguration configurationWriter) {
92             this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
93             this.appRibId = appRibId;
94             this.dataTreeChangeService = rib.getService();
95             this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
96             this.configurationWriter = configurationWriter;
97             LOG.info("Application Peer Singleton Service {} registered", getIdentifier());
98             //this need to be always the last step
99             this.singletonServiceRegistration = rib.registerClusterSingletonService(this);
100         }
101
102         @Override
103         public void close() throws Exception {
104             if (this.singletonServiceRegistration != null) {
105                 this.singletonServiceRegistration.close();
106                 this.singletonServiceRegistration = null;
107             }
108         }
109
110         @Override
111         public void instantiateServiceInstance() {
112             if(this.configurationWriter != null) {
113                 this.configurationWriter.apply();
114             }
115             LOG.info("Application Peer Singleton Service {} instantiated", getIdentifier());
116             final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
117                 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue()).node(Tables.QNAME).node(Tables.QNAME).build();
118             this.applicationPeer.instantiateServiceInstance();
119             this.registration = this.dataTreeChangeService
120                 .registerDataTreeChangeListener(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId), this.applicationPeer);
121         }
122
123         @Override
124         public ListenableFuture<Void> closeServiceInstance() {
125             LOG.info("Application Peer Singleton Service {} instance closed", getIdentifier());
126             this.registration.close();
127             this.applicationPeer.close();
128             return Futures.immediateFuture(null);
129         }
130
131         @Override
132         public ServiceGroupIdentifier getIdentifier() {
133             return this.serviceGroupIdentifier;
134         }
135     }
136 }