Sonar Major: Replace the type specification.
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / util / MakeNamedQuery.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.util;
23 import java.io.File;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.UUID;
30
31 import org.apache.commons.io.FileUtils;
32
33 import org.onap.aai.introspection.Introspector;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.LoaderFactory;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.introspection.Version;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.util.AAIConfig;
40 import org.onap.aai.util.AAIConstants;
41
42 public class MakeNamedQuery {
43
44         public static void main(String[] args) throws Exception {
45                 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
46                 String widgetJsonDir = null;
47                 String modelVersion = null;
48                 String namedQueryUuid = UUID.randomUUID().toString();
49                 if (args.length > 0) { 
50                         if (args[0] != null) {
51                                 _apiVersion = args[0];
52                         }
53                         if (args[1] != null) { 
54                                 widgetJsonDir = args[1];
55                         }
56                         if (args[2] != null) { 
57                                 modelVersion = args[2];
58                         }
59                         if (args[3] != null) {
60                                 namedQueryUuid = args[3];
61                         }
62                 }
63
64                 if (widgetJsonDir == null) { 
65                         System.err.println("You must specify a directory for widgetModelJson");
66                         System.exit(0);
67                 }
68                 if (modelVersion == null) { 
69                         System.err.println("You must specify a modelVersion");
70                         System.exit(0);
71                 }
72
73
74                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
75
76                 // iterate the collection of resources
77
78                 ArrayList<String> processedWidgets = new ArrayList<>();
79
80
81                 HashMap<String, List<Introspector>> widgetToRelationship = new HashMap<String, List<Introspector>>();
82                 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
83                         Introspector meObject = loader.introspectorFromName("model");
84                         // no need for a ModelVers DynamicEntity
85
86                         Introspector aaiRes = aaiResEnt.getValue();
87
88                         if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
89                                 String resource = aaiRes.getName();
90
91                                 if (processedWidgets.contains(resource)) {
92                                         continue;
93                                 }
94                                 processedWidgets.add(resource);
95
96                                 String widgetName = resource;
97                                 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
98                                 File f = new File(filePathString);
99                                 if (f.exists()) { 
100                                         System.out.println(f.toString());
101                                         String json = FileUtils.readFileToString(f);
102
103                                         meObject = loader.unmarshal("Model", json);
104                                         String modelInvariantId = meObject.getValue("model-invariant-id");
105                                         if (meObject.hasProperty("model-vers")) { 
106                                                 Introspector modelVers = meObject.getWrappedValue("model-vers");
107                                                 List<Introspector> modelVerList = (List<Introspector>) modelVers.getWrappedListValue("model-ver");
108                                                 for (Introspector modelVer : modelVerList) {
109                                                         
110                                                         List<Introspector> relList = new ArrayList<Introspector>();
111                                                         Introspector widgetRelationship = makeWidgetRelationship(loader, modelInvariantId, 
112                                                                         modelVer.getValue("model-version-id").toString());
113                                                         relList.add(widgetRelationship);
114                                                                                                                 
115                                                         widgetToRelationship.put(widgetName, relList);
116                                                 }
117                                         }
118                                 }
119                         }
120                 }
121                 
122 //              esr-system-info-from-vnf=builder.store('x').union(\
123 //                builder.newInstance().createEdgeTraversal(EdgeType.COUSIN, 'generic-vnf', 'vserver').store('x').union(\
124 //                        builder.newInstance().createEdgeTraversal(EdgeType.TREE, 'vserver', 'tenant').store('x')\
125 //                        .createEdgeTraversal(EdgeType.TREE, 'tenant', 'cloud-region').store('x')\
126 //                        .createEdgeTraversal(EdgeType.TREE, 'cloud-region', 'esr-system-info').store('x')\
127 //                )).cap('x').unfold.dedup()
128  
129                 //source vnf-id, related service-instance-id, all related vnfs in this service-instance-id
130                 
131                 //this should be abstracted and moved to a file
132                 
133                 HashMap<String, List<Introspector>> relationshipMap = new HashMap<String, List<Introspector>>();
134                                 
135                 List<Introspector> genericVnfRelationship = widgetToRelationship.get("generic-vnf");
136                 List<Introspector> vserverRelationship = widgetToRelationship.get("vserver");
137                 List<Introspector> tenantRelationship = widgetToRelationship.get("tenant");
138                 List<Introspector> cloudRegionRelationship = widgetToRelationship.get("cloud-region");
139                 List<Introspector> esrSystemInfoRelationship = widgetToRelationship.get("esr-system-info");
140
141                 Introspector namedQueryObj = loader.introspectorFromName("named-query");
142                 namedQueryObj.setValue("named-query-uuid", namedQueryUuid);
143                 namedQueryObj.setValue("named-query-name", "vnf-to-esr-system-info");
144                 namedQueryObj.setValue("named-query-version", "1.0");
145                 namedQueryObj.setValue("description", "Named Query - VNF to ESR System Info");
146                                          
147                 Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRelationship);
148                                 
149                 Introspector vserverNQE = setupNQElements(genericVnfNQE, vserverRelationship);
150                 
151                 Introspector tenantNQE = setupNQElements(vserverNQE, tenantRelationship);
152                 
153                 Introspector cloudRegionNQE = setupNQElements(tenantNQE, cloudRegionRelationship);
154
155                 Introspector esrSystemInfoNQE = setupNQElements(cloudRegionNQE, esrSystemInfoRelationship);
156                 
157                 System.out.println(namedQueryObj.marshal(true));
158                 
159                 System.exit(0);
160
161         }       
162         private static List<Introspector> getRels(String widgetName, HashMap<String, Introspector> widgetToRelationship) {
163                 List<Introspector> relList = new ArrayList<Introspector>();
164                 Introspector genericVnfRelationship = widgetToRelationship.get(widgetName);
165                 relList.add(genericVnfRelationship);
166                 return relList;
167         }
168         
169         private static Introspector setupNQElements (Introspector nqeObj, List<Introspector> listOfRelationships) {
170                 Introspector newNQElement = null;
171                 try {
172                         Introspector newNQElements = null;
173                         List<Object> nqElementList = null;
174                         if (nqeObj.getWrappedValue("named-query-elements") != null) {
175                                 newNQElements = nqeObj.getWrappedValue("named-query-elements");
176                                 nqElementList = newNQElements.getValue("named-query-element");
177                         } else { 
178                                 newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements");
179                                 nqeObj.setValue("named-query-elements",  newNQElements.getUnderlyingObject());
180                                 nqElementList = (List<Object>)newNQElements.getValue("named-query-element");
181                         }
182                         newNQElement = loadNQElement(newNQElements, listOfRelationships);
183                         nqElementList.add(newNQElement.getUnderlyingObject());
184                 
185                 } catch (AAIUnknownObjectException e) {
186                         // TODO Auto-generated catch block
187                         e.printStackTrace();
188                 } catch (IllegalArgumentException e) {
189                         // TODO Auto-generated catch block
190                         e.printStackTrace();
191                 }
192                 return newNQElement;
193         }
194         
195         private static Introspector loadNQElement (Introspector nqElements, List<Introspector> listOfRelationships) {
196                 Introspector newNqElement = null;
197                 try {
198                         newNqElement = nqElements.getLoader().introspectorFromName("named-query-element");
199                                 
200                         //newNqElement.setValue("named-query-element-uuid", UUID.randomUUID().toString());
201
202                         Introspector newRelationshipList = newNqElement.getLoader().introspectorFromName("relationship-list");
203                         newNqElement.setValue("relationship-list", newRelationshipList.getUnderlyingObject());
204
205                         List<Object> newRelationshipListList = (List<Object>)newRelationshipList.getValue("relationship");
206
207                         for (Introspector rel : listOfRelationships) { 
208                                 newRelationshipListList.add(rel.getUnderlyingObject());
209                         }
210                         
211                 } catch (AAIUnknownObjectException e) {
212                         // TODO Auto-generated catch block
213                         e.printStackTrace();
214                 } catch (IllegalArgumentException e) {
215                         // TODO Auto-generated catch block
216                         e.printStackTrace();
217                 }
218                 return newNqElement;
219
220         }
221         private static Introspector makeWidgetRelationship(Loader loader, String modelInvariantId, String modelVersionId) {
222
223                 Introspector newRelationship = null;
224                 try {
225                         newRelationship = loader.introspectorFromName("relationship");
226
227                         List<Object> newRelationshipData = (List<Object>)newRelationship.getValue("relationship-data");
228
229                         newRelationship.setValue("related-to", "model");
230
231                         Introspector newRelationshipDatum1 = newRelationship.getLoader().introspectorFromName("relationship-data");
232                         Introspector newRelationshipDatum2 = newRelationship.getLoader().introspectorFromName("relationship-data");
233
234
235                         newRelationshipDatum1.setValue("relationship-key", "model.model-invariant-id");
236                         newRelationshipDatum1.setValue("relationship-value", modelInvariantId);
237
238                         //newRelationshipDatum2.setValue("relationship-key", "model-ver.model-version-id");
239                         //newRelationshipDatum2.setValue("relationship-value", modelVersionId);
240
241                         newRelationshipData.add(newRelationshipDatum1.getUnderlyingObject());
242                         //newRelationshipData.add(newRelationshipDatum2.getUnderlyingObject());
243                 } catch (AAIUnknownObjectException e) {
244                         // TODO Auto-generated catch block
245                         e.printStackTrace();
246                 } catch (IllegalArgumentException e) {
247                         // TODO Auto-generated catch block
248                         e.printStackTrace();
249                 }
250
251                 return newRelationship;
252         }
253
254 }