HoneyNode Java 11 support for 221 devices
[transportpce.git] / tests / honeynode / 1.2.1 / 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.device.tools.DefaultPmListFactory;
20 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
21 import io.fd.honeycomb.transportpce.test.common.DataStoreContextImpl;
22
23 import java.io.File;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26
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;
33
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Converts XML Data operation file  to Configuration file.
41  *
42  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
43  */
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
51
52
53     /**
54      * Convert data xml to config xml device.
55      *
56      * @return String result
57      */
58     public static String operToConfig() {
59         String result =null;
60         LOG.info("process to transform xml file {}",DEVICE_OPER);
61         TransformerFactory factory = TransformerFactory.newInstance();
62         Source xslt = new StreamSource(new File(DEVICE_XSL));
63         try {
64             StringWriter tmpwriter = new StringWriter();
65             LOG.info("transforming xml data to config device ...");
66             Transformer transformer = factory.newTransformer(xslt);
67             Source text = new StreamSource(new File(DEVICE_OPER));
68             transformer.transform(text, new StreamResult(tmpwriter));
69             result = tmpwriter.toString();
70         } catch (TransformerException e) {
71             LOG.error("Transformer failed ", e);
72         }
73         return result;
74     }
75
76     public static String getDeviceFromXML(String xml) {
77         String config_result =null;
78         LOG.info("process to transform xml file to config data");
79         TransformerFactory factory = TransformerFactory.newInstance();
80         Source xslt = new StreamSource(new File(CONFIG_XSL));
81         Transformer transformer;
82         Source text;
83         StringWriter device_config = new StringWriter();
84         try {
85             LOG.info("transforming xml string to config device ...");
86             transformer = factory.newTransformer(xslt);
87             text = new StreamSource(new StringReader(xml));
88             transformer.transform(text, new StreamResult(device_config));
89             config_result = device_config.toString();
90             LOG.info(config_result);
91         } catch (TransformerException e) {
92             LOG.error("Transformer failed ");
93         }
94         return config_result;
95     }
96
97     public static void createDeviceFromString(String xml) throws NullPointerException {
98         OrgOpenroadmDevice result = null;
99         LOG.info("Parameterized string is : {}", xml);
100         DataStoreContext dataStoreContextUtil = new DataStoreContextImpl();
101         DefaultDeviceFactory defaultDeviceFactory = new DefaultDeviceFactory();
102         result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,xml);
103         if (result != null) {
104             LOG.info("result info : {}", result.getInfo().getNodeId());
105         } else {
106             LOG.error("failed !");
107         }
108
109     }
110
111     public static void createPmListFromFile() throws NullPointerException {
112         CurrentPmlist result = null;
113         DataStoreContext dataStoreContextUtil = new DataStoreContextImpl();
114         DefaultPmListFactory defaultDeviceFactory = new DefaultPmListFactory();
115         result = defaultDeviceFactory.createDefaultPmList(dataStoreContextUtil,new File(PM_LIST_OPER));
116         if (result != null) {
117             LOG.info("result pm list size : {}", result.getCurrentPm().size());
118         } else {
119             LOG.error("failed !");
120         }
121
122     }
123
124     public static void main(String[] args) {
125         DeviceOperToConfig.createPmListFromFile();
126
127     }
128 }