2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Strings;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Objects;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
21 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
22 import org.opendaylight.protocol.bgp.rib.impl.ApplicationPeer;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer.WriteConfiguration;
24 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
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.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRib;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public final class AppPeer implements PeerBean {
37 private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
38 private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
39 private Neighbor currentConfiguration;
40 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
41 private BGPOpenConfigMappingService mappingService;
44 public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
45 final WriteConfiguration configurationWriter) {
46 Preconditions.checkState(this.bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
47 this.currentConfiguration = neighbor;
48 this.mappingService = mappingService;
49 this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
50 neighbor.getNeighborAddress().getIpv4Address(), configurationWriter);
54 public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
55 Preconditions.checkState(this.currentConfiguration != null);
56 start(rib, this.currentConfiguration, mappingService, null);
62 this.bgpAppPeerSingletonService.close();
63 this.bgpAppPeerSingletonService = null;
64 } catch (final Exception e) {
65 LOG.warn("Failed to close application peer instance", e);
70 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
71 return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
72 && this.mappingService.isApplicationPeer(neighbor);
75 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
76 final Config config = neighbor.getConfig();
77 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
78 return new ApplicationRibId(config.getDescription());
80 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
83 private final class BgpAppPeerSingletonService implements ClusterSingletonService, AutoCloseable {
84 private final ApplicationPeer applicationPeer;
85 private final DOMDataTreeChangeService dataTreeChangeService;
86 private final ApplicationRibId appRibId;
87 private ClusterSingletonServiceRegistration singletonServiceRegistration;
88 private final ServiceGroupIdentifier serviceGroupIdentifier;
89 private final WriteConfiguration configurationWriter;
91 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
92 final WriteConfiguration configurationWriter) {
93 this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
94 this.appRibId = appRibId;
95 this.dataTreeChangeService = rib.getService();
96 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
97 this.configurationWriter = configurationWriter;
98 LOG.info("Application Peer Singleton Service {} registered", getIdentifier());
99 //this need to be always the last step
100 this.singletonServiceRegistration = rib.registerClusterSingletonService(this);
104 public void close() throws Exception {
105 if (this.singletonServiceRegistration != null) {
106 this.singletonServiceRegistration.close();
107 this.singletonServiceRegistration = null;
112 public void instantiateServiceInstance() {
113 if(this.configurationWriter != null) {
114 this.configurationWriter.apply();
116 LOG.info("Application Peer Singleton Service {} instantiated", getIdentifier());
117 final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
118 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue()).node(Tables.QNAME).node(Tables.QNAME).build();
119 this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
120 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
124 public ListenableFuture<Void> closeServiceInstance() {
125 LOG.info("Application Peer Singleton Service {} instance closed", getIdentifier());
126 return this.applicationPeer.close();
130 public ServiceGroupIdentifier getIdentifier() {
131 return this.serviceGroupIdentifier;