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.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.BGPTableTypeRegistryConsumer;
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.protocol.bgp.rib.spi.state.BGPPeerState;
27 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.Config;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRib;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.osgi.framework.ServiceRegistration;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class AppPeer implements PeerBean, BGPPeerStateConsumer {
41 private static final Logger LOG = LoggerFactory.getLogger(AppPeer.class);
42 private static final QName APP_ID_QNAME = QName.create(ApplicationRib.QNAME, "id").intern();
43 private Neighbor currentConfiguration;
44 private BgpAppPeerSingletonService bgpAppPeerSingletonService;
45 private ServiceRegistration<?> serviceRegistration;
48 public void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
49 final WriteConfiguration configurationWriter) {
50 Preconditions.checkState(this.bgpAppPeerSingletonService == null, "Previous peer instance was not closed.");
51 this.currentConfiguration = neighbor;
52 this.bgpAppPeerSingletonService = new BgpAppPeerSingletonService(rib, createAppRibId(neighbor),
53 neighbor.getNeighborAddress().getIpv4Address(), configurationWriter);
57 public void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
58 Preconditions.checkState(this.currentConfiguration != null);
59 start(rib, this.currentConfiguration, tableTypeRegistry, null);
65 this.bgpAppPeerSingletonService.close();
66 this.bgpAppPeerSingletonService = null;
67 } catch (final Exception e) {
68 LOG.warn("Failed to close application peer instance", e);
70 if (this.serviceRegistration != null) {
71 this.serviceRegistration.unregister();
72 this.serviceRegistration = null;
77 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
78 return Objects.equals(this.currentConfiguration.getKey(), neighbor.getKey())
79 && OpenConfigMappingUtil.isApplicationPeer(neighbor);
82 private static ApplicationRibId createAppRibId(final Neighbor neighbor) {
83 final Config config = neighbor.getConfig();
84 if (config != null && !Strings.isNullOrEmpty(config.getDescription())) {
85 return new ApplicationRibId(config.getDescription());
87 return new ApplicationRibId(neighbor.getNeighborAddress().getIpv4Address().getValue());
91 public BGPPeerState getPeerState() {
92 return this.bgpAppPeerSingletonService.getPeerState();
95 void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
96 this.serviceRegistration = serviceRegistration;
99 private final class BgpAppPeerSingletonService implements ClusterSingletonService, BGPPeerStateConsumer,
101 private final ApplicationPeer applicationPeer;
102 private final DOMDataTreeChangeService dataTreeChangeService;
103 private final ApplicationRibId appRibId;
104 private ClusterSingletonServiceRegistration singletonServiceRegistration;
105 private final ServiceGroupIdentifier serviceGroupIdentifier;
106 private final WriteConfiguration configurationWriter;
108 BgpAppPeerSingletonService(final RIB rib, final ApplicationRibId appRibId, final Ipv4Address neighborAddress,
109 final WriteConfiguration configurationWriter) {
110 this.applicationPeer = new ApplicationPeer(appRibId, neighborAddress, rib);
111 this.appRibId = appRibId;
112 this.dataTreeChangeService = rib.getService();
113 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
114 this.configurationWriter = configurationWriter;
115 LOG.info("Application Peer Singleton Service {} registered", getIdentifier());
116 //this need to be always the last step
117 this.singletonServiceRegistration = rib.registerClusterSingletonService(this);
121 public void close() throws Exception {
122 if (this.singletonServiceRegistration != null) {
123 this.singletonServiceRegistration.close();
124 this.singletonServiceRegistration = null;
129 public void instantiateServiceInstance() {
130 if(this.configurationWriter != null) {
131 this.configurationWriter.apply();
133 LOG.info("Application Peer Singleton Service {} instantiated", getIdentifier());
134 final YangInstanceIdentifier yangIId = YangInstanceIdentifier.builder().node(ApplicationRib.QNAME)
135 .nodeWithKey(ApplicationRib.QNAME, APP_ID_QNAME, this.appRibId.getValue()).node(Tables.QNAME).node(Tables.QNAME).build();
136 this.applicationPeer.instantiateServiceInstance(this.dataTreeChangeService,
137 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangIId));
141 public ListenableFuture<Void> closeServiceInstance() {
142 LOG.info("Application Peer Singleton Service {} instance closed", getIdentifier());
143 this.applicationPeer.close();
144 return Futures.immediateFuture(null);
148 public ServiceGroupIdentifier getIdentifier() {
149 return this.serviceGroupIdentifier;
153 public BGPPeerState getPeerState() {
154 return this.applicationPeer.getPeerState();