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;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.platform.rev180130.platform.component.top.Components;
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 * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com,
42 * christophe.betoule@orange.com )
44 public class DefaultOcPlatformFactory {
46 private static final Logger LOG = LoggerFactory.getLogger(DefaultOcPlatformFactory.class);
49 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
52 * @return {@link CurrentPmlist}
54 public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, File oc_platform_data) {
55 Components result = null;
56 if (oc_platform_data.exists()) {
57 String oper = oc_platform_data.getName();
58 LOG.info("file '{}' exists at location : {}", oper, oc_platform_data.getAbsolutePath());
59 InputStream targetStream;
61 targetStream = new FileInputStream(oc_platform_data);
62 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
63 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
64 .transformIntoNormalizedNode(targetStream);
65 if (!transformIntoNormalizedNode.isPresent()) {
66 throw new IllegalStateException(
67 String.format("Could not transform the input %s into normalized nodes", oper));
69 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
70 .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
71 if (!dataObject.isPresent()) {
72 LOG.warn("Could not transform normalized nodes into data object");
75 result = (Components) dataObject.get();
76 } catch (FileNotFoundException e) {
77 LOG.error("File not found : {} at {}", e.getMessage(), e.getLocalizedMessage());
78 } catch (IllegalStateException e) {
79 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
83 LOG.info("xml file not existed at : '{}'", oc_platform_data.getAbsolutePath());
89 * Returns a new instance of {@link CurrentPmlist} from the loaded XML stored in
92 * @return {@link CurrentPmlist}
94 public Components createDefaultComponents(DataStoreContext dataStoreContextUtil, String oc_platform_data) {
95 Components result = null;
96 if (oc_platform_data != null) {
97 LOG.info("openconfig platform data string is ok ");
98 LOG.info(oc_platform_data);
99 InputStream targetStream;
101 targetStream = new ByteArrayInputStream(oc_platform_data.getBytes());
102 LOG.info("targetStream = {}", targetStream);
103 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
104 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
105 .transformIntoNormalizedNode(targetStream);
106 if (!transformIntoNormalizedNode.isPresent()) {
107 throw new IllegalStateException(
108 String.format("Could not transform the input into normalized nodes"));
110 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
111 .getDataObject(transformIntoNormalizedNode.get(), Components.QNAME);
112 if (!dataObject.isPresent()) {
113 LOG.warn("Could not transform normalized nodes into data object");
115 result = (Components) dataObject.get();
116 } catch (IllegalStateException e) {
117 LOG.warn("Could not transform the input OCPlatform into normalized nodes");
121 LOG.info("openconfig platform data string is null!");
127 * create an XML String from an instance of {@link CurrentPmlist}.
130 public void createXMLFromComponents(DataStoreContext dataStoreContextUtil, Components components, String output) {
131 if (components != null) {
132 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
133 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
134 .toNormalizedNodes(components, Components.class);
135 if (!transformIntoNormalizedNode.isPresent()) {
136 throw new IllegalStateException(
137 String.format("Could not transform the input %s into normalized nodes", components));
139 XMLDataObjectConverter createWithDataStoreUtil = XMLDataObjectConverter
140 .createWithDataStoreUtil(dataStoreContextUtil);
141 Writer writerFromDataObject = createWithDataStoreUtil.writerFromDataObject(components, Components.class,
142 createWithDataStoreUtil.dataContainer());
144 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
145 writer.write(writerFromDataObject.toString());
147 } catch (IOException e) {
148 LOG.error("Bufferwriter error ");
150 LOG.info("openconf platform xml : {}", writerFromDataObject.toString());