Honeynode Enhancements
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / configuration / DeviceConfiguration.java
1 /*
2  * Copyright (c) 2016 Cisco 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
17 package io.fd.honeycomb.transportpce.device.configuration;
18
19 import io.fd.honeycomb.transportpce.device.tools.DefaultDeviceFactory;
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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import net.jmob.guice.conf.core.BindConfig;
39 import net.jmob.guice.conf.core.InjectConfig;
40 import net.jmob.guice.conf.core.Syntax;
41
42 /**
43  * Class containing static configuration for honeynode-plugin module<br>
44  * <p/>
45  * Further documentation for the configuration injection can be found at:
46  * https://github.com/yyvess/gconf
47  */
48 @BindConfig(value = "honeycomb", syntax = Syntax.JSON)
49 public final class DeviceConfiguration {
50     private static final Logger LOG = LoggerFactory.getLogger(DeviceConfiguration.class);
51     private static final String DEVICE_XSL = "device/OperToConfig.xsl";
52     private static final String CONFIG_XSL = "device/config.xsl";
53     private static final String NAMESPACE_TRIMER_XSL = "device/NamespaceTrimmer.xslt";
54     //private static final String DEVICE_DATA_SAMPLE_OPER_XML = "device/sample-config-ROADM.xml";
55
56     public String config_device;
57     public OrgOpenroadmDevice oper_device;
58     private DataStoreContext dataStoreContextUtil;
59     private DefaultDeviceFactory defaultDeviceFactory;
60     private ClassLoader classLoader;
61     private Boolean register;
62
63     @InjectConfig("persisted-config-path")
64     public String peristConfigPath;
65
66     @InjectConfig("netconf-initial-config-xml")
67     public String DEVICE_DATA_SAMPLE_OPER_XML;
68
69     public DeviceConfiguration() {
70         classLoader = Thread.currentThread().getContextClassLoader();
71         dataStoreContextUtil = new DataStoreContextImpl();
72         defaultDeviceFactory = new DefaultDeviceFactory();
73         register = false;
74     }
75
76
77     public String getConfigDevice() {
78         return operToConfig();
79     }
80
81     public String getPeristConfigPath() {
82         return peristConfigPath;
83     }
84
85     public String getNetconfInitialConfigXml() {
86         return DEVICE_DATA_SAMPLE_OPER_XML;
87     }
88
89     public Boolean getRegister() {
90         return register;
91     }
92
93     public void setRegister(Boolean reg) {
94         this.register = reg;
95     }
96
97     public OrgOpenroadmDevice getDataDevice() {
98         OrgOpenroadmDevice result = null;
99         File device_data = new File(classLoader.getResource(DEVICE_DATA_SAMPLE_OPER_XML).getFile());
100         result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,device_data);
101         if (result != null) {
102             LOG.info("result info : {}", result.getInfo().getNodeId());
103         }
104         return result;
105     }
106
107     public OrgOpenroadmDevice getDeviceFromXML(String xml) {
108         String config_result =null;
109         LOG.info("process to transform xml file to config data");
110         TransformerFactory factory = TransformerFactory.newInstance();
111         Source xslt = new StreamSource(new File(classLoader.getResource(CONFIG_XSL).getFile()));
112         Transformer transformer;
113         Source text;
114         StringWriter device_config = new StringWriter();
115         OrgOpenroadmDevice result = null;
116         try {
117             LOG.info("transforming xml string to config device ...");
118             transformer = factory.newTransformer(xslt);
119             text = new StreamSource(new StringReader(xml));
120             transformer.transform(text, new StreamResult(device_config));
121             config_result = device_config.toString();
122             //LOG.info("config_result: {}",config_result);
123             result  = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,config_result);
124             if (result != null) {
125                 LOG.info("result info : {}", result.getInfo().getNodeId());
126             }
127         } catch (TransformerException e) {
128             LOG.error("Transformer failed ");
129         }
130
131         return result;
132     }
133
134
135    /**
136     * Convert data xml to config xml device.
137     * @return String result
138     */
139     public String operToConfig() {
140         String result =null;
141         LOG.info("process to transform xml file ");
142         TransformerFactory factory = TransformerFactory.newInstance();
143         Source xslt = new StreamSource(new File(classLoader.getResource(DEVICE_XSL).getFile()));
144         Transformer transformer;
145         Source text;
146         StringWriter tmpwriter = new StringWriter();
147         StringReader reader;
148         try {
149             LOG.info("transforming xml data to config device ...");
150             transformer = factory.newTransformer(xslt);
151             text = new StreamSource(new File(classLoader.getResource(DEVICE_DATA_SAMPLE_OPER_XML).getFile()));
152             transformer.transform(text, new StreamResult(tmpwriter));
153             LOG.info("removing namespace ...");
154             xslt = new StreamSource(new File(classLoader.getResource(NAMESPACE_TRIMER_XSL).getFile()));
155             transformer = factory.newTransformer(xslt);
156             reader = new StringReader(tmpwriter.toString());
157             StringWriter device_config = new StringWriter();
158             text = new StreamSource(reader);
159             transformer.transform(text, new StreamResult(device_config));
160             result = device_config.toString();
161             //LOG.info(result);
162         } catch (TransformerException e) {
163             LOG.error("Transformer failed ");
164         }
165         return result;
166     }
167 }