Add OTN links support in TAPI topology
[transportpce.git] / tapi / src / test / java / org / opendaylight / transportpce / tapi / utils / TopologyDataUtils.java
1 /*
2  * Copyright © 2019 Orange, Inc. and others.  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.transportpce.tapi.utils;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileNotFoundException;
14 import java.io.InputStream;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.transportpce.test.DataStoreContext;
22 import org.opendaylight.transportpce.test.converter.XMLDataObjectConverter;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.Network;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInputBuilder;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public final class TopologyDataUtils {
34
35     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataUtils.class);
36     public static final String OPENROADM_TOPOLOGY_FILE = "src/test/resources/openroadm-topology2.xml";
37     public static final String OTN_TOPOLOGY_FILE = "src/test/resources/otn-topology.xml";
38     public static final String OTN_TOPOLOGY_WITH_OTN_LINKS_FILE = "src/test/resources/otn-topology-with-otn-links.xml";
39     public static final String PORTMAPPING_FILE = "src/test/resources/portmapping-example.xml";
40
41     public static GetTopologyDetailsInput buildGetTopologyDetailsInput(String topoName) {
42         GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
43         builtInput.setTopologyIdOrName(topoName);
44         return builtInput.build();
45     }
46
47     public static <T> void writeTopologyFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file,
48         InstanceIdentifier ii) throws InterruptedException, ExecutionException {
49         Networks networks = null;
50         File topoFile = new File(file);
51         if (topoFile.exists()) {
52             String fileName = topoFile.getName();
53             InputStream targetStream;
54             try {
55                 targetStream = new FileInputStream(topoFile);
56                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
57                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
58                     .transformIntoNormalizedNode(targetStream);
59                 if (!transformIntoNormalizedNode.isPresent()) {
60                     throw new IllegalStateException(String.format(
61                         "Could not transform the input %s into normalized nodes", fileName));
62                 }
63                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
64                     .getDataObject(transformIntoNormalizedNode.get(), Networks.QNAME);
65                 if (!dataObject.isPresent()) {
66                     throw new IllegalStateException("Could not transform normalized nodes into data object");
67                 } else {
68                     networks = (Networks) dataObject.get();
69                 }
70             } catch (FileNotFoundException e) {
71                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
72             }
73         } else {
74             LOG.error("xml file {} not found at {}", topoFile.getName(), topoFile.getAbsolutePath());
75         }
76         FluentFuture<? extends CommitInfo> commitFuture = writeTransaction(dataStoreContextUtil.getDataBroker(), ii,
77                 networks.nonnullNetwork().values().stream().findFirst().get());
78         commitFuture.get();
79         LOG.info("extraction from {} stored with success in datastore", topoFile.getName());
80     }
81
82     @SuppressWarnings("unchecked")
83     private static FluentFuture<? extends CommitInfo> writeTransaction(DataBroker dataBroker,
84         InstanceIdentifier instanceIdentifier, DataObject object) {
85         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
86         transaction.put(LogicalDatastoreType.CONFIGURATION, instanceIdentifier, object);
87         return transaction.commit();
88     }
89
90     public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil)
91         throws InterruptedException, ExecutionException {
92         Network result = null;
93         File portmappingFile = new File(PORTMAPPING_FILE);
94         if (portmappingFile.exists()) {
95             String fileName = portmappingFile.getName();
96             InputStream targetStream;
97             try {
98                 targetStream = new FileInputStream(portmappingFile);
99                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
100                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
101                     .transformIntoNormalizedNode(targetStream);
102                 if (!transformIntoNormalizedNode.isPresent()) {
103                     throw new IllegalStateException(String.format(
104                         "Could not transform the input %s into normalized nodes", fileName));
105                 }
106                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
107                     .getDataObject(transformIntoNormalizedNode.get(), Network.QNAME);
108                 if (!dataObject.isPresent()) {
109                     throw new IllegalStateException("Could not transform normalized nodes into data object");
110                 } else {
111                     result = (Network) dataObject.get();
112                 }
113             } catch (FileNotFoundException e) {
114                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
115             }
116         } else {
117             LOG.error("xml file {} not found at {}", portmappingFile.getName(), portmappingFile.getAbsolutePath());
118         }
119         InstanceIdentifier<Network> portmappingIID = InstanceIdentifier.builder(Network.class).build();
120         FluentFuture<? extends CommitInfo> writeTransaction = writeTransaction(dataStoreContextUtil.getDataBroker(),
121             portmappingIID, result);
122         writeTransaction.get();
123         LOG.info("portmapping-example stored with success in datastore");
124     }
125
126     private TopologyDataUtils() {
127     }
128
129 }