HoneyNode Java 11 support for 121 devices
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultDeviceFactory.java
@@ -15,9 +15,6 @@
  */
 package io.fd.honeycomb.transportpce.device.tools;
 
-import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
-import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
-
 import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
@@ -35,9 +32,11 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
+import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
+
 /**
- * Factory for creating the default device from the XML stored in
- * classpath.
+ * Factory for creating the default device from the XML stored in classpath.
  *
  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
  */
@@ -46,8 +45,8 @@ public class DefaultDeviceFactory {
     private static final Logger LOG = LoggerFactory.getLogger(DefaultDeviceFactory.class);
 
     /**
-     * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML stored
-     * in File.
+     * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
+     * stored in File.
      *
      * @return {@link OrgOpenroadmDevice}
      */
@@ -60,30 +59,34 @@ public class DefaultDeviceFactory {
             try {
                 targetStream = new FileInputStream(device_data_config);
                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
-                transformIntoNormalizedNode =
-                        XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
+                transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                         .transformIntoNormalizedNode(targetStream);
                 if (!transformIntoNormalizedNode.isPresent()) {
-                    throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
-                            config));
+                    throw new IllegalStateException(
+                            String.format("Could not transform the input %s into normalized nodes", config));
                 }
                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                         .getDataObject(transformIntoNormalizedNode.get(), OrgOpenroadmDevice.QNAME);
                 if (!dataObject.isPresent()) {
-                    throw new IllegalStateException("Could not transform normalized nodes into data object");
+                    LOG.warn("Could not transform normalized nodes into data object");
+                    return null;
                 }
-                result =  (OrgOpenroadmDevice) dataObject.get();
+                result = (OrgOpenroadmDevice) dataObject.get();
             } catch (FileNotFoundException e) {
                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
+            } catch (IllegalStateException e) {
+                LOG.warn("Could not transform the input OrgOpenroadmDevice into normalized nodes");
+                return null;
             }
         } else {
             LOG.info("xml file not existed at : '{}'", device_data_config.getAbsolutePath());
         }
         return result;
     }
+
     /**
-     * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML stored
-     * in String.
+     * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
+     * stored in String.
      *
      * @return {@link OrgOpenroadmDevice}
      */
@@ -91,41 +94,47 @@ public class DefaultDeviceFactory {
         OrgOpenroadmDevice result = null;
         if (device_data_config != null) {
             LOG.info("device data config string is ok ");
-            InputStream targetStream;
-            targetStream = new ByteArrayInputStream(device_data_config.getBytes());
-            Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
-            transformIntoNormalizedNode =
-                    XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
-                    .transformIntoNormalizedNode(targetStream);
-            if (!transformIntoNormalizedNode.isPresent()) {
-                throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes"));
-            }
-            Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
-                    .getDataObject(transformIntoNormalizedNode.get(), OrgOpenroadmDevice.QNAME);
-            if (!dataObject.isPresent()) {
-                throw new IllegalStateException("Could not transform normalized nodes into data object");
+            LOG.info("device data config = {}", device_data_config);
+           InputStream targetStream;
+            try {
+                targetStream = new ByteArrayInputStream(device_data_config.getBytes());
+                Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
+                transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
+                        .transformIntoNormalizedNode(targetStream);
+                if (!transformIntoNormalizedNode.isPresent()) {
+                    throw new IllegalStateException(
+                            String.format("Could not transform the input %s into normalized nodes"));
+                }
+                Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
+                        .getDataObject(transformIntoNormalizedNode.get(), OrgOpenroadmDevice.QNAME);
+                if (!dataObject.isPresent()) {
+                    LOG.warn("Could not transform normalized nodes into data object");
+                    return null;
+                }
+                result = (OrgOpenroadmDevice) dataObject.get();
+            } catch (IllegalStateException e) {
+                LOG.warn("Could not transform the input OrgOpenroadmDevice into normalized nodes");
+                return null;
             }
-            result =  (OrgOpenroadmDevice) dataObject.get();
         } else {
             LOG.info("device data config string is null!");
         }
         return result;
     }
 
-
     public void createXMLFromDevice(DataStoreContext dataStoreContextUtil, OrgOpenroadmDevice device, String output) {
         if (device != null) {
             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
-            transformIntoNormalizedNode =
-                    XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
+            transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                     .toNormalizedNodes(device, OrgOpenroadmDevice.class);
             if (!transformIntoNormalizedNode.isPresent()) {
-                throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
-                        device));
+                throw new IllegalStateException(
+                        String.format("Could not transform the input %s into normalized nodes", device));
             }
-            XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil);
-            Writer writerFromDataObject =
-                    createWithDataStoreUtil.writerFromDataObject(device, OrgOpenroadmDevice.class, createWithDataStoreUtil.dataContainer());
+            XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
+                    .createWithDataStoreUtil(dataStoreContextUtil);
+            Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(device, OrgOpenroadmDevice.class,
+                    createWithDataStoreUtil.dataContainer());
             try {
                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
                 writer.write(writerFromDataObject.toString());