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