blob: cb8e89c11c5ae0508061244a95cb3525a03e2e69 [file] [log] [blame]
Bharat saraswalb0dbe6b2017-03-24 22:51:06 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Bharat saraswalb0dbe6b2017-03-24 22:51:06 +05303 *
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
17package org.onosproject.netconf.client.impl;
18
19import javax.xml.transform.OutputKeys;
20import javax.xml.transform.Source;
21import javax.xml.transform.Transformer;
22import javax.xml.transform.TransformerConfigurationException;
23import javax.xml.transform.TransformerException;
24import javax.xml.transform.TransformerFactory;
25import javax.xml.transform.stream.StreamResult;
26import javax.xml.transform.stream.StreamSource;
27import java.io.StringReader;
28import java.io.StringWriter;
29
30/**
31 * Represents utilities for huawei driver.
32 */
33public final class Utils {
34
35 /**
36 * Prevents creation of utils instance.
37 */
38 private Utils() {
39 }
40
41 // Default namespace given in yang files
42 private static final String XMLNS_STRING = "xmlns=\"ne-l3vpn-api\"";
43 private static final String XMLNS_HUA_STRING = "xmlns=\"http://www.huawei" +
44 ".com/netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\"";
45
46 /**
47 * YMS encode the java object into a xml string with xml namespace equals to
48 * the namespace defined in YANG file. Huawei driver overwriting this
49 * default xml namespace in generated xml string with xml string for Huawei.
50 *
51 * @param request xml string as an output of YMS encode operation
52 * @return formatted string
53 */
54 private static String formatMessage(String request) {
55 if (request.contains(XMLNS_STRING)) {
56 request = request.replaceFirst(XMLNS_STRING, XMLNS_HUA_STRING);
57 }
58 return request;
59 }
60
61 /**
62 * Returns the appended provided xml string with device specific rpc
63 * request tags.
64 *
65 * @param encodedString xml string need to be updated
66 * @return appended new tags xml string
67 */
68 static String editConfig(String encodedString) {
69
70 // Add opening protocol edit config tags.
71 StringBuilder rpc =
72 new StringBuilder(
73 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0" +
74 "\" " +
75 "message-id=\"1\">");
76 rpc.append("<edit-config>");
77 rpc.append("<target>");
78 rpc.append("<running/>");
79 rpc.append("</target>");
80
81 // Get the formatted XML namespace string.
82 encodedString = formatMessage(encodedString);
83
84 // Add the closing protocol edit config tags.
85 rpc.append(encodedString);
86 rpc.append("</edit-config>");
87 rpc.append("</rpc>");
88
89 return rpc.toString();
90 }
91
92 /**
93 * Converts xml string to pretty format.
94 *
95 * @param input xml string to be converted to pretty format
96 * @return pretty format xml string
97 */
98 static String prettyFormat(String input) {
99 // Prepare input and output stream
100 Source xmlInput = new StreamSource(new StringReader(input));
101 StringWriter stringWriter = new StringWriter();
102 StreamResult xmlOutput = new StreamResult(stringWriter);
103
104 // Create transformer
105 TransformerFactory transformerFactory = TransformerFactory.newInstance();
106 Transformer transformer = null;
107
108 try {
109 transformer = transformerFactory.newTransformer();
110 } catch (TransformerConfigurationException e) {
111 e.printStackTrace();
112 }
113
114 // Need to omit the xml header and set indent to 4
115 if (transformer != null) {
116 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
117 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
118 transformer.setOutputProperty("{http://xml.apache" +
119 ".org/xslt}indent-amount", "4");
120
121 // Covert input string to xml pretty format and return
122 try {
123 transformer.transform(xmlInput, xmlOutput);
124 } catch (TransformerException e) {
125 e.printStackTrace();
126 }
127 }
128 return xmlOutput.getWriter().toString();
129 }
130}