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