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