From b09fb9d9df8ff2539710aa3fd05e1f9085d7a936 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Wed, 31 May 2017 10:50:13 +0200 Subject: Dedicated provider for BgpNeighbours Change-Id: I1a98ea27dde5ba77f8b20382dfeb29496f3282b2 Signed-off-by: Marek Gradzki --- .../infra/bgp/BGPPeerRegistryProvider.java | 123 -------------------- .../java/io/fd/honeycomb/infra/bgp/BgpModule.java | 9 +- .../honeycomb/infra/bgp/BgpNeighboursProvider.java | 125 +++++++++++++++++++++ .../io/fd/honeycomb/infra/bgp/distro/Main.java | 3 + 4 files changed, 136 insertions(+), 124 deletions(-) delete mode 100644 infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BGPPeerRegistryProvider.java create mode 100644 infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpNeighboursProvider.java (limited to 'infra/bgp-distribution') diff --git a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BGPPeerRegistryProvider.java b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BGPPeerRegistryProvider.java deleted file mode 100644 index 217c43823..000000000 --- a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BGPPeerRegistryProvider.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2017 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.infra.bgp; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static io.fd.honeycomb.translate.util.JsonUtils.readContainerEntryJson; -import static org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil.APPLICATION_PEER_GROUP_NAME; -import static org.opendaylight.yangtools.sal.binding.generator.impl.BindingSchemaContextUtils.findDataNodeContainer; - -import com.google.common.base.Optional; -import com.google.inject.Inject; -import io.fd.honeycomb.infra.distro.ProviderTrait; -import java.io.InputStream; -import java.util.Map; -import javax.annotation.Nonnull; -import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec; -import org.opendaylight.controller.sal.core.api.model.SchemaService; -import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService; -import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry; -import org.opendaylight.protocol.bgp.rib.impl.config.AppPeer; -import org.opendaylight.protocol.bgp.rib.impl.config.BgpPeer; -import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; -import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborPeerGroupConfig; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey; -import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Config2; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Protocol1; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.api.SchemaNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -final class BGPPeerRegistryProvider extends ProviderTrait { - private static final Logger LOG = LoggerFactory.getLogger(BGPPeerRegistryProvider.class); - private static final String PEERS_CFG = "/bgp-peers.json"; - @Inject - private BindingToNormalizedNodeCodec codec; - @Inject - private RIB globalRib; - @Inject - private BGPOpenConfigMappingService mappingService; - @Inject - private SchemaService schemaService; - - @Override - protected BGPPeerRegistry create() { - final BGPPeerRegistry peerRegistry = StrictBGPPeerRegistry.instance(); - final Neighbors neighbors = readNeighbours(); - for (final Neighbor neighbor : neighbors.getNeighbor()) { - if (isApplicationPeer(neighbor)) { - LOG.trace("Starting AppPeer for {}", neighbor); - new AppPeer().start(globalRib, neighbor, mappingService, null); - } else { - LOG.trace("Starting BgpPeer for {}", neighbor); - new BgpPeer(null, peerRegistry).start(globalRib, neighbor, mappingService, null); - } - } - LOG.debug("Created BGPPeerRegistry with neighbours {}", neighbors); - return peerRegistry; - } - - private Neighbors readNeighbours() { - LOG.debug("Reading BGP neighbours from {}", PEERS_CFG); - final InputStream resourceStream = this.getClass().getResourceAsStream(PEERS_CFG); - checkState(resourceStream != null, "Resource %s not found", PEERS_CFG); - - final InstanceIdentifier bgpII = InstanceIdentifier.create(NetworkInstances.class) - .child(NetworkInstance.class, new NetworkInstanceKey("dummy-value")).child(Protocols.class) - .child(Protocol.class, new ProtocolKey(BGP.class, "dummy-value")).augmentation(Protocol1.class) - .child(Bgp.class); - final InstanceIdentifier neighborsII = bgpII.child(Neighbors.class); - - final YangInstanceIdentifier neighborsYII = codec.toYangInstanceIdentifier(neighborsII); - final SchemaContext schemaContext = schemaService.getGlobalContext(); - final Optional parentNode = findDataNodeContainer(schemaContext, bgpII); - final ContainerNode parentContainer = readContainerEntryJson(schemaContext, resourceStream, - (SchemaNode) parentNode.get(), - (YangInstanceIdentifier.NodeIdentifier) neighborsYII.getLastPathArgument()); - final NormalizedNode neighborsContainer = parentContainer.getValue().iterator().next(); - - final Map.Entry, DataObject> entry = codec.fromNormalizedNode(neighborsYII, neighborsContainer); - checkNotNull(entry, "Failed to deserialize neighbours configuration at %s", PEERS_CFG); - return (Neighbors) entry.getValue(); - } - - private static boolean isApplicationPeer(@Nonnull final Neighbor neighbor) { - return java.util.Optional.of(neighbor.getConfig()) - .map(config -> config.getAugmentation(Config2.class)) - .map(BgpNeighborPeerGroupConfig::getPeerGroup) - .map(APPLICATION_PEER_GROUP_NAME::equals) - .orElse(false); - } -} diff --git a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpModule.java b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpModule.java index 128e9f0de..efef5a71f 100644 --- a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpModule.java +++ b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpModule.java @@ -32,9 +32,11 @@ import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore; import org.opendaylight.protocol.bgp.openconfig.impl.BGPOpenConfigMappingServiceImpl; import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService; +import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher; import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighbors; public final class BgpModule extends PrivateModule { @@ -49,11 +51,16 @@ public final class BgpModule extends PrivateModule { // Configure peer registry bind(BGPOpenConfigMappingService.class).toInstance(new BGPOpenConfigMappingServiceImpl()); - bind(BGPPeerRegistry.class).toProvider(BGPPeerRegistryProvider.class); + bind(BGPPeerRegistry.class).toInstance(StrictBGPPeerRegistry.instance()); + // Create BGP server instance bind(BgpServerProvider.BgpServer.class).toProvider(BgpServerProvider.class).in(Singleton.class); expose(BgpServerProvider.BgpServer.class); + + // Initialize BgpNeighbours + bind(BgpNeighbors.class).toProvider(BgpNeighboursProvider.class).in(Singleton.class); + expose(BgpNeighbors.class); } private void configureRIB() { diff --git a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpNeighboursProvider.java b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpNeighboursProvider.java new file mode 100644 index 000000000..fab14e827 --- /dev/null +++ b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/BgpNeighboursProvider.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.infra.bgp; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static io.fd.honeycomb.translate.util.JsonUtils.readContainerEntryJson; +import static org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil.APPLICATION_PEER_GROUP_NAME; +import static org.opendaylight.yangtools.sal.binding.generator.impl.BindingSchemaContextUtils.findDataNodeContainer; + +import com.google.common.base.Optional; +import com.google.inject.Inject; +import io.fd.honeycomb.infra.distro.ProviderTrait; +import java.io.InputStream; +import java.util.Map; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec; +import org.opendaylight.controller.sal.core.api.model.SchemaService; +import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService; +import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry; +import org.opendaylight.protocol.bgp.rib.impl.config.AppPeer; +import org.opendaylight.protocol.bgp.rib.impl.config.BgpPeer; +import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry; +import org.opendaylight.protocol.bgp.rib.impl.spi.RIB; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighborPeerGroupConfig; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighbors; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.Bgp; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Neighbors; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.NetworkInstances; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstance; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.NetworkInstanceKey; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.Protocols; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.Protocol; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.network.instance.rev151018.network.instance.top.network.instances.network.instance.protocols.ProtocolKey; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.BGP; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Config2; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.Protocol1; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class BgpNeighboursProvider extends ProviderTrait { + private static final Logger LOG = LoggerFactory.getLogger(BgpNeighboursProvider.class); + private static final String PEERS_CFG = "/bgp-peers.json"; + @Inject + private BindingToNormalizedNodeCodec codec; + @Inject + private RIB globalRib; + @Inject + private BGPOpenConfigMappingService mappingService; + @Inject + private SchemaService schemaService; + @Inject + private BGPPeerRegistry peerRegistry; + + @Override + protected BgpNeighbors create() { + final BgpNeighbors neighbors = readNeighbours(); + for (final Neighbor neighbor : neighbors.getNeighbor()) { + if (isApplicationPeer(neighbor)) { + LOG.trace("Starting AppPeer for {}", neighbor); + new AppPeer().start(globalRib, neighbor, mappingService, null); + } else { + LOG.trace("Starting BgpPeer for {}", neighbor); + new BgpPeer(null, peerRegistry).start(globalRib, neighbor, mappingService, null); + } + } + LOG.debug("BgpNeighbours initialized: {}", neighbors); + return neighbors; + } + + private Neighbors readNeighbours() { + LOG.debug("Reading BGP neighbours from {}", PEERS_CFG); + final InputStream resourceStream = this.getClass().getResourceAsStream(PEERS_CFG); + checkState(resourceStream != null, "Resource %s not found", PEERS_CFG); + + final InstanceIdentifier bgpII = InstanceIdentifier.create(NetworkInstances.class) + .child(NetworkInstance.class, new NetworkInstanceKey("dummy-value")).child(Protocols.class) + .child(Protocol.class, new ProtocolKey(BGP.class, "dummy-value")).augmentation(Protocol1.class) + .child(Bgp.class); + final InstanceIdentifier neighborsII = bgpII.child(Neighbors.class); + + final YangInstanceIdentifier neighborsYII = codec.toYangInstanceIdentifier(neighborsII); + final SchemaContext schemaContext = schemaService.getGlobalContext(); + final Optional parentNode = findDataNodeContainer(schemaContext, bgpII); + final ContainerNode parentContainer = readContainerEntryJson(schemaContext, resourceStream, + (SchemaNode) parentNode.get(), + (YangInstanceIdentifier.NodeIdentifier) neighborsYII.getLastPathArgument()); + final NormalizedNode neighborsContainer = parentContainer.getValue().iterator().next(); + + final Map.Entry, DataObject> entry = codec.fromNormalizedNode(neighborsYII, neighborsContainer); + checkNotNull(entry, "Failed to deserialize neighbours configuration at %s", PEERS_CFG); + return (Neighbors) entry.getValue(); + } + + private static boolean isApplicationPeer(@Nonnull final Neighbor neighbor) { + return java.util.Optional.of(neighbor.getConfig()) + .map(config -> config.getAugmentation(Config2.class)) + .map(BgpNeighborPeerGroupConfig::getPeerGroup) + .map(APPLICATION_PEER_GROUP_NAME::equals) + .orElse(false); + } +} diff --git a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java index 0204e1f5c..bf6725a09 100644 --- a/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java +++ b/infra/bgp-distribution/src/main/java/io/fd/honeycomb/infra/bgp/distro/Main.java @@ -28,6 +28,7 @@ import com.google.inject.ProvisionException; import io.fd.honeycomb.infra.bgp.BgpConfiguration; import io.fd.honeycomb.infra.bgp.BgpServerProvider; import java.util.Set; +import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighbors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,6 +55,8 @@ public final class Main { if (bgpAttributes.isBgpEnabled()) { LOG.info("Starting BGP"); injector.getInstance(BgpServerProvider.BgpServer.class); + LOG.info("Initializing BgpNeighbours"); + injector.getInstance(BgpNeighbors.class); LOG.info("BGP started successfully!"); } -- cgit 1.2.3-korg