Resolved below sonar issues:
[aai/aai-service.git] / ajsc-aai / src / main / java / org / openecomp / aai / workarounds / ModifyOXMProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.aai.workarounds;
22
23 import com.google.common.base.CaseFormat;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamResult;
39 import javax.xml.xpath.XPath;
40 import javax.xml.xpath.XPathConstants;
41 import javax.xml.xpath.XPathExpression;
42 import javax.xml.xpath.XPathExpressionException;
43 import javax.xml.xpath.XPathFactory;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.SAXException;
49
50 public class ModifyOXMProperties {
51
52     private static String[] versions = new String[]{"v8"};
53
54     /**
55      * The main method.
56      *
57      * @param args the arguments
58      * @throws SAXException the SAX exception
59      * @throws IOException Signals that an I/O exception has occurred.
60      * @throws ParserConfigurationException the parser configuration exception
61      * @throws XPathExpressionException the x path expression exception
62      * @throws TransformerException the transformer exception
63      */
64     public static void main(String[] args)
65         throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
66
67         for (String version : versions) {
68             process(version);
69         }
70     }
71
72     /**
73      * Process.
74      *
75      * @param version the version
76      * @throws SAXException the SAX exception
77      * @throws IOException Signals that an I/O exception has occurred.
78      * @throws ParserConfigurationException the parser configuration exception
79      * @throws XPathExpressionException the x path expression exception
80      * @throws TransformerException the transformer exception
81      */
82     private static void process(String version)
83         throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
84
85         String filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
86         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
87         docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
88         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
89         Document doc = docBuilder.parse(filepath);
90         XPath xpath = XPathFactory.newInstance().newXPath();
91         //Find namespaces - restrict to inventory
92         //XPathExpression expr = xpath.compile("//java-type[java-attributes[count(xml-element[contains(@type, 'aai.openecomp.org')])=count(./xml-element)][count(xml-element) > 1]]/xml-root-element");
93         XPathExpression expr = xpath.compile("//java-type[@name='Inventory']/java-attributes/xml-element/@name");
94         Object result = expr.evaluate(doc, XPathConstants.NODESET);
95         NodeList nodes = (NodeList) result;
96         List<String> itemsUnderInventory = new ArrayList<>();
97         for (int i = 0; i < nodes.getLength(); i++) {
98             itemsUnderInventory.add(nodes.item(i).getTextContent());
99         }
100         Map<String, String> namespaces = new HashMap<>();
101
102         itemsUnderInventory.remove("Search");
103         for (String item : itemsUnderInventory) {
104             expr = xpath.compile("//java-type[xml-root-element/@name='" + item + "']");
105             result = expr.evaluate(doc, XPathConstants.NODESET);
106             nodes = (NodeList) result;
107             handleItemsUnderInventory(nodes, namespaces, xpath, item);
108         }
109
110         //go through plurals
111         expr = xpath.compile(
112             "//java-type[java-attributes[count(xml-element) = 1]/xml-element[contains(@type, 'aai.openecomp.org')]]/xml-root-element");
113
114         result = expr.evaluate(doc, XPathConstants.NODESET);
115         nodes = (NodeList) result;
116         List<String> children = new ArrayList<>();
117         for (int i = 0; i < nodes.getLength(); i++) {
118             XPathExpression expr2 = xpath
119                 .compile("../java-attributes/xml-element[1]/@type[contains(.,'aai.openecomp.org')]");
120             Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
121
122             NodeList node2 = (NodeList) result2;
123             String[] temp = node2.item(0).getTextContent().split("\\.");
124             String containerName = nodes.item(i).getAttributes().getNamedItem("name").getTextContent();
125             String childrenTuple = containerName + "," + temp[temp.length - 1];
126             if (namespaces.containsKey(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, containerName))) {
127                 childrenTuple +=
128                     "," + namespaces.get(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, containerName));
129             }
130             children.add(childrenTuple);
131         }
132
133         //match types up with plurals
134         handleChildrenNodes(children, xpath, doc);
135
136         filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
137         TransformerFactory transformerFactory = TransformerFactory.newInstance();
138         transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
139         Transformer transformer = transformerFactory.newTransformer();
140         DOMSource source = new DOMSource(doc);
141         StreamResult file = new StreamResult(new File(filepath));
142         transformer.transform(source, file);
143     }
144
145     private static void handleItemsUnderInventory(NodeList nodes, Map<String, String> namespaces, XPath xpath,
146         String item) throws XPathExpressionException {
147         String[] temp;
148         for (int i = 0; i < nodes.getLength(); i++) {
149             String a = "java-attributes/xml-element/@type[contains(.,'aai.openecomp.org')]";
150             XPathExpression expr2 = xpath.compile(a);
151             Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
152
153             NodeList node2 = (NodeList) result2;
154             for (int j = 0; j < node2.getLength(); j++) {
155                 temp = node2.item(j).getTextContent().split("\\.");
156                 namespaces.put(temp[temp.length - 1], item);
157             }
158         }
159     }
160
161     private static void handleChildrenNodes(List<String> children, XPath xpath, Document doc)
162         throws XPathExpressionException {
163         NodeList nodes;
164         XPathExpression expr;
165         Object result;
166         String[] split;
167         for (String s : children) {
168             split = s.split(",");
169             expr = xpath.compile("//java-type[@name='" + split[1] + "']/xml-properties");
170             result = expr.evaluate(doc, XPathConstants.NODESET);
171             nodes = (NodeList) result;
172             if (nodes.getLength() > 0) {
173
174                 if (!hasChild(nodes.item(0), "name", "container")) {
175
176                     Element property = doc.createElement("xml-property");
177                     property.setAttribute("name", "container");
178                     property.setAttribute("value", split[0]);
179                     nodes.item(0).appendChild(property);
180                 }
181
182                 if (split.length == 3) {
183                     Element property;
184                     if (!hasChild(nodes.item(0), "name", "namespace")) {
185                         property = doc.createElement("xml-property");
186                         property.setAttribute("name", "namespace");
187                         property.setAttribute("value", split[2]);
188                         nodes.item(0).appendChild(property);
189                     }
190                 }
191             }
192         }
193     }
194
195     /**
196      * Checks for child.
197      *
198      * @param node the node
199      * @param name the name
200      * @param value the value
201      * @return true, if successful
202      */
203     private static boolean hasChild(Node node, String name, String value) {
204         boolean result = false;
205         NodeList list = node.getChildNodes();
206         Node temp = null;
207         for (int i = 0; i < list.getLength(); i++) {
208
209             if (list.item(i).hasAttributes()) {
210                 temp = list.item(i).getAttributes().getNamedItem(name);
211             }
212
213             if (temp != null && temp.getTextContent().equals(value)) {
214                 result = true;
215             }
216         }
217
218         return result;
219     }
220 }