2 * Copyright (c) 2018 Orange and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.transportpce.device.tools;
18 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
19 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
21 import java.io.BufferedWriter;
22 import java.io.ByteArrayInputStream;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.Writer;
30 import java.util.Optional;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Factory for creating the default device from the XML stored in
42 * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
44 public class DefaultPmListFactory {
46 private static final Logger LOG = LoggerFactory.getLogger(DefaultPmListFactory.class);
49 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored
52 * @return {@link CurrentPmlist}
54 public CurrentPmlist createDefaultPmList(DataStoreContext dataStoreContextUtil, File pm_data_config) {
55 CurrentPmlist result = null;
56 if (pm_data_config.exists()) {
57 String config = pm_data_config.getName();
58 LOG.info("file '{}' exists at location : {}", config, pm_data_config.getAbsolutePath());
59 InputStream targetStream;
61 targetStream = new FileInputStream(pm_data_config);
62 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
63 transformIntoNormalizedNode =
64 XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
65 .transformIntoNormalizedNode(targetStream);
66 if (!transformIntoNormalizedNode.isPresent()) {
67 throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
70 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
71 .getDataObject(transformIntoNormalizedNode.get(), CurrentPmlist.QNAME);
72 if (!dataObject.isPresent()) {
73 throw new IllegalStateException("Could not transform normalized nodes into data object");
75 result = (CurrentPmlist) dataObject.get();
76 } catch (FileNotFoundException | IllegalStateException e) {
77 LOG.error("File not found : {} at {}", e);
80 LOG.info("xml file not existed at : '{}'", pm_data_config.getAbsolutePath());
85 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored
88 * @return {@link CurrentPmlist}
90 public CurrentPmlist createDefaultPmList(DataStoreContext dataStoreContextUtil, String pm_data_config) {
91 CurrentPmlist result = null;
92 if (pm_data_config != null) {
93 LOG.info("Pm List data config string is ok ");
94 InputStream targetStream;
95 targetStream = new ByteArrayInputStream(pm_data_config.getBytes());
96 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
97 transformIntoNormalizedNode =
98 XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
99 .transformIntoNormalizedNode(targetStream);
100 if (!transformIntoNormalizedNode.isPresent()) {
101 throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes"));
103 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
104 .getDataObject(transformIntoNormalizedNode.get(), CurrentPmlist.QNAME);
105 if (!dataObject.isPresent()) {
106 throw new IllegalStateException("Could not transform normalized nodes into data object");
108 result = (CurrentPmlist) dataObject.get();
110 LOG.info("device data config string is null!");
117 * create an XML String from an instance of {@link CurrentPmlist}.
120 public void createXMLFromPmList(DataStoreContext dataStoreContextUtil, CurrentPmlist pm_list, String output) {
121 if (pm_list != null) {
122 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
123 transformIntoNormalizedNode =
124 XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
125 .toNormalizedNodes(pm_list, CurrentPmlist.class);
126 if (!transformIntoNormalizedNode.isPresent()) {
127 throw new IllegalStateException(String.format("Could not transform the input %s into normalized nodes",
130 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil);
131 Writer writerFromDataObject =
132 createWithDataStoreUtil.writerFromDataObject(pm_list, CurrentPmlist.class, createWithDataStoreUtil.dataContainer());
134 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
135 writer.write(writerFromDataObject.toString());
137 } catch (IOException e) {
138 LOG.error("Bufferwriter error ");
140 LOG.info("device xml : {}", writerFromDataObject.toString());