2 * Copyright © 2019 Orange, 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
8 package org.opendaylight.transportpce.tapi.utils;
10 import edu.umd.cs.findbugs.annotations.NonNull;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.InputStream;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.WriteTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.transportpce.common.DataStoreContext;
20 import org.opendaylight.transportpce.common.converter.XMLDataObjectConverter;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.Network;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
23 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInputBuilder;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class TopologyDataUtils {
33 private static final Logger LOG = LoggerFactory.getLogger(TopologyDataUtils.class);
34 public static final String OPENROADM_TOPOLOGY_FILE = "src/test/resources/openroadm-topology2.xml";
35 public static final String OTN_TOPOLOGY_FILE = "src/test/resources/otn-topology.xml";
36 public static final String PORTMAPPING_FILE = "src/test/resources/portmapping-example.xml";
38 public static GetTopologyDetailsInput buildGetTopologyDetailsInput(String topoName) {
39 GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
40 builtInput.setTopologyIdOrName(topoName);
41 return builtInput.build();
44 public static <T> void writeTopologyFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file,
45 InstanceIdentifier ii) {
46 Networks networks = null;
47 File topoFile = new File(file);
48 if (topoFile.exists()) {
49 String fileName = topoFile.getName();
50 InputStream targetStream;
52 targetStream = new FileInputStream(topoFile);
53 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
54 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
55 .transformIntoNormalizedNode(targetStream);
56 if (!transformIntoNormalizedNode.isPresent()) {
57 throw new IllegalStateException(String.format(
58 "Could not transform the input %s into normalized nodes", fileName));
60 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
61 .getDataObject(transformIntoNormalizedNode.get(), Networks.QNAME);
62 if (!dataObject.isPresent()) {
63 throw new IllegalStateException("Could not transform normalized nodes into data object");
65 networks = (Networks) dataObject.get();
67 } catch (FileNotFoundException e) {
68 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
71 LOG.error("xml file {} not found at {}", topoFile.getName(), topoFile.getAbsolutePath());
73 writeTransaction(dataStoreContextUtil.getDataBroker(), ii, networks.getNetwork().get(0));
74 LOG.info("extraction from {} stored with success in datastore", topoFile.getName());
77 @SuppressWarnings("unchecked")
78 private static boolean writeTransaction(DataBroker dataBroker, InstanceIdentifier instanceIdentifier,
81 WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
82 transaction.put(LogicalDatastoreType.CONFIGURATION, instanceIdentifier, object);
87 public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil) {
88 Network result = null;
89 File portmappingFile = new File(PORTMAPPING_FILE);
90 if (portmappingFile.exists()) {
91 String fileName = portmappingFile.getName();
92 InputStream targetStream;
94 targetStream = new FileInputStream(portmappingFile);
95 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
96 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
97 .transformIntoNormalizedNode(targetStream);
98 if (!transformIntoNormalizedNode.isPresent()) {
99 throw new IllegalStateException(String.format(
100 "Could not transform the input %s into normalized nodes", fileName));
102 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
103 .getDataObject(transformIntoNormalizedNode.get(), Network.QNAME);
104 if (!dataObject.isPresent()) {
105 throw new IllegalStateException("Could not transform normalized nodes into data object");
107 result = (Network) dataObject.get();
109 } catch (FileNotFoundException e) {
110 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
113 LOG.error("xml file {} not found at {}", portmappingFile.getName(), portmappingFile.getAbsolutePath());
115 InstanceIdentifier<Network> portmappingIID = InstanceIdentifier.builder(Network.class).build();
116 writeTransaction(dataStoreContextUtil.getDataBroker(), portmappingIID, result);
117 LOG.info("portmapping-example stored with success in datastore");
120 private TopologyDataUtils() {