6f3aa91b4b4c142c8a39f35fecc78cd9954b1370
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultDeviceFactory.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.fd.honeycomb.transportpce.device.tools;
17
18 import java.io.BufferedWriter;
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Writer;
27 import java.util.Optional;
28
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
36 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
37
38 /**
39  * Factory for creating the default device from the XML stored in classpath.
40  *
41  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
42  */
43 public class DefaultDeviceFactory {
44
45     private static final Logger LOG = LoggerFactory.getLogger(DefaultDeviceFactory.class);
46
47     /**
48      * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
49      * stored in File.
50      *
51      * @return {@link OrgOpenroadmDevice}
52      */
53     public OrgOpenroadmDevice createDefaultDevice(DataStoreContext dataStoreContextUtil, File device_data_config) {
54         OrgOpenroadmDevice result = null;
55         if (device_data_config.exists()) {
56             String config = device_data_config.getName();
57             LOG.info("file '{}' exists at location : {}", config, device_data_config.getAbsolutePath());
58             InputStream targetStream;
59             try {
60                 targetStream = new FileInputStream(device_data_config);
61                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
62                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
63                         .transformIntoNormalizedNode(targetStream);
64                 if (!transformIntoNormalizedNode.isPresent()) {
65                     throw new IllegalStateException(
66                             String.format("Could not transform the input %s into normalized nodes", config));
67                 }
68                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
69                         .getDataObject(transformIntoNormalizedNode.get(), OrgOpenroadmDevice.QNAME);
70                 if (!dataObject.isPresent()) {
71                     LOG.warn("Could not transform normalized nodes into data object");
72                     return null;
73                 }
74                 result = (OrgOpenroadmDevice) dataObject.get();
75             } catch (FileNotFoundException e) {
76                 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
77             } catch (IllegalStateException e) {
78                 LOG.warn("Could not transform the input OrgOpenroadmDevice into normalized nodes");
79                 return null;
80             }
81         } else {
82             LOG.info("xml file not existed at : '{}'", device_data_config.getAbsolutePath());
83         }
84         return result;
85     }
86
87     /**
88      * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
89      * stored in String.
90      *
91      * @return {@link OrgOpenroadmDevice}
92      */
93     public OrgOpenroadmDevice createDefaultDevice(DataStoreContext dataStoreContextUtil, String device_data_config) {
94         OrgOpenroadmDevice result = null;
95         if (device_data_config != null) {
96             LOG.info("device data config string is ok ");
97             LOG.info("device data config = {}", device_data_config);
98            InputStream targetStream;
99             try {
100                 targetStream = new ByteArrayInputStream(device_data_config.getBytes());
101                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
102                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
103                         .transformIntoNormalizedNode(targetStream);
104                 if (!transformIntoNormalizedNode.isPresent()) {
105                     throw new IllegalStateException(
106                             String.format("Could not transform the input %s into normalized nodes"));
107                 }
108                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
109                         .getDataObject(transformIntoNormalizedNode.get(), OrgOpenroadmDevice.QNAME);
110                 if (!dataObject.isPresent()) {
111                     LOG.warn("Could not transform normalized nodes into data object");
112                     return null;
113                 }
114                 result = (OrgOpenroadmDevice) dataObject.get();
115             } catch (IllegalStateException e) {
116                 LOG.warn("Could not transform the input OrgOpenroadmDevice into normalized nodes");
117                 return null;
118             }
119         } else {
120             LOG.info("device data config string is null!");
121         }
122         return result;
123     }
124
125     public void createXMLFromDevice(DataStoreContext dataStoreContextUtil, OrgOpenroadmDevice device, String output) {
126         if (device != null) {
127             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
128             transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
129                     .toNormalizedNodes(device, OrgOpenroadmDevice.class);
130             if (!transformIntoNormalizedNode.isPresent()) {
131                 throw new IllegalStateException(
132                         String.format("Could not transform the input %s into normalized nodes", device));
133             }
134             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
135                     .createWithDataStoreUtil(dataStoreContextUtil);
136             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(device, OrgOpenroadmDevice.class,
137                     createWithDataStoreUtil.dataContainer());
138             try {
139                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
140                 writer.write(writerFromDataObject.toString());
141                 writer.close();
142             } catch (IOException e) {
143                 LOG.error("Bufferwriter error ");
144             }
145             LOG.info("device xml : {}", writerFromDataObject.toString());
146         }
147     }
148 }