2 * Copyright (c) 2017 AT&T Intellectual Property. 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.bgpcep.config.loader.topology;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
15 import java.util.concurrent.ExecutionException;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.bgpcep.config.loader.spi.ConfigFileProcessor;
18 import org.opendaylight.bgpcep.config.loader.spi.ConfigLoader;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
26 import org.opendaylight.yangtools.concepts.AbstractRegistration;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class NetworkTopologyConfigFileProcessor implements ConfigFileProcessor, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(NetworkTopologyConfigFileProcessor.class);
43 private static final SchemaPath TOPOLOGY_SCHEMA_PATH = SchemaPath.create(true, NetworkTopology.QNAME);
44 private final BindingNormalizedNodeSerializer bindingSerializer;
45 private AbstractRegistration registration;
46 private final YangInstanceIdentifier topologyYii;
47 private static final InstanceIdentifier<Topology> TOPOLOGY_IID =
48 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class);
49 private final DataBroker dataBroker;
50 private final ConfigLoader configLoader;
52 public NetworkTopologyConfigFileProcessor(final ConfigLoader configLoader, final DataBroker dataBroker) {
53 requireNonNull(configLoader);
54 this.dataBroker = requireNonNull(dataBroker);
55 this.configLoader = requireNonNull(configLoader);
56 this.bindingSerializer = configLoader.getBindingNormalizedNodeSerializer();
57 this.topologyYii = this.bindingSerializer.toYangInstanceIdentifier(TOPOLOGY_IID);
60 public synchronized void init() {
61 this.registration = this.configLoader.registerConfigFile(this);
62 LOG.info("Network Topology Loader service initiated");
66 public synchronized void close() {
67 if (this.registration != null) {
68 this.registration.close();
69 this.registration = null;
75 public SchemaPath getSchemaPath() {
76 return TOPOLOGY_SCHEMA_PATH;
80 public synchronized void loadConfiguration(final NormalizedNode<?, ?> dto) {
81 final ContainerNode networkTopologyContainer = (ContainerNode) dto;
82 final MapNode topologyList = (MapNode) networkTopologyContainer.getChild(
83 this.topologyYii.getLastPathArgument()).get();
84 final Collection<MapEntryNode> networkTopology = topologyList.getValue();
85 if (networkTopology.isEmpty()) {
88 final WriteTransaction wtx = this.dataBroker.newWriteOnlyTransaction();
90 for (final MapEntryNode topologyEntry : networkTopology) {
91 final Map.Entry<InstanceIdentifier<?>, DataObject> bi =
92 this.bindingSerializer.fromNormalizedNode(this.topologyYii, topologyEntry);
94 processTopology((Topology) bi.getValue(), wtx);
99 } catch (final ExecutionException | InterruptedException e) {
100 LOG.warn("Failed to create Network Topologies", e);
104 private static void processTopology(final Topology topology, final WriteTransaction wtx) {
105 LOG.info("Storing Topology {}", topology);
106 final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
107 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology.getKey());
108 wtx.merge(LogicalDatastoreType.CONFIGURATION, topologyIIdKeyed, topology, true);