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.openconfig.net.yang.platform.rev180130.platform.component.top.Components;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev181019.CurrentPmList;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import io.fd.honeycomb.transportpce.binding.converter.XMLDataObjectConverter;
37 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
40 * Factory for creating the default device from the XML stored in classpath.
42 * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com,
43 * christophe.betoule@orange.com )
45 public class DefaultOcPlatformFactory {
47 private static final Logger LOG = LoggerFactory.getLogger(DefaultOcPlatformFactory.class);
50 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
53 * @return {@link CurrentPmlist}
55 public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, File oc_platform_data) {
56 Components result = null;
57 if (oc_platform_data.exists()) {
58 String oper = oc_platform_data.getName();
59 LOG.info("file '{}' exists at location : {}", oper, oc_platform_data.getAbsolutePath());
60 InputStream targetStream;
62 targetStream = new FileInputStream(oc_platform_data);
63 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
64 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
65 .transformIntoNormalizedNode(targetStream);
66 if (!transformIntoNormalizedNode.isPresent()) {
67 throw new IllegalStateException(
68 String.format("Could not transform the input %s into normalized nodes", oper));
70 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
71 .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
72 if (!dataObject.isPresent()) {
73 LOG.warn("Could not transform normalized nodes into data object");
76 result = (Components) dataObject.get();
77 } catch (FileNotFoundException e) {
78 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
79 } catch (IllegalStateException e) {
80 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
84 LOG.info("xml file not existed at : '{}'", oc_platform_data.getAbsolutePath());
90 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
93 * @return {@link CurrentPmlist}
95 public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, String oc_platform_data) {
96 Components result = null;
97 if (oc_platform_data != null) {
98 LOG.info("openconfig platform data string is ok ");
99 LOG.info(oc_platform_data);
100 InputStream targetStream;
102 targetStream = new ByteArrayInputStream(oc_platform_data.getBytes());
103 LOG.info("targetStream = {}", targetStream);
104 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
105 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
106 .transformIntoNormalizedNode(targetStream);
107 if (!transformIntoNormalizedNode.isPresent()) {
108 throw new IllegalStateException(
109 String.format("Could not transform the input into normalized nodes"));
111 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
112 .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
113 if (!dataObject.isPresent()) {
114 LOG.warn("Could not transform normalized nodes into data object");
116 result = (Components) dataObject.get();
117 } catch (IllegalStateException e) {
118 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
122 LOG.info("openconfig platform data string is null!");
128 * create an XML String from an instance of {@link CurrentPmlist}.
131 public void createXMLFromComponents(DataStoreContext dataStoreContextUtil, Components components, String output) {
132 if (components != null) {
133 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
134 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
135 .toNormalizedNodes(components, Components.class);
136 if (!transformIntoNormalizedNode.isPresent()) {
137 throw new IllegalStateException(
138 String.format("Could not transform the input %s into normalized nodes", components));
140 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
141 .createWithDataStoreUtil(dataStoreContextUtil);
142 Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(components, Components.class,
143 createWithDataStoreUtil.dataContainer());
145 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
146 writer.write(writerFromDataObject.toString());
148 } catch (IOException e) {
149 LOG.error("Bufferwriter error ");
151 LOG.info("openconf platform xml : {}", writerFromDataObject.toString());