2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.transportpce.device.configuration;
20 import java.io.StringReader;
21 import java.io.StringWriter;
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;
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;
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;
43 * Class containing static configuration for honeynode-plugin module<br>
45 * Further documentation for the configuration injection can be found at:
46 * https://github.com/yyvess/gconf
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";
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;
60 @InjectConfig("netconf-initial-config-xml")
61 public String OC_PLATFORM_DATA_SAMPLE_OPER_XML;
63 public OcPlatformConfiguration() {
64 LOG.info("initializing OcPlatformConfiguration");
65 classLoader = Thread.currentThread().getContextClassLoader();
66 dataStoreContextUtil = new DataStoreContextImpl();
67 defaultOcPlatformFactory = new DefaultOcPlatformFactory();
71 public String getNetconfInitialOcPlatformXml() {
72 return OC_PLATFORM_DATA_SAMPLE_OPER_XML;
75 public String getConfigComponents() {
76 return operToConfig();
79 public Boolean getRegister() {
83 public void setRegister(Boolean reg) {
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);
92 LOG.info("reading initial data.");
94 LOG.warn("failed to get OC Platform Data");
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);
115 * Convert data xml to config xml device.
117 * @return String result
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;
126 StringWriter tmpwriter = new StringWriter();
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();
137 } catch (TransformerException e) {
138 LOG.error("Transformer failed ");