4f42afbf539b8290a308794fb8458c41b5213e82
[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 java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
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
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
47 import org.w3c.dom.Node;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.SAXException;
50
51 import com.google.common.base.CaseFormat;
52
53 public class ModifyOXMProperties {
54
55         private static String[] versions = new String[]{"v8"};
56         
57         /**
58          * The main method.
59          *
60          * @param args the arguments
61          * @throws SAXException the SAX exception
62          * @throws IOException Signals that an I/O exception has occurred.
63          * @throws ParserConfigurationException the parser configuration exception
64          * @throws XPathExpressionException the x path expression exception
65          * @throws TransformerException the transformer exception
66          */
67         public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
68
69                 for (int i = 0; i < versions.length; i++) {
70                         process(versions[i]);
71                 }
72                 
73                 
74                 
75         }
76         
77         /**
78          * Process.
79          *
80          * @param version the version
81          * @throws SAXException the SAX exception
82          * @throws IOException Signals that an I/O exception has occurred.
83          * @throws ParserConfigurationException the parser configuration exception
84          * @throws XPathExpressionException the x path expression exception
85          * @throws TransformerException the transformer exception
86          */
87         private static void process(String version) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
88
89
90                 
91                 String filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
92                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
93                 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
94                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
95                 Document doc = docBuilder.parse(filepath);
96                 XPath xpath = XPathFactory.newInstance().newXPath();
97                 NodeList list = doc.getElementsByTagName("java-type");
98                 //Find namespaces - restrict to inventory
99                 //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");
100                 XPathExpression expr = xpath.compile("//java-type[@name='Inventory']/java-attributes/xml-element/@name");
101                 Object result = expr.evaluate(doc, XPathConstants.NODESET);
102                 NodeList nodes = (NodeList) result;
103                 String[] temp = null;
104                 List<String> itemsUnderInventory = new ArrayList<>();
105                 for (int i = 0; i < nodes.getLength(); i++) {
106                         itemsUnderInventory.add(nodes.item(i).getTextContent());
107                 }
108                 Map<String, String> namespaces = new HashMap<>();
109
110                 itemsUnderInventory.remove("Search");
111                 for (String item : itemsUnderInventory) {
112                         expr = xpath.compile("//java-type[xml-root-element/@name='" + item + "']");
113                         result = expr.evaluate(doc, XPathConstants.NODESET);
114                         nodes = (NodeList) result;
115                         for (int i = 0; i < nodes.getLength(); i++) {
116                                 String a = "java-attributes/xml-element/@type[contains(.,'aai.openecomp.org')]";
117                                 XPathExpression expr2 = xpath.compile(a);
118                                 Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
119
120                                 NodeList node2 = (NodeList) result2;
121                                 for (int j = 0; j < node2.getLength(); j++) {
122                                         temp = node2.item(j).getTextContent().split("\\.");
123                                         namespaces.put(temp[temp.length-1], item);
124                                 }
125                         }
126                 }
127                 
128                 
129                 
130                 //go through plurals
131                 expr = xpath.compile("//java-type[java-attributes[count(xml-element) = 1]/xml-element[contains(@type, 'aai.openecomp.org')]]/xml-root-element");
132
133                 result = expr.evaluate(doc, XPathConstants.NODESET);
134                 nodes = (NodeList) result;
135                 List<String> children = new ArrayList<>();
136                 for (int i = 0; i < nodes.getLength(); i++) {
137                         XPathExpression expr2 = xpath.compile("../java-attributes/xml-element[1]/@type[contains(.,'aai.openecomp.org')]");
138                         Object result2 = expr2.evaluate(nodes.item(i), XPathConstants.NODESET);
139
140                         NodeList node2 = (NodeList) result2;
141                         temp = node2.item(0).getTextContent().split("\\.");
142                         String containerName = nodes.item(i).getAttributes().getNamedItem("name").getTextContent();
143                         String childrenTuple = containerName + "," + temp[temp.length-1];
144                         if (namespaces.containsKey(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL,containerName))) {
145                                 childrenTuple += "," + namespaces.get(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL,containerName));
146                         }
147                         children.add(childrenTuple);
148                 }
149                 
150                 //match types up with plurals
151                 String[] split = null;
152                 for (String s : children) {
153                         split = s.split(",");
154                         expr = xpath.compile("//java-type[@name='"+split[1]+"']/xml-properties");
155                         result = expr.evaluate(doc, XPathConstants.NODESET);
156                         nodes = (NodeList) result;
157                         if (nodes.getLength() > 0) {
158                                 Element property = null;
159                                 
160                                 if (!hasChild(nodes.item(0), "name", "container")) {
161
162                                         property = doc.createElement("xml-property");
163                                         
164                                         property.setAttribute("name", "container");
165                                         property.setAttribute("value",split[0]);
166                                         nodes.item(0).appendChild(property);
167
168                                 }
169                                 
170                                 if (split.length == 3) {
171                                         Element property2 = null;
172                                         if (!hasChild(nodes.item(0), "name", "namespace")) {
173                                                 property2 = doc.createElement("xml-property");
174                                                 property2.setAttribute("name", "namespace");
175                                                 property2.setAttribute("value",split[2]);
176                                                 nodes.item(0).appendChild(property2);
177                                         }
178
179                                 }
180                         }
181
182
183                 }
184                 
185                 filepath = "bundleconfig-local/etc/oxm/aai_oxm_" + version + ".xml";
186                 TransformerFactory transformerFactory = TransformerFactory.newInstance();
187                 transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
188                 Transformer transformer = transformerFactory.newTransformer();
189                 DOMSource source = new DOMSource(doc);
190                 StreamResult file = new StreamResult(new File(filepath));
191                 transformer.transform(source, file);
192         }
193         
194         /**
195          * Checks for child.
196          *
197          * @param node the node
198          * @param name the name
199          * @param value the value
200          * @return true, if successful
201          */
202         private static boolean hasChild(Node node, String name, String value) {
203                 boolean result = false;
204                 NodeList list = node.getChildNodes();
205                 Node temp = null;
206                 for (int i = 0; i < list.getLength(); i++) {
207
208                         if (list.item(i).hasAttributes()) {
209                                 temp = list.item(i).getAttributes().getNamedItem(name);
210                         }
211                         
212                         if (temp != null && temp.getTextContent().equals(value)) {
213                                 result =  true;
214                         }
215                 }
216                 
217                 return result;
218         }
219
220 }