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.test;
18 import io.fd.honeycomb.transportpce.device.tools.DefaultDeviceFactory;
19 import io.fd.honeycomb.transportpce.device.tools.DefaultPmListFactory;
20 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
21 import io.fd.honeycomb.transportpce.test.common.DataStoreContextImpl;
24 import java.io.StringReader;
25 import java.io.StringWriter;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.stream.StreamResult;
32 import javax.xml.transform.stream.StreamSource;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev181019.CurrentPmList;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Converts XML Data operation file to Configuration file.
42 * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
44 public class DeviceOperToConfig {
45 private static final Logger LOG = LoggerFactory.getLogger(DeviceOperToConfig.class);
46 private static final String DEVICE_OPER = "src/test/resources/oper-ROADMA.xml";
47 private static final String PM_LIST_OPER = "src/test/resources/oper-ROADMA-cpm.xml";
48 private static final String CONFIG_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/config.xsl";
49 private static final String DEVICE_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/OperToConfig.xsl";
50 private static final String NAMESPACE_TRIMER_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/NamespaceTrimmer.xslt";
54 * Convert data xml to config xml device.
56 * @return String result
58 public static String operToConfig() {
60 LOG.info("process to transform xml file ");
61 TransformerFactory factory = TransformerFactory.newInstance();
62 Source xslt = new StreamSource(new File(DEVICE_XSL));
63 Transformer transformer;
65 StringWriter tmpwriter = new StringWriter();
68 LOG.info("transforming xml data to config device ...");
69 transformer = factory.newTransformer(xslt);
70 text = new StreamSource(new File(DEVICE_OPER));
71 transformer.transform(text, new StreamResult(tmpwriter));
72 LOG.info("removing namespace ...");
73 xslt = new StreamSource(new File(NAMESPACE_TRIMER_XSL));
74 transformer = factory.newTransformer(xslt);
75 reader = new StringReader(tmpwriter.toString());
76 StringWriter device_config = new StringWriter();
77 text = new StreamSource(reader);
78 transformer.transform(text, new StreamResult(device_config));
79 //LOG.info("xml transformed : {}\n", device_config.toString());
80 result = device_config.toString();
81 } catch (TransformerException e) {
82 LOG.error("Transformer failed ");
87 public static String getDeviceFromXML(String xml) {
88 String config_result =null;
89 LOG.info("process to transform xml file to config data");
90 TransformerFactory factory = TransformerFactory.newInstance();
91 Source xslt = new StreamSource(new File(CONFIG_XSL));
92 Transformer transformer;
94 StringWriter device_config = new StringWriter();
96 LOG.info("transforming xml string to config device ...");
97 transformer = factory.newTransformer(xslt);
98 text = new StreamSource(new StringReader(xml));
99 transformer.transform(text, new StreamResult(device_config));
100 config_result = device_config.toString();
101 LOG.info(config_result);
102 } catch (TransformerException e) {
103 LOG.error("Transformer failed ");
105 return config_result;
108 public static void createDeviceFromString(String xml) throws NullPointerException {
109 OrgOpenroadmDevice result = null;
110 LOG.info("Parameterized string is : {}", xml);
111 DataStoreContext dataStoreContextUtil = new DataStoreContextImpl();
112 DefaultDeviceFactory defaultDeviceFactory = new DefaultDeviceFactory();
113 result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,xml);
114 if (result != null) {
115 LOG.info("result info : {}", result.getInfo().getNodeId());
117 LOG.error("failed !");
122 public static void createPmListFromFile() throws NullPointerException {
123 CurrentPmList result = null;
124 DataStoreContext dataStoreContextUtil = new DataStoreContextImpl();
125 DefaultPmListFactory defaultDeviceFactory = new DefaultPmListFactory();
126 result = defaultDeviceFactory.createDefaultPmList(dataStoreContextUtil,new File(PM_LIST_OPER));
127 if (result != null) {
128 LOG.info("result pm list size : {}", result.getCurrentPmEntry().size());
130 LOG.error("failed !");
135 public static void main(String[] args) {
136 // String xml = DeviceOperToConfig.getDeviceFromXML(DeviceOperToConfig.operToConfig());
137 // DeviceOperToConfig.createDeviceFromString(xml);
138 DeviceOperToConfig.createPmListFromFile();