HoneyNode Java 11 support for 221 devices
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultPmListFactory.java
index 0309cf6e9b0432801d54b74dda8f2c4816d63c24..2aff854e7cfc80c9ccc1e57590106c170a3dae76 100644 (file)
@@ -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 DefaultPmListFactory {
     private static final Logger LOG = LoggerFactory.getLogger(DefaultPmListFactory.class);
 
     /**
-     * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored
-     * in File.
+     * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
+     * File.
      *
      * @return {@link CurrentPmlist}
      */
@@ -55,35 +54,38 @@ public class DefaultPmListFactory {
         CurrentPmList result = null;
         if (pm_data_config.exists()) {
             String config = pm_data_config.getName();
-            LOG.info("file with pm '{}' exists at location : {}", config, pm_data_config.getAbsolutePath());
+            LOG.info("file '{}' exists at location : {}", config, pm_data_config.getAbsolutePath());
             InputStream targetStream;
             try {
                 targetStream = new FileInputStream(pm_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(), CurrentPmList.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 =  (CurrentPmList) dataObject.get();
-            } catch (FileNotFoundException | IllegalStateException e) {
-                LOG.error("File not found : {} at {}", e);
+                result = (CurrentPmList) 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 PM into normalized nodes");
             }
         } else {
             LOG.info("xml file not existed at : '{}'", pm_data_config.getAbsolutePath());
         }
         return result;
     }
+
     /**
-     * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored
-     * in String.
+     * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
+     * String.
      *
      * @return {@link CurrentPmlist}
      */
@@ -92,27 +94,31 @@ public class DefaultPmListFactory {
         if (pm_data_config != null) {
             LOG.info("Pm List data config string is ok ");
             InputStream targetStream;
-            targetStream = new ByteArrayInputStream(pm_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(), CurrentPmList.QNAME);
-            if (!dataObject.isPresent()) {
-                throw new IllegalStateException("Could not transform normalized nodes into data object");
+            try {
+                targetStream = new ByteArrayInputStream(pm_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(), CurrentPmList.QNAME);
+                if (!dataObject.isPresent()) {
+                    LOG.warn("Could not transform normalized nodes into data object");
+                    return null;
+                }
+                result = (CurrentPmList) dataObject.get();
+            } catch (IllegalStateException e) {
+                LOG.warn("Could not transform the input pm into normalized nodes");
             }
-            result =  (CurrentPmList) dataObject.get();
         } else {
-            LOG.info("pm data config string is null!");
+            LOG.info("device data config string is null!");
         }
         return result;
     }
 
-
     /**
      * create an XML String from an instance of {@link CurrentPmlist}.
      *
@@ -120,16 +126,16 @@ public class DefaultPmListFactory {
     public void createXMLFromPmList(DataStoreContext dataStoreContextUtil, CurrentPmList pm_list, String output) {
         if (pm_list != null) {
             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
-            transformIntoNormalizedNode =
-                    XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
+            transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
                     .toNormalizedNodes(pm_list, CurrentPmList.class);
             if (!transformIntoNormalizedNode.isPresent()) {
-                throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
-                        pm_list));
+                throw new IllegalStateException(
+                        String.format("Could not transform the input %s into normalized nodes", pm_list));
             }
-            XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil);
-            Writer writerFromDataObject =
-                    createWithDataStoreUtil.writerFromDataObject(pm_list, CurrentPmList.class, createWithDataStoreUtil.dataContainer());
+            XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
+                    .createWithDataStoreUtil(dataStoreContextUtil);
+            Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(pm_list, CurrentPmList.class,
+                    createWithDataStoreUtil.dataContainer());
             try {
                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
                 writer.write(writerFromDataObject.toString());