fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 1.2.1 / 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 java.io.File;
20 import java.io.StringReader;
21 import java.io.StringWriter;
22
23 import javax.xml.transform.Source;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.stream.StreamResult;
28 import javax.xml.transform.stream.StreamSource;
29
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import io.fd.honeycomb.transportpce.device.tools.DefaultDeviceFactory;
35 import io.fd.honeycomb.transportpce.device.tools.ExtractXMLTag;
36 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
37 import io.fd.honeycomb.transportpce.test.common.DataStoreContextImpl;
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
54     public String config_device;
55     public OrgOpenroadmDevice oper_device;
56     private DataStoreContext dataStoreContextUtil;
57     private DefaultDeviceFactory defaultDeviceFactory;
58     private ClassLoader classLoader;
59     private Boolean register;
60
61     @InjectConfig("persisted-config-path")
62     public String peristConfigPath;
63
64     @InjectConfig("netconf-initial-config-xml")
65     public String DEVICE_DATA_SAMPLE_OPER_XML;
66
67     public DeviceConfiguration() {
68         classLoader = Thread.currentThread().getContextClassLoader();
69         dataStoreContextUtil = new DataStoreContextImpl();
70         defaultDeviceFactory = new DefaultDeviceFactory();
71         register = false;
72     }
73
74     public String getConfigDevice() {
75         return operToConfig();
76     }
77
78     public String getPeristConfigPath() {
79         return peristConfigPath;
80     }
81
82     public String getNetconfInitialConfigXml() {
83         return DEVICE_DATA_SAMPLE_OPER_XML;
84     }
85
86     public Boolean getRegister() {
87         return register;
88     }
89
90     public void setRegister(Boolean reg) {
91         this.register = reg;
92     }
93
94     public OrgOpenroadmDevice getDataDevice() {
95         OrgOpenroadmDevice result = null;
96         File device_data = new File(classLoader.getResource(DEVICE_DATA_SAMPLE_OPER_XML).getFile());
97         result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil, device_data);
98         if (result != null) {
99             LOG.info("result info : {}", result.getInfo().getNodeId());
100         } else {
101             LOG.warn("Failed to get Device data !");
102         }
103         return result;
104     }
105
106     public OrgOpenroadmDevice getDeviceFromXML(String xml) {
107         String config_result = null;
108         LOG.info("process to transform xml file to config data");
109         TransformerFactory factory = TransformerFactory.newInstance();
110         Source xslt = new StreamSource(new File(classLoader.getResource(CONFIG_XSL).getFile()));
111         Transformer transformer;
112         Source text;
113         StringWriter device_config = new StringWriter();
114         OrgOpenroadmDevice result = null;
115         try {
116             LOG.info("transforming xml string to config device ...");
117             transformer = factory.newTransformer(xslt);
118             text = new StreamSource(new StringReader(xml));
119             transformer.transform(text, new StreamResult(device_config));
120             config_result = device_config.toString();
121             // LOG.info("config_result: {}",config_result);
122             result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil, config_result);
123             if (result != null) {
124                 LOG.info("result info : {}", result.getInfo().getNodeId());
125             }
126         } catch (TransformerException e) {
127             LOG.error("Transformer failed ");
128         }
129
130         return result;
131     }
132
133     /**
134      * Convert data xml to config xml device.
135      *
136      * @return String result
137      */
138     public String operToConfig() {
139         String result = null;
140         LOG.info("process to transform xml file {}",DEVICE_DATA_SAMPLE_OPER_XML);
141         TransformerFactory factory = TransformerFactory.newInstance();
142         Source xslt = new StreamSource(new File(classLoader.getResource(DEVICE_XSL).getFile()));
143         StringWriter tmpwriter = new StringWriter();
144         try {
145             LOG.info("transforming xml data to config device ...");
146             Transformer transformer = factory.newTransformer(xslt);
147             String extract_data = ExtractXMLTag.extractTagElement(DEVICE_DATA_SAMPLE_OPER_XML, "org-openroadm-device",
148                     "http://org/openroadm/device");
149             Source text = new StreamSource(new StringReader(extract_data));
150             LOG.info("text avant transform = {}", text.toString());
151             transformer.transform(text, new StreamResult(tmpwriter));
152             result = tmpwriter.toString();
153         } catch (TransformerException e) {
154             LOG.error("Transformer failed ", e);
155         }
156         return result;
157     }
158 }