fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / configuration / OcPlatformConfiguration.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.openconfig.net.yang.platform.rev180130.platform.component.top.Components;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import io.fd.honeycomb.transportpce.device.tools.DefaultOcPlatformFactory;
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 OcPlatformConfiguration {
50     private static final Logger LOG = LoggerFactory.getLogger(OcPlatformConfiguration.class);
51     private static final String OC_PLATFORM_XSL = "device/ocplatformOperToConfig.xsl";
52
53     private DataStoreContext dataStoreContextUtil;
54     private DefaultOcPlatformFactory defaultOcPlatformFactory;
55     private ClassLoader classLoader;
56     private Boolean register;
57     public String config_components;
58     public Components oper_components;
59
60     @InjectConfig("netconf-initial-config-xml")
61     public String OC_PLATFORM_DATA_SAMPLE_OPER_XML;
62
63     public OcPlatformConfiguration() {
64         LOG.info("initializing OcPlatformConfiguration");
65         classLoader = Thread.currentThread().getContextClassLoader();
66         dataStoreContextUtil = new DataStoreContextImpl();
67         defaultOcPlatformFactory = new DefaultOcPlatformFactory();
68         register = false;
69     }
70
71     public String getNetconfInitialOcPlatformXml() {
72         return OC_PLATFORM_DATA_SAMPLE_OPER_XML;
73     }
74
75     public String getConfigComponents() {
76         return operToConfig();
77     }
78
79     public Boolean getRegister() {
80         return register;
81     }
82
83     public void setRegister(Boolean reg) {
84         this.register = reg;
85     }
86
87     public Components getDataComponents() {
88         Components result = null;
89         File components_data = new File(classLoader.getResource(OC_PLATFORM_DATA_SAMPLE_OPER_XML).getFile());
90         result = defaultOcPlatformFactory.createDefaultComponents(dataStoreContextUtil, components_data);
91         if (result != null) {
92             LOG.info("reading initial data.");
93         } else {
94             LOG.warn("failed to get OC Platform Data");
95         }
96         return result;
97     }
98
99     public Components getComponentsFromXML(String xml) {
100         String config_result = null;
101         LOG.info("process to transform xml file to config data");
102         LOG.info("xml={}", xml);
103         Components result = null;
104         LOG.info("transforming xml string to config components ...");
105         config_result = xml.toString();
106         // LOG.info("config_result: {}",config_result);
107         result = defaultOcPlatformFactory.createDefaultComponents(dataStoreContextUtil, config_result);
108         if (result != null) {
109             LOG.info("result : {}", result);
110         }
111         return result;
112     }
113
114     /**
115      * Convert data xml to config xml device.
116      *
117      * @return String result
118      */
119     public String operToConfig() {
120         String result = null;
121         LOG.info("process to transform oper xml file to config xml file ");
122         TransformerFactory factory = TransformerFactory.newInstance();
123         Source xslt = new StreamSource(new File(classLoader.getResource(OC_PLATFORM_XSL).getFile()));
124         Transformer transformer;
125         Source text;
126         StringWriter tmpwriter = new StringWriter();
127         try {
128             LOG.info("transforming xml data to config components ...");
129             transformer = factory.newTransformer(xslt);
130             String extract_data = ExtractXMLTag.extractTagElement(OC_PLATFORM_DATA_SAMPLE_OPER_XML, "components",
131                     "http://openconfig.net/yang/platform");
132             text = new StreamSource(new StringReader(extract_data));
133             LOG.info("text avant transform = {}", text.toString());
134             transformer.transform(text, new StreamResult(tmpwriter));
135             result = tmpwriter.toString();
136             LOG.info(result);
137         } catch (TransformerException e) {
138             LOG.error("Transformer failed ");
139         }
140         return result;
141     }
142
143 }