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.terminal.device.rev170708.terminal.device.top.TerminalDevice;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import io.fd.honeycomb.transportpce.device.tools.DefaultOcTerminalDeviceFactory;
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 OcTerminalDeviceConfiguration {
50 private static final Logger LOG = LoggerFactory.getLogger(OcTerminalDeviceConfiguration.class);
51 private static final String OC_TERMINAL_DEVICE_XSL = "device/octerminaldeviceOperToConfig.xsl";
53 private DataStoreContext dataStoreContextUtil;
54 private DefaultOcTerminalDeviceFactory defaultOcTermminalDeviceFactory;
55 private ClassLoader classLoader;
56 private Boolean register;
57 public String config_terminalDevice;
58 public TerminalDevice oper_terminalDevice;
60 @InjectConfig("netconf-initial-config-xml")
61 public String OC_TERMINAL_DEVICE_DATA_SAMPLE_OPER_XML;
63 public OcTerminalDeviceConfiguration() {
64 LOG.info("initializing OcTerminalDeviceConfiguration");
65 classLoader = Thread.currentThread().getContextClassLoader();
66 dataStoreContextUtil = new DataStoreContextImpl();
67 defaultOcTermminalDeviceFactory = new DefaultOcTerminalDeviceFactory();
71 public String getNetconfInitialOcTerminalDeviceXml() {
72 return OC_TERMINAL_DEVICE_DATA_SAMPLE_OPER_XML;
75 public String getConfigTerminalDevice() {
76 return operToConfig();
79 public Boolean getRegister() {
83 public void setRegister(Boolean reg) {
87 public TerminalDevice getDataTerminalDevice() {
88 TerminalDevice result = null;
89 File terminalDevice_data = new File(classLoader.getResource(OC_TERMINAL_DEVICE_DATA_SAMPLE_OPER_XML).getFile());
90 result = defaultOcTermminalDeviceFactory.createDefaultTerminalDevice(dataStoreContextUtil, terminalDevice_data);
92 LOG.info("reading initial data.");
94 LOG.warn("failed to get OC Platform Data");
99 public TerminalDevice getTerminalDeviceFromXML(String xml) {
100 String config_result = null;
101 LOG.info("process to transform xml file to config data");
102 TerminalDevice result = null;
103 LOG.info("transforming xml string to config components ...");
104 config_result = xml.toString();
105 // LOG.info("config_result: {}",config_result);
106 result = defaultOcTermminalDeviceFactory.createDefaultTerminalDevice(dataStoreContextUtil, config_result);
107 if (result != null) {
108 LOG.info("result : {}", result);
114 * Convert data xml to config xml terminal-device.
116 * @return String result
118 public String operToConfig() {
119 String result = null;
120 LOG.info("process to transform oper xml file to config xml file for terminal-device");
121 TransformerFactory factory = TransformerFactory.newInstance();
122 Source xslt = new StreamSource(new File(classLoader.getResource(OC_TERMINAL_DEVICE_XSL).getFile()));
123 Transformer transformer;
125 StringWriter tmpwriter = new StringWriter();
127 LOG.info("transforming xml data to config terminal-device ...");
128 transformer = factory.newTransformer(xslt);
129 String extract_data = ExtractXMLTag.extractTagElement(OC_TERMINAL_DEVICE_DATA_SAMPLE_OPER_XML,
130 "terminal-device", "http://openconfig.net/yang/terminal-device");
131 text = new StreamSource(new StringReader(extract_data));
132 LOG.info("text avant transform = {}", text.toString());
133 transformer.transform(text, new StreamResult(tmpwriter));
134 result = tmpwriter.toString();
136 } catch (TransformerException e) {
137 LOG.error("Transformer failed ");