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.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
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;
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 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";
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;
61 @InjectConfig("persisted-config-path")
62 public String peristConfigPath;
64 @InjectConfig("netconf-initial-config-xml")
65 public String DEVICE_DATA_SAMPLE_OPER_XML;
67 public DeviceConfiguration() {
68 classLoader = Thread.currentThread().getContextClassLoader();
69 dataStoreContextUtil = new DataStoreContextImpl();
70 defaultDeviceFactory = new DefaultDeviceFactory();
74 public String getConfigDevice() {
75 return operToConfig();
78 public String getPeristConfigPath() {
79 return peristConfigPath;
82 public String getNetconfInitialConfigXml() {
83 return DEVICE_DATA_SAMPLE_OPER_XML;
86 public Boolean getRegister() {
90 public void setRegister(Boolean reg) {
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);
99 LOG.info("result info : {}", result.getInfo().getNodeId());
101 LOG.warn("Failed to get Device data !");
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;
113 StringWriter device_config = new StringWriter();
114 OrgOpenroadmDevice result = null;
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());
126 } catch (TransformerException e) {
127 LOG.error("Transformer failed ");
134 * Convert data xml to config xml device.
136 * @return String result
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();
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);