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.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;
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 DefaultDeviceFactory {
45 private static final Logger LOG = LoggerFactory.getLogger(DefaultDeviceFactory.class);
48 * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
51 * @return {@link OrgOpenroadmDevice}
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;
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));
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");
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");
82 LOG.info("xml file not existed at : '{}'", device_data_config.getAbsolutePath());
88 * Returns a new instance of {@link OrgOpenroadmDevice} from the loaded XML
91 * @return {@link OrgOpenroadmDevice}
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;
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"));
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");
114 result = (OrgOpenroadmDevice) dataObject.get();
115 } catch (IllegalStateException e) {
116 LOG.warn("Could not transform the input OrgOpenroadmDevice into normalized nodes");
120 LOG.info("device data config string is null!");
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));
134 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
135 .createWithDataStoreUtil(dataStoreContextUtil);
136 Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(device, OrgOpenroadmDevice.class,
137 createWithDataStoreUtil.dataContainer());
139 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
140 writer.write(writerFromDataObject.toString());
142 } catch (IOException e) {
143 LOG.error("Bufferwriter error ");
145 LOG.info("device xml : {}", writerFromDataObject.toString());