Migrate to MD-SAL APIs
[bgpcep.git] / config-loader / topology-config-loader / src / main / java / org / opendaylight / bgpcep / config / loader / topology / NetworkTopologyConfigFileProcessor.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. 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 package org.opendaylight.bgpcep.config.loader.topology;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.bgpcep.config.loader.spi.ConfigFileProcessor;
16 import org.opendaylight.bgpcep.config.loader.spi.ConfigLoader;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yangtools.concepts.AbstractRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class NetworkTopologyConfigFileProcessor implements ConfigFileProcessor, AutoCloseable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(NetworkTopologyConfigFileProcessor.class);
40
41     private static final SchemaPath TOPOLOGY_SCHEMA_PATH = SchemaPath.create(true, NetworkTopology.QNAME);
42     private final BindingNormalizedNodeSerializer bindingSerializer;
43     private AbstractRegistration registration;
44     private final YangInstanceIdentifier topologyYii;
45     private static final InstanceIdentifier<Topology> TOPOLOGY_IID =
46             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class);
47     private final DataBroker dataBroker;
48     private final ConfigLoader configLoader;
49
50     public NetworkTopologyConfigFileProcessor(final ConfigLoader configLoader, final DataBroker dataBroker) {
51         requireNonNull(configLoader);
52         this.dataBroker = requireNonNull(dataBroker);
53         this.configLoader = requireNonNull(configLoader);
54         this.bindingSerializer = configLoader.getBindingNormalizedNodeSerializer();
55         this.topologyYii = this.bindingSerializer.toYangInstanceIdentifier(TOPOLOGY_IID);
56     }
57
58     public synchronized void init() {
59         this.registration = this.configLoader.registerConfigFile(this);
60         LOG.info("Network Topology Loader service initiated");
61     }
62
63     @Override
64     public synchronized void close() {
65         if (this.registration != null) {
66             this.registration.close();
67             this.registration = null;
68         }
69     }
70
71     @Override
72     public SchemaPath getSchemaPath() {
73         return TOPOLOGY_SCHEMA_PATH;
74     }
75
76     @Override
77     public synchronized void loadConfiguration(final NormalizedNode<?, ?> dto) {
78         final ContainerNode networkTopologyContainer = (ContainerNode) dto;
79         final MapNode topologyList = (MapNode) networkTopologyContainer.getChild(
80                 this.topologyYii.getLastPathArgument()).get();
81         final Collection<MapEntryNode> networkTopology = topologyList.getValue();
82         if (networkTopology.isEmpty()) {
83             return;
84         }
85         final WriteTransaction wtx = this.dataBroker.newWriteOnlyTransaction();
86
87         for (final MapEntryNode topologyEntry : networkTopology) {
88             final Map.Entry<InstanceIdentifier<?>, DataObject> bi =
89                     this.bindingSerializer.fromNormalizedNode(this.topologyYii, topologyEntry);
90             if (bi != null) {
91                 processTopology((Topology) bi.getValue(), wtx);
92             }
93         }
94         try {
95             wtx.commit().get();
96         } catch (final ExecutionException | InterruptedException e) {
97             LOG.warn("Failed to create Network Topologies", e);
98         }
99     }
100
101     private static void processTopology(final Topology topology, final WriteTransaction wtx) {
102         LOG.info("Storing Topology {}", topology);
103         final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
104                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, topology.key());
105         wtx.merge(LogicalDatastoreType.CONFIGURATION, topologyIIdKeyed, topology, true);
106     }
107 }