Honeynode test tool
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / test / java / io / fd / honeycomb / transportpce / device / test / DeviceOperToConfig.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.transportpce.device.test;
17
18 import io.fd.honeycomb.transportpce.device.tools.DefaultDeviceFactory;
19 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
20 import io.fd.honeycomb.transportpce.test.common.DataStoreContextImpl;
21
22 import java.io.File;
23 import java.io.StringReader;
24 import java.io.StringWriter;
25
26 import javax.xml.transform.Source;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.stream.StreamResult;
31 import javax.xml.transform.stream.StreamSource;
32
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Converts XML Data operation file  to Configuration file.
39  *
40  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
41  */
42 public class DeviceOperToConfig {
43     private static final Logger LOG = LoggerFactory.getLogger(DeviceOperToConfig.class);
44     private static final String DEVICE_OPER = "src/main/resources/honeycomb-minimal-resources/config/device/oper-ROADMA.xml";
45     private static final String CONFIG_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/config.xsl";
46     private static final String DEVICE_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/OperToConfig.xsl";
47     private static final String NAMESPACE_TRIMER_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/NamespaceTrimmer.xslt";
48
49
50     /**
51      * Convert data xml to config xml device.
52      *
53      * @return String result
54      */
55     public static String operToConfig() {
56         String result =null;
57         LOG.info("process to transform xml file ");
58         TransformerFactory factory = TransformerFactory.newInstance();
59         Source xslt = new StreamSource(new File(DEVICE_XSL));
60         Transformer transformer;
61         Source text;
62         StringWriter tmpwriter = new StringWriter();
63         StringReader reader;
64         try {
65             LOG.info("transforming xml data to config device ...");
66             transformer = factory.newTransformer(xslt);
67             text = new StreamSource(new File(DEVICE_OPER));
68             transformer.transform(text, new StreamResult(tmpwriter));
69             LOG.info("removing namespace ...");
70             xslt = new StreamSource(new File(NAMESPACE_TRIMER_XSL));
71             transformer = factory.newTransformer(xslt);
72             reader = new StringReader(tmpwriter.toString());
73             StringWriter device_config = new StringWriter();
74             text = new StreamSource(reader);
75             transformer.transform(text, new StreamResult(device_config));
76             //LOG.info("xml transformed : {}\n", device_config.toString());
77             result = device_config.toString();
78         } catch (TransformerException e) {
79             LOG.error("Transformer failed ");
80         }
81         return result;
82     }
83
84     public static String getDeviceFromXML(String xml) {
85         String config_result =null;
86         LOG.info("process to transform xml file to config data");
87         TransformerFactory factory = TransformerFactory.newInstance();
88         Source xslt = new StreamSource(new File(CONFIG_XSL));
89         Transformer transformer;
90         Source text;
91         StringWriter device_config = new StringWriter();
92         try {
93             LOG.info("transforming xml string to config device ...");
94             transformer = factory.newTransformer(xslt);
95             text = new StreamSource(new StringReader(xml));
96             transformer.transform(text, new StreamResult(device_config));
97             config_result = device_config.toString();
98             LOG.info(config_result);
99         } catch (TransformerException e) {
100             LOG.error("Transformer failed ");
101         }
102         return config_result;
103     }
104
105     public static void createDeviceFromString(String xml) throws NullPointerException {
106         OrgOpenroadmDevice result = null;
107         LOG.info("Parameterized string is : {}", xml);
108         DataStoreContext dataStoreContextUtil = new DataStoreContextImpl();
109         DefaultDeviceFactory defaultDeviceFactory = new DefaultDeviceFactory();
110         result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,xml);
111         if (result != null) {
112             LOG.info("result info : {}", result.getInfo().getNodeId());
113         } else {
114             LOG.error("failed !");
115         }
116
117     }
118
119     public static void main(String[] args) {
120         String xml = DeviceOperToConfig.getDeviceFromXML(DeviceOperToConfig.operToConfig());
121         DeviceOperToConfig.createDeviceFromString(xml);
122     }
123 }