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 java.io.BufferedWriter;
19 import java.io.ByteArrayInputStream;
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;
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;
35 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
36 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
39 * Factory for creating the default device from the XML stored in classpath.
41 * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
43 public class DefaultPmListFactory {
45 private static final Logger LOG = LoggerFactory.getLogger(DefaultPmListFactory.class);
48 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
51 * @return {@link CurrentPmlist}
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;
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));
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");
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");
81 LOG.info("xml file not existed at : '{}'", pm_data_config.getAbsolutePath());
87 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
90 * @return {@link CurrentPmlist}
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;
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"));
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");
112 result = (CurrentPmlist) dataObject.get();
113 } catch (IllegalStateException e) {
114 LOG.warn("Could not transform the input pm into normalized nodes");
117 LOG.info("device data config string is null!");
123 * create an XML String from an instance of {@link CurrentPmlist}.
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));
135 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
136 .createWithDataStoreUtil(dataStoreContextUtil);
137 Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(pm_list, CurrentPmlist.class,
138 createWithDataStoreUtil.dataContainer());
140 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
141 writer.write(writerFromDataObject.toString());
143 } catch (IOException e) {
144 LOG.error("Bufferwriter error ");
146 LOG.info("device xml : {}", writerFromDataObject.toString());