e14322edde79b6e34959d0bc549359f132e24e90
[transportpce.git] / tests / honeynode221 / 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.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;
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     private static final String NAMESPACE_TRIMER_XSL = "src/main/resources/honeycomb-minimal-resources/config/device/NamespaceTrimmer.xslt";
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 ");
61         TransformerFactory factory = TransformerFactory.newInstance();
62         Source xslt = new StreamSource(new File(DEVICE_XSL));
63         Transformer transformer;
64         Source text;
65         StringWriter tmpwriter = new StringWriter();
66         StringReader reader;
67         try {
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 ");
83         }
84         return result;
85     }
86
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;
93         Source text;
94         StringWriter device_config = new StringWriter();
95         try {
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 ");
104         }
105         return config_result;
106     }
107
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());
116         } else {
117             LOG.error("failed !");
118         }
119
120     }
121
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());
129         } else {
130             LOG.error("failed !");
131         }
132
133     }
134
135     public static void main(String[] args) {
136 //        String xml = DeviceOperToConfig.getDeviceFromXML(DeviceOperToConfig.operToConfig());
137 //        DeviceOperToConfig.createDeviceFromString(xml);
138         DeviceOperToConfig.createPmListFromFile();
139
140     }
141 }