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;
19 import io.fd.honeycomb.transportpce.device.tools.DefaultDeviceFactory;
20 import io.fd.honeycomb.transportpce.test.common.DataStoreContext;
21 import io.fd.honeycomb.transportpce.test.common.DataStoreContextImpl;
24 import java.io.StringReader;
25 import java.io.StringWriter;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.stream.StreamResult;
32 import javax.xml.transform.stream.StreamSource;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
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";
53 private static final String NAMESPACE_TRIMER_XSL = "device/NamespaceTrimmer.xslt";
54 //private static final String DEVICE_DATA_SAMPLE_OPER_XML = "device/sample-config-ROADM.xml";
56 public String config_device;
57 public OrgOpenroadmDevice oper_device;
58 private DataStoreContext dataStoreContextUtil;
59 private DefaultDeviceFactory defaultDeviceFactory;
60 private ClassLoader classLoader;
61 private Boolean register;
63 @InjectConfig("persisted-config-path")
64 public String peristConfigPath;
66 @InjectConfig("netconf-initial-config-xml")
67 public String DEVICE_DATA_SAMPLE_OPER_XML;
69 public DeviceConfiguration() {
70 classLoader = Thread.currentThread().getContextClassLoader();
71 dataStoreContextUtil = new DataStoreContextImpl();
72 defaultDeviceFactory = new DefaultDeviceFactory();
77 public String getConfigDevice() {
78 return operToConfig();
81 public String getPeristConfigPath() {
82 return peristConfigPath;
85 public String getNetconfInitialConfigXml() {
86 return DEVICE_DATA_SAMPLE_OPER_XML;
89 public Boolean getRegister() {
93 public void setRegister(Boolean reg) {
97 public OrgOpenroadmDevice getDataDevice() {
98 OrgOpenroadmDevice result = null;
99 File device_data = new File(classLoader.getResource(DEVICE_DATA_SAMPLE_OPER_XML).getFile());
100 result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,device_data);
101 if (result != null) {
102 LOG.info("result info : {}", result.getInfo().getNodeId());
107 public OrgOpenroadmDevice getDeviceFromXML(String xml) {
108 String config_result =null;
109 LOG.info("process to transform xml file to config data");
110 TransformerFactory factory = TransformerFactory.newInstance();
111 Source xslt = new StreamSource(new File(classLoader.getResource(CONFIG_XSL).getFile()));
112 Transformer transformer;
114 StringWriter device_config = new StringWriter();
115 OrgOpenroadmDevice result = null;
117 LOG.info("transforming xml string to config device ...");
118 transformer = factory.newTransformer(xslt);
119 text = new StreamSource(new StringReader(xml));
120 transformer.transform(text, new StreamResult(device_config));
121 config_result = device_config.toString();
122 //LOG.info("config_result: {}",config_result);
123 result = defaultDeviceFactory.createDefaultDevice(dataStoreContextUtil,config_result);
124 if (result != null) {
125 LOG.info("result info : {}", result.getInfo().getNodeId());
127 } catch (TransformerException e) {
128 LOG.error("Transformer failed ");
136 * Convert data xml to config xml device.
137 * @return String result
139 public String operToConfig() {
141 LOG.info("process to transform xml file ");
142 TransformerFactory factory = TransformerFactory.newInstance();
143 Source xslt = new StreamSource(new File(classLoader.getResource(DEVICE_XSL).getFile()));
144 Transformer transformer;
146 StringWriter tmpwriter = new StringWriter();
149 LOG.info("transforming xml data to config device ...");
150 transformer = factory.newTransformer(xslt);
151 text = new StreamSource(new File(classLoader.getResource(DEVICE_DATA_SAMPLE_OPER_XML).getFile()));
152 transformer.transform(text, new StreamResult(tmpwriter));
153 LOG.info("removing namespace ...");
154 xslt = new StreamSource(new File(classLoader.getResource(NAMESPACE_TRIMER_XSL).getFile()));
155 transformer = factory.newTransformer(xslt);
156 reader = new StringReader(tmpwriter.toString());
157 StringWriter device_config = new StringWriter();
158 text = new StreamSource(reader);
159 transformer.transform(text, new StreamResult(device_config));
160 result = device_config.toString();
162 } catch (TransformerException e) {
163 LOG.error("Transformer failed ");