d199d9068bee667892a049304ba827c01b5e855f
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / tools / DefaultPmListFactory.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.pm.rev161014.CurrentPmlist;
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 DefaultPmListFactory {
44
45     private static final Logger LOG = LoggerFactory.getLogger(DefaultPmListFactory.class);
46
47     /**
48      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
49      * File.
50      *
51      * @return {@link CurrentPmlist}
52      */
53     public CurrentPmlist createDefaultPmList(DataStoreContext dataStoreContextUtil, File pm_data_config) {
54         CurrentPmlist result = null;
55         if (pm_data_config.exists()) {
56             String config = pm_data_config.getName();
57             LOG.info("file '{}' exists at location : {}", config, pm_data_config.getAbsolutePath());
58             InputStream targetStream;
59             try {
60                 targetStream = new FileInputStream(pm_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(), CurrentPmlist.QNAME);
70                 if (!dataObject.isPresent()) {
71                     LOG.warn("Could not transform normalized nodes into data object");
72                     return null;
73                 }
74                 result = (CurrentPmlist) 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 PM into normalized nodes");
79             }
80         } else {
81             LOG.info("xml file not existed at : '{}'", pm_data_config.getAbsolutePath());
82         }
83         return result;
84     }
85
86     /**
87      * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
88      * String.
89      *
90      * @return {@link CurrentPmlist}
91      */
92     public CurrentPmlist createDefaultPmList(DataStoreContext dataStoreContextUtil, String pm_data_config) {
93         CurrentPmlist result = null;
94         if (pm_data_config != null) {
95             LOG.info("Pm List data config string is ok ");
96             InputStream targetStream;
97             try {
98                 targetStream = new ByteArrayInputStream(pm_data_config.getBytes());
99                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
100                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
101                         .transformIntoNormalizedNode(targetStream);
102                 if (!transformIntoNormalizedNode.isPresent()) {
103                     throw new IllegalStateException(
104                             String.format("Could not transform the input %s into normalized nodes"));
105                 }
106                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
107                         .getDataObject(transformIntoNormalizedNode.get(), CurrentPmlist.QNAME);
108                 if (!dataObject.isPresent()) {
109                     LOG.warn("Could not transform normalized nodes into data object");
110                     return null;
111                 }
112                 result = (CurrentPmlist) dataObject.get();
113             } catch (IllegalStateException e) {
114                 LOG.warn("Could not transform the input pm into normalized nodes");
115             }
116         } else {
117             LOG.info("device data config string is null!");
118         }
119         return result;
120     }
121
122     /**
123      * create an XML String from an instance of {@link CurrentPmlist}.
124      *
125      */
126     public void createXMLFromPmList(DataStoreContext dataStoreContextUtil, CurrentPmlist pm_list, String output) {
127         if (pm_list != null) {
128             Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
129             transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
130                     .toNormalizedNodes(pm_list, CurrentPmlist.class);
131             if (!transformIntoNormalizedNode.isPresent()) {
132                 throw new IllegalStateException(
133                         String.format("Could not transform the input %s into normalized nodes", pm_list));
134             }
135             XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
136                     .createWithDataStoreUtil(dataStoreContextUtil);
137             Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(pm_list, CurrentPmlist.class,
138                     createWithDataStoreUtil.dataContainer());
139             try {
140                 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
141                 writer.write(writerFromDataObject.toString());
142                 writer.close();
143             } catch (IOException e) {
144                 LOG.error("Bufferwriter error ");
145             }
146             LOG.info("device xml : {}", writerFromDataObject.toString());
147         }
148     }
149 }