Move TopologyDataUtils from tapi to test-common
[transportpce.git] / test-common / src / main / java / org / opendaylight / transportpce / test / utils / TopologyDataUtils.java
similarity index 78%
rename from tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java
rename to test-common/src/main/java/org/opendaylight/transportpce/test/utils/TopologyDataUtils.java
index 0f560f3f742657a2754305036367e3952a933ce9..a5f3220f6e077f1d9bed9b6fc713209d5db5eaa8 100644 (file)
@@ -5,12 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.transportpce.tapi.utils;
+package org.opendaylight.transportpce.test.utils;
 
 import com.google.common.util.concurrent.FluentFuture;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
@@ -22,8 +22,6 @@ import org.opendaylight.transportpce.test.DataStoreContext;
 import org.opendaylight.transportpce.test.converter.XMLDataObjectConverter;
 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.Network;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
-import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
-import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInputBuilder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -33,15 +31,6 @@ import org.slf4j.LoggerFactory;
 public final class TopologyDataUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataUtils.class);
-    public static final String OPENROADM_TOPOLOGY_FILE = "src/test/resources/openroadm-topology.xml";
-    public static final String OTN_TOPOLOGY_FILE = "src/test/resources/otn-topology.xml";
-    public static final String PORTMAPPING_FILE = "src/test/resources/portmapping.xml";
-
-    public static GetTopologyDetailsInput buildGetTopologyDetailsInput(String topoName) {
-        GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
-        builtInput.setTopologyIdOrName(topoName);
-        return builtInput.build();
-    }
 
     public static <T> void writeTopologyFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file,
         InstanceIdentifier ii) throws InterruptedException, ExecutionException {
@@ -49,9 +38,7 @@ public final class TopologyDataUtils {
         File topoFile = new File(file);
         if (topoFile.exists()) {
             String fileName = topoFile.getName();
-            InputStream targetStream;
-            try {
-                targetStream = new FileInputStream(topoFile);
+            try (InputStream targetStream = new FileInputStream(topoFile)) {
                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = XMLDataObjectConverter
                         .createWithDataStoreUtil(dataStoreContextUtil).transformIntoNormalizedNode(targetStream);
                 if (!transformIntoNormalizedNode.isPresent()) {
@@ -65,12 +52,15 @@ public final class TopologyDataUtils {
                 } else {
                     networks = (Networks) dataObject.get();
                 }
-            } catch (FileNotFoundException e) {
-                LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
+            } catch (IOException e) {
+                LOG.error("An error occured while reading file {}", file, e);
             }
         } else {
             LOG.error("xml file {} not found at {}", topoFile.getName(), topoFile.getAbsolutePath());
         }
+        if (networks == null) {
+            throw new IllegalStateException("Network is null cannot write it to datastore");
+        }
         FluentFuture<? extends CommitInfo> commitFuture = writeTransaction(dataStoreContextUtil.getDataBroker(), ii,
                 networks.nonnullNetwork().values().stream().findFirst().get());
         commitFuture.get();
@@ -85,15 +75,13 @@ public final class TopologyDataUtils {
         return transaction.commit();
     }
 
-    public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil)
+    public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file)
         throws InterruptedException, ExecutionException {
         Network result = null;
-        File portmappingFile = new File(PORTMAPPING_FILE);
+        File portmappingFile = new File(file);
         if (portmappingFile.exists()) {
             String fileName = portmappingFile.getName();
-            InputStream targetStream;
-            try {
-                targetStream = new FileInputStream(portmappingFile);
+            try (InputStream targetStream = new FileInputStream(portmappingFile)) {
                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                     .transformIntoNormalizedNode(targetStream);
@@ -108,8 +96,8 @@ public final class TopologyDataUtils {
                 } else {
                     result = (Network) dataObject.get();
                 }
-            } catch (FileNotFoundException e) {
-                LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
+            } catch (IOException e) {
+                LOG.error("An error occured while reading file {}", file, e);
             }
         } else {
             LOG.error("xml file {} not found at {}", portmappingFile.getName(), portmappingFile.getAbsolutePath());