Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / ingestModel / IngestModelMoxyOxm.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.ingestModel;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Timer;
37 import java.util.TimerTask;
38 import java.util.Vector;
39
40 import org.eclipse.persistence.descriptors.ClassDescriptor;
41 import org.eclipse.persistence.dynamic.DynamicType;
42 import org.eclipse.persistence.internal.dynamic.DynamicTypeImpl;
43 import org.eclipse.persistence.internal.helper.DatabaseField;
44 import org.eclipse.persistence.jaxb.JAXBContextProperties;
45 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
46 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
47 import org.eclipse.persistence.mappings.DatabaseMapping;
48 import org.eclipse.persistence.oxm.XMLField;
49 import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping;
50 import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping;
51
52 import org.openecomp.aai.domain.model.AAIResource;
53 import org.openecomp.aai.domain.model.AAIResourceKey;
54 import org.openecomp.aai.domain.model.AAIResourceKeys;
55 import org.openecomp.aai.domain.model.AAIResources;
56 import org.openecomp.aai.exceptions.AAIException;
57 import org.openecomp.aai.logging.ErrorLogHelper;
58 import org.openecomp.aai.util.AAIConfig;
59 import org.openecomp.aai.util.AAIConstants;
60 import org.openecomp.aai.util.FileWatcher;
61 import com.att.eelf.configuration.EELFLogger;
62 import com.att.eelf.configuration.EELFManager;
63 import com.google.common.base.CaseFormat;
64 import com.google.common.base.Splitter;
65 import com.google.common.collect.Multimap;
66
67 /**
68  * The Class IngestModelMoxyOxm.
69  */
70 public class IngestModelMoxyOxm
71 {
72
73         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(IngestModelMoxyOxm.class);
74
75         public static HashMap<String, AAIResources> aaiResourceContainer;
76         public static HashMap<String, DbMaps> dbMapsContainer;
77
78         private static HashMap<String, Timer> timers = new HashMap<String,Timer>();
79
80         /**
81          * The main method.
82          *
83          * @param args the arguments
84          * @throws Exception the exception
85          */
86         public static void main(String[] args) throws Exception {
87
88                 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
89
90                 if (args.length > 0) { 
91                         if (args[0] != null) {
92                                 _apiVersion = args[0];
93                         }
94                 }
95                 ArrayList<String> apiVersions = new ArrayList<String>();
96                 apiVersions.add(_apiVersion);
97                 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
98                 m.init(apiVersions, false);
99
100                 ArrayList<String> endpoints = new ArrayList<String>();
101
102                 for (Map.Entry<String, AAIResources> ent: aaiResourceContainer.entrySet()) {
103
104                         AAIResources aaiResources = ent.getValue();
105                         DynamicJAXBContext jaxbContext = aaiResources.getJaxbContext();
106                         for (Map.Entry<String, AAIResource> aaiResEnt : aaiResources.getAaiResources().entrySet()) { 
107                                 AAIResource aaiRes = aaiResEnt.getValue();
108
109                                 String uri = aaiRes.getUri();
110                                 if (uri != null) { 
111                                         endpoints.add(uri);
112                                         DynamicType dt = jaxbContext.getDynamicType(aaiRes.getResourceClassName());
113                                         if (dt.containsProperty("relationshipList")) {
114                                                 endpoints.add(uri + "/relationship-list/relationship");
115                                         }
116                                 }
117                         }
118                         Collections.sort(endpoints);
119                         for (String endpoint : endpoints) {
120                                 if (!endpoint.contains("/aai-internal/")) { 
121                                         System.out.println(endpoint);
122                                 }
123                         }
124                 }
125
126                 System.exit(0);
127         }
128
129         /**
130          * Inits the.
131          *
132          * @param apiVersions the api versions
133          * @throws Exception the exception
134          */
135         public synchronized void init(ArrayList<String> apiVersions) throws AAIException {
136                 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
137                 m.init(apiVersions, true);
138         }
139
140         /**
141          * Inits the.
142          *
143          * @param apiVersions the api versions
144          * @param setTimer the set timer
145          * @throws AAIException If AAIConfig is missing necessary properties
146          * @throws Exception the exception
147          */
148         public synchronized void init(ArrayList<String> apiVersions, Boolean setTimer) throws AAIException {
149
150                 aaiResourceContainer = new HashMap<String, AAIResources>();
151                 dbMapsContainer = new HashMap<String, DbMaps>();
152
153                 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
154
155                 for (String apiVersion : apiVersions) { 
156
157                         String relationshipUtils = "org.openecomp.aai.dbmap.RelationshipUtils";
158                         final String thisRelationshipUtils = relationshipUtils;
159
160                         final String thisApiVersion = apiVersion;
161                         final String schemaFile = AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + apiVersion + ".xml";
162
163                         m.loadSchema(apiVersion, schemaFile, relationshipUtils);
164
165                         if (!setTimer) continue;
166
167                         TimerTask task = null;
168                         task = new FileWatcher ( new File(schemaFile)) {
169                                 protected void onChange( File file ) {
170                                         m.loadSchema(thisApiVersion, schemaFile, thisRelationshipUtils);
171                                 }
172                         };
173
174                         if (!timers.containsKey(apiVersion)) {
175                                 Timer timer = new Timer();
176                                 timer.schedule( task , new Date(), 10000 );
177                                 timers.put(apiVersion,  timer);
178
179                         }
180                 }
181                 if (apiVersions.contains(AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP))) { 
182                         m.createPropertyAndNodeVersionInfoMapFromDbMaps();
183
184                         for (Map.Entry<String, AAIResources> ent: aaiResourceContainer.entrySet()) {
185                                 String apiVersion = ent.getKey();
186                                 AAIResources aaiResources = ent.getValue();
187
188                                 DbMaps dbMap = dbMapsContainer.get(apiVersion);
189
190                                 for (Map.Entry<String, AAIResource> aaiResEnt : aaiResources.getAaiResources().entrySet()) { 
191                                         AAIResource aaiRes = aaiResEnt.getValue();
192                                         aaiRes.setPropertyDataTypeMap(dbMap.PropertyDataTypeMap); 
193                                         aaiRes.setNodeKeyProps(dbMap.NodeKeyProps);
194                                         aaiRes.setNodeNameProps(dbMap.NodeNameProps);
195                                         aaiRes.setNodeMapIndexedProps(dbMap.NodeMapIndexedProps);
196                                 }
197                         }
198                 }
199         }
200
201         /**
202          * Load schema.
203          *
204          * @param apiVersion the api version
205          * @param schemaFile the schema file
206          * @param relationshipUtils the relationship utils
207          * @return the dynamic JAXB context
208          */
209         private DynamicJAXBContext loadSchema(String apiVersion,
210                         String schemaFile, 
211                         String relationshipUtils)  {
212
213                 AAIResources aaiResources = new AAIResources();
214                 DbMaps dbMaps = new DbMaps();
215                 DynamicJAXBContext jaxbContext = null;
216
217                 try {
218
219                         InputStream iStream = new FileInputStream(new File(schemaFile));
220
221                         Map<String, Object> properties = new HashMap<String, Object>(); 
222                         properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream); 
223
224                         jaxbContext = 
225                                         DynamicJAXBContextFactory.createContextFromOXM(IngestModelMoxyOxm.class.getClassLoader(), properties);
226
227                         aaiResources.setJaxbContext(jaxbContext);
228                         String rootClassName = "inventory.aai.openecomp.org." + apiVersion + ".Inventory";
229
230                         if ("v2".equals(apiVersion)) { 
231                                 rootClassName = "inventory.aai.openecomp.org.Inventory";
232                         } 
233
234                         DynamicTypeImpl t = (DynamicTypeImpl)jaxbContext.getDynamicType(rootClassName);
235
236                         lookAtDynamicResource("Inventory",
237                                         "inventory.aai.openecomp.org." + apiVersion, 
238                                         jaxbContext, 
239                                         t,
240                                         1, "", "", apiVersion, "/" + apiVersion, false, aaiResources, dbMaps, relationshipUtils);
241
242                 } catch (Exception e) {
243                         ErrorLogHelper.logException(new AAIException("AAI_3000", e));
244                 }
245
246                 LOGGER.info("---> Loading " + apiVersion + " in aaiResourceContainer");
247                 aaiResourceContainer.put(apiVersion,  aaiResources);
248
249                 createDbMapsfromAAIResources(aaiResources, dbMaps);
250
251                 LOGGER.info("---> Loading " + apiVersion + " in dbMapsContainer");
252                 dbMapsContainer.put(apiVersion,  dbMaps);
253
254                 return jaxbContext;
255         }
256
257         /**
258          * Cleanup.
259          */
260         public void cleanup() {
261                 aaiResourceContainer.clear();
262                 dbMapsContainer.clear();
263         }
264
265         /**
266          * Look at dynamic resource.
267          *
268          * @param resource the resource
269          * @param pojoBase the pojo base
270          * @param jaxbContext the jaxb context
271          * @param t the t
272          * @param depth the depth
273          * @param parent the parent
274          * @param namespace the namespace
275          * @param apiVersion the api version
276          * @param url the url
277          * @param container the container
278          * @param aaiResources the aai resources
279          * @param dbMaps the db maps
280          * @param relationshipUtils the relationship utils
281          * @throws ClassNotFoundException the class not found exception
282          * @throws NoSuchFieldException the no such field exception
283          * @throws SecurityException the security exception
284          * @throws IOException Signals that an I/O exception has occurred.
285          */
286         private void lookAtDynamicResource(String resource, 
287                         String pojoBase, 
288                         DynamicJAXBContext jaxbContext, 
289                         DynamicTypeImpl t,
290                         int depth,
291                         String parent,
292                         String namespace,
293                         String apiVersion,
294                         String url, 
295                         boolean container, 
296                         AAIResources aaiResources,
297                         DbMaps dbMaps,
298                         String relationshipUtils
299                         ) 
300                                         throws ClassNotFoundException, NoSuchFieldException, SecurityException,  IOException {
301
302                 String className = pojoBase + "." + CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL, resource);
303
304                 AAIResource parentResource = aaiResources.getAaiResources().get(parent);
305
306                 AAIResources siblings = null;
307
308                 if (parentResource == null) { 
309                         String fullName = "/" +  resource;
310                         AAIResource aaiRes = new AAIResource();
311                         aaiRes.setFullName(fullName);
312                         aaiRes.setSimpleName(resource);
313                         aaiRes.setResourceType("container");
314                         aaiRes.setResourceClassName(className);
315                         aaiRes.setApiVersion(apiVersion);
316
317                         aaiResources.getAaiResources().put(fullName, aaiRes);
318                         parentResource = aaiRes;
319                 }
320
321                 if (depth >= 50) return;
322
323                 siblings = parentResource.getChildren();
324
325                 if (depth == 2) { 
326                         namespace = resource;
327                 }
328                 if (depth >= 50) {
329                         return;
330                 }
331
332                 /*              if ("Actions".equals(namespace) || "Search".equals(namespace)) {
333                         return;
334                 }*/
335
336                 ClassDescriptor cd = t.getDescriptor();
337
338                 createDbMapsfromOXM(cd.getProperties(), resource, dbMaps);
339
340                 
341
342                 Vector<DatabaseMapping> dm = cd.getMappings();          
343
344                 for (DatabaseMapping dmInst : dm) {
345                         String dmName = dmInst.getAttributeName();
346
347                         ClassDescriptor cd2 = dmInst.getReferenceDescriptor();
348                         if (cd2 != null) { 
349
350                                 String newClassName = cd2.getJavaClassName();
351                                 //                              
352                                 if (newClassName.contains("RelationshipList")) {
353                                         continue;
354                                 }
355
356                                 DynamicTypeImpl newDt = (DynamicTypeImpl)jaxbContext.getDynamicType(newClassName);
357
358                                 if (dmInst instanceof XMLCompositeCollectionMapping) { 
359                                         String simpleName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, dmName);
360                                         //                                      System.out.println(spaces + "+ List of A&AI Object named " + simpleName);
361
362                                         String hypName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, simpleName);
363
364                                         String fullName = parent + "/" + simpleName;
365
366                                         //Class<?> newClazz = Class.forName(newClassName);
367                                         AAIResource aaiRes = new AAIResource();
368
369                                         if ("cvlan-tag-entry".equals(hypName)) {
370                                         }
371
372                                         ClassDescriptor cd3 = newDt.getDescriptor();
373
374                                         boolean allowDirectWrite = true;
375                                         if (cd3.getProperties().containsKey("allowDirectWrite")) {
376                                                 if (cd3.getProperties().get("allowDirectWrite").equals("false")) {
377                                                         allowDirectWrite = false;
378                                                 }
379                                         }
380                                         
381
382                                         boolean allowDirectRead = true;
383                                         if (cd3.getProperties().containsKey("allowDirectRead")) {
384                                                 if (cd3.getProperties().get("allowDirectRead").equals("false")) {
385                                                         allowDirectRead = false;
386                                                 }
387                                         }
388                                         
389                                         List<DatabaseField> dbfList = cd3.getPrimaryKeyFields();
390                                         ArrayList<String> keyFields = new ArrayList<String>();
391
392                                         if (dbfList != null) { 
393                                                 for (DatabaseField dbf : dbfList) {
394                                                         String name = dbf.getName();
395                                                         name = name.substring(0, name.indexOf('/'));
396                                                         keyFields.add(name);
397                                                 }
398                                         }
399                                         Vector<DatabaseMapping> dm2 = cd3.getMappings();
400                                         for (DatabaseMapping dmInst2 : dm2) {
401                                                 String dmName2= CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmInst2.getAttributeName());
402                                                 DatabaseField xf2 = dmInst2.getField();
403                                                 if (dmInst2.getProperties().containsKey("autoGenerateUuid")) { 
404                                                         if (dmInst2.getProperties().get("autoGenerateUuid").equals("true")) { 
405                                                                 aaiRes.getAutoGenUuidFields().add(dmName2);
406                                                         }
407                                                 }
408                                                 if (xf2 instanceof XMLField) { 
409                                                         XMLField x = (XMLField)xf2;
410                                                         if (x != null) { 
411                                                                 if (x.isRequired()) {
412                                                                         aaiRes.getRequiredFields().add(dmName2);
413                                                                 }
414                                                         }
415                                                         
416                                                 }
417                                                 try {
418                                                         Class<?> xf2Clazz = xf2.getType();
419                                                         if (xf2Clazz.getSimpleName().equals("String")) {
420                                                                 if (dmInst2 instanceof XMLCompositeDirectCollectionMapping) { 
421                                                                         aaiRes.getStringListFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
422                                                                 } else { 
423                                                                         aaiRes.getStringFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
424                                                                 }
425                                                         } else if (xf2Clazz.getSimpleName().toLowerCase().contains("long")) {
426                                                                 aaiRes.getLongFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
427                                                         } else if (xf2Clazz.getSimpleName().toLowerCase().contains("int")) {
428                                                                 aaiRes.getIntFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
429                                                         } else if (xf2Clazz.getSimpleName().toLowerCase().contains("short")) {
430                                                                 aaiRes.getShortFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
431                                                         } else if (xf2Clazz.getSimpleName().toLowerCase().contains("boolean")) {
432                                                                 aaiRes.getBooleanFields().add(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmName2));
433                                                         }
434                                                 } catch (Exception e) {  // this xf2.getType() throws null pointer when I try to get the type and it doesn't have one
435                                                         ;
436                                                 }
437                                         }
438
439                                         // get the key(s) from DbRules
440                                         String uriKey = "";
441                                         LinkedHashMap<String, ArrayList<String>> itemKeyList = new LinkedHashMap<String, ArrayList<String>>();
442
443                                         aaiRes.setApiVersion(apiVersion);
444                                         itemKeyList.put(hypName, new ArrayList<String>());
445                                         for (String thisKey : keyFields) { 
446                                                 String pathParamName = hypName + "-" + thisKey;
447
448                                                 AAIResourceKey aaiResKey = new AAIResourceKey();
449                                                 aaiResKey.setKeyName(thisKey);
450                                                 aaiResKey.setDnCamKeyName(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, pathParamName));
451                                                 aaiResKey.setPathParamName(pathParamName);
452
453                                                 for (DatabaseMapping dmInst2 : dm2) {
454                                                         String dmName2= CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,dmInst2.getAttributeName());
455                                                         if (dmName2.equals(thisKey)){ 
456                                                                 DatabaseField xf2 = dmInst2.getField();
457                                                                 aaiResKey.setKeyType(xf2.getType().getSimpleName());
458                                                                 break;
459                                                         }
460                                                 }
461
462                                                 aaiRes.getAaiResourceKeys().getAaiResourceKey().add(aaiResKey);
463
464                                                 if (siblings != null) {
465                                                         siblings.getAaiResources().put(fullName, aaiRes);
466                                                 }
467
468                                                 uriKey += "/{" + pathParamName + "}";
469                                         }
470
471                                         String newUri = url + "/" + hypName + uriKey;
472
473
474                                         if ("v2".equals(apiVersion)) { 
475                                                 aaiRes.setResourceClassName("inventory.aai.openecomp.org." + simpleName);
476                                         } else { 
477                                                 aaiRes.setResourceClassName("inventory.aai.openecomp.org." + apiVersion + "." + simpleName);
478                                         }
479                                         
480                                         aaiRes.setAllowDirectWrite(allowDirectWrite);
481                                         aaiRes.setAllowDirectRead(allowDirectRead);
482                                         aaiRes.setNamespace(namespace);
483                                         aaiRes.setSimpleName(simpleName);
484                                         
485                                         if (!aaiResources.getResourceLookup().containsKey(simpleName)) {
486                                                 aaiResources.getResourceLookup().put(simpleName, aaiRes);
487                                         }
488                                         
489                                         aaiRes.setFullName(fullName);
490                                         aaiRes.setUri(newUri);
491                                         aaiRes.setResourceType("node");
492                                         if ("v2".equals(apiVersion)) { 
493                                                 aaiRes.setRelationshipListClass("inventory.aai.openecomp.org.RelationshipList");
494                                         } else { 
495                                                 aaiRes.setRelationshipListClass("inventory.aai.openecomp.org." + apiVersion + ".RelationshipList");
496                                         }
497                                         aaiRes.setRelationshipUtils(relationshipUtils);
498
499                                         if (parentResource != null) { 
500                                                 aaiRes.setParent(parentResource);
501                                         } else { 
502                                                 aaiRes.setParent(aaiRes);
503                                         }
504
505                                         aaiResources.getAaiResources().put(fullName, aaiRes);
506
507                                         if (siblings != null) {
508                                                 siblings.getAaiResources().put(fullName, aaiRes);
509                                         }
510 //                                      AAIResource ancestor = parentResource;
511 //
512 //                                      boolean recursiveEntity = false;
513 //                                      while (ancestor != null) { 
514 //
515 //                                              if (ancestor.getSimpleName().equals(aaiRes.getSimpleName())) {
516 //                                                      recursiveEntity = true;
517 //                                                      // attach it to the container that contains the resource above this one with the same name
518 //                                                      if (ancestor.getParent() != null && ancestor.getParent().getResourceType().equals("container")) {
519 //                                                              AAIResource recurseHere = ancestor.getParent();
520 //                                                              aaiRes.setRecurseToResource(recurseHere);
521 //                                                      }
522 //                                                      break;
523 //                                              }
524 //                                              ancestor = ancestor.getParent();
525 //
526 //                                      }
527 //                                      if (recursiveEntity == false) { 
528                                                 lookAtDynamicResource(cd2.getJavaClass().getSimpleName(),
529                                                                 pojoBase, 
530                                                                 jaxbContext, 
531                                                                 newDt,
532                                                                 (depth + 1), fullName, namespace, apiVersion, newUri, false, aaiResources, dbMaps,
533                                                                 relationshipUtils);
534 //                                      }
535                                 } else { 
536                                         String simpleName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, dmName);
537                                         String fullName = parent + "/" +  simpleName;
538                                         //                                      System.out.println(spaces + "+ Container of A&AI Object named " + simpleName);
539
540                                         AAIResource aaiRes = new AAIResource();
541                                         if (parentResource != null) { 
542                                                 aaiRes.setParent(parentResource);
543                                         } else { 
544                                                 aaiRes.setParent(aaiRes);
545                                         }
546                                         aaiRes.setAllowDirectWrite(true);
547                                         aaiRes.setAllowDirectRead(true);
548                                         aaiRes.setFullName(fullName);
549                                         aaiRes.setSimpleName(simpleName);
550                                         if (!aaiResources.getResourceLookup().containsKey(simpleName)) {
551                                                 aaiResources.getResourceLookup().put(simpleName, aaiRes);
552                                         }
553                                         aaiRes.setResourceType("container");
554
555                                         if ("v2".equals(apiVersion)) { 
556                                                 aaiRes.setResourceClassName("inventory.aai.openecomp.org." + simpleName);
557                                                 aaiRes.setRelationshipListClass("inventory.aai.openecomp.org.RelationshipList");
558                                         } else {
559                                                 aaiRes.setResourceClassName("inventory.aai.openecomp.org." + apiVersion + "." + simpleName);
560                                                 aaiRes.setRelationshipListClass("inventory.aai.openecomp.org." + apiVersion  + ".RelationshipList");
561                                         }
562                                         aaiRes.setApiVersion(apiVersion);
563
564                                         aaiResources.getAaiResources().put(fullName, aaiRes);
565                                         aaiRes.setRelationshipUtils(relationshipUtils);
566
567                                         String hypName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, simpleName);
568
569                                         if (siblings != null) {
570                                                 siblings.getAaiResources().put(fullName, aaiRes);
571                                         }
572
573                                         lookAtDynamicResource(cd2.getJavaClass().getSimpleName(),
574                                                         pojoBase, 
575                                                         jaxbContext, 
576                                                         (DynamicTypeImpl)jaxbContext.getDynamicType(newClassName),
577                                                         (depth + 1), fullName, namespace, apiVersion, url + "/" + hypName, false, aaiResources, dbMaps,
578                                                         relationshipUtils);
579
580
581                                 }
582                         }
583                 }
584         }
585
586         /**
587          * Creates the db mapsfrom OXM.
588          *
589          * @param propMap the prop map
590          * @param resource the resource
591          * @param dbMaps the db maps
592          */
593         private void createDbMapsfromOXM(Map<?, ?> propMap, String resource, DbMaps dbMaps) {
594                 String nodeType = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, resource);
595                 if ("cvlan-tag-entry".equals(nodeType)) {
596                         nodeType = "cvlan-tag";
597                 }
598
599                 // if we have nodes dependent on multiple nodes we might revisit the node again - skip then
600                 if (propMap.size() > 1 && !dbMaps.NodeMapIndexedProps.containsKey(nodeType)) {
601
602                         if (propMap.containsKey("nameProps")) 
603                                 dbMaps.NodeNameProps.putAll(nodeType, 
604                                                 (Iterable<String>) fromCommaSeparatedString(propMap.get("nameProps").toString()));
605
606                         if (propMap.containsKey("indexedProps")) 
607                                 dbMaps.NodeMapIndexedProps.putAll(nodeType, 
608                                                 (Iterable<String>) fromCommaSeparatedString(propMap.get("indexedProps").toString()));
609
610                         if (propMap.containsKey("dependentOn")) 
611                                 dbMaps.NodeDependencies.putAll(nodeType, 
612                                                 (Iterable<String>) fromCommaSeparatedString(propMap.get("dependentOn").toString()));
613
614                         if (propMap.containsKey("alternateKeys1")) 
615                                 dbMaps.NodeAltKey1Props.putAll(nodeType, 
616                                                 (Iterable<String>) fromCommaSeparatedString(propMap.get("alternateKeys1").toString()));
617
618                         if (propMap.containsKey("uniqueProps")) 
619                                 dbMaps.NodeMapUniqueProps.putAll(nodeType, 
620                                                 (Iterable<String>) fromCommaSeparatedString(propMap.get("uniqueProps").toString()));
621
622                         // build EdgeInfoMap
623                         if (propMap.containsKey("edgeInfo")) {
624                                 int i = 0;
625                                 Iterable<String> edgeInfoIterable = (Iterable<String>) fromCommaSeparatedString(propMap.get("edgeInfo").toString());
626                                 Iterator<String> edgeInfoIterator = edgeInfoIterable.iterator();
627                                 while(edgeInfoIterator.hasNext()) {
628                                         String propName = edgeInfoIterator.next();
629                                         dbMaps.EdgeInfoMap.put(i++, propName);
630                                 }
631                         }
632                 }
633         }
634
635         /**
636          * Creates the db mapsfrom AAI resources.
637          *
638          * @param aaiResources the aai resources
639          * @param dbMaps the db maps
640          */
641         private void createDbMapsfromAAIResources(AAIResources aaiResources, DbMaps dbMaps) {
642
643                 for (String  resource: aaiResources.getAaiResources().keySet()) {
644
645                         AAIResource aaiResource = aaiResources.getAaiResources().get(resource);
646                         String nodeType = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,aaiResource.getSimpleName());
647
648                         if (nodeType.equals("cvlan-tag-entry"))
649                                 nodeType = "cvlan-tag"; 
650
651                         // Build NodeNamespace
652                         if (aaiResource.getNamespace() != null && !aaiResource.getNamespace().equalsIgnoreCase("search")) 
653                                 // oamNetworks is also defined under the search namespace - do not want that namespace 
654                                 dbMaps.NodeNamespace.put(nodeType, CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, aaiResource.getNamespace()));
655
656                         // only process one nodetype once 
657                         if (dbMaps.NodeProps.containsKey(nodeType)) 
658                                 continue;
659
660                         // Build NodePlural
661                         if (aaiResource.getPluralName() != null && !aaiResource.getPluralName().equals(aaiResource.getNamespace())) 
662                                 // dont want resources which are namespaces themselves in map
663                                 dbMaps.NodePlural.put(nodeType, CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, aaiResource.getPluralName()));
664
665                         // Build NodeProps
666                         dbMaps.NodeProps.putAll(nodeType, aaiResource.getAllFields());
667
668                         // build ReservedNames
669                         if (nodeType.equalsIgnoreCase("reserved-prop-names")) {
670                                 for (String propName: aaiResource.getAllFields()) {
671                                         dbMaps.ReservedPropNames.put(propName, "");
672                                 }
673                         }
674
675                         // Build NodekeyProps
676                         AAIResourceKeys aaiResKeys = aaiResource.getAaiResourceKeys();
677                         List<String> keyList = new ArrayList<String>();
678                         for (AAIResourceKey rk : aaiResKeys.getAaiResourceKey()) { 
679                                 String keyProp = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN,rk.getKeyName());
680                                 if (!keyList.contains(keyProp))
681                                         keyList.add(keyProp);
682                         }                               
683                         dbMaps.NodeKeyProps.putAll(nodeType, (Iterable<String>)keyList);
684
685                         // Build PropertyDataTypeMap
686                         for (String propName: aaiResource.getBooleanFields()) {
687                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
688                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
689                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
690                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
691                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName))
692                                         dbMaps.PropertyDataTypeMap.put(propName, "Boolean");
693                                 else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("Boolean"))
694                                         System.out.println(propName + "defined with mis-matched types in oxm file");
695                         }
696                         for (String propName: aaiResource.getShortFields()) {
697                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
698                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
699                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
700                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
701                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName))
702                                         dbMaps.PropertyDataTypeMap.put(propName, "Integer");
703                                 else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("Integer"))
704                                         System.out.println(propName + "defined with mis-matched types in oxm file");
705                         }
706                         for (String propName: aaiResource.getLongFields()) {
707                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
708                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
709                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
710                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
711                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName)) {
712                                         if (propName.contains("-ts"))
713                                                 dbMaps.PropertyDataTypeMap.put(propName, "Long");
714                                         else
715                                                 dbMaps.PropertyDataTypeMap.put(propName, "Integer");
716                                 } else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("Integer"))
717                                         System.out.println(propName + "defined with mis-matched types in oxm file");
718                         }
719                         for (String propName: aaiResource.getIntFields()) {
720                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
721                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
722                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
723                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
724                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName))
725                                         dbMaps.PropertyDataTypeMap.put(propName, "Integer");
726                                 else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("Integer"))
727                                         System.out.println(propName + "defined with mis-matched types in oxm file");
728                         }
729                         for (String propName: aaiResource.getStringFields()) {
730                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
731                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
732                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
733                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
734                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName))
735                                         dbMaps.PropertyDataTypeMap.put(propName, "String");
736                                 else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("String"))
737                                         System.out.println(propName + "defined with mis-matched types in oxm file");
738                         }
739                         for (String propName: aaiResource.getStringListFields()) {
740                                 if (nodeType.equalsIgnoreCase("edge-prop-names")) // these properties are in mixed format in DB
741                                         propName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, propName);
742                                 if (propName.equals("sVCINFRA")) propName = "SVC-INFRA";
743                                 if (propName.equals("sVCINFRAREV")) propName = "SVC-INFRA-REV";
744                                 if (!dbMaps.PropertyDataTypeMap.containsKey(propName))
745                                         dbMaps.PropertyDataTypeMap.put(propName, "Set<String>");
746                                 else if (!dbMaps.PropertyDataTypeMap.get(propName).equals("Set<String>"))
747                                         System.out.println(propName + "defined with mis-matched types in oxm file");
748                         }
749                 }
750
751         }
752
753
754         /**
755          * Creates the property and node version info map from db maps.
756          */
757         private void createPropertyAndNodeVersionInfoMapFromDbMaps() {
758                 DbMaps dbMaps = null;
759                 String previousApiVersion = null;
760                 List<Integer> apiIntegerKeySet = new ArrayList<Integer>();
761                 for ( String vers : dbMapsContainer.keySet()) {
762                         apiIntegerKeySet.add(Integer.valueOf(vers.substring(1)));
763                 }
764                 ArrayList<Integer> apiIntegerVersionsList = (ArrayList<Integer>) asSortedList(apiIntegerKeySet);
765                 String apiVersion;
766                 for ( Integer apiIntegerVersion : apiIntegerVersionsList) {
767                         apiVersion = "v" + apiIntegerVersion;
768                         System.out.println("apiVersion=" + apiVersion);
769                         dbMaps = dbMapsContainer.get(apiVersion);
770
771                         if (previousApiVersion != null) { // when running more than one version
772                                 dbMaps.PropertyVersionInfoMap.putAll(dbMapsContainer.get(previousApiVersion).PropertyVersionInfoMap);
773                                 dbMaps.NodeVersionInfoMap.putAll(dbMapsContainer.get(previousApiVersion).NodeVersionInfoMap);
774                         }
775                         
776                         Iterator<String> nodeTypeIterator = dbMaps.NodeProps.keySet().iterator();
777                         while( nodeTypeIterator.hasNext() ){
778                                 String nType = nodeTypeIterator.next();
779                                 if (!dbMaps.NodeVersionInfoMap.containsKey(nType)) {
780                                         dbMaps.NodeVersionInfoMap.put(nType, apiVersion);
781                                 }
782                                 Collection <String> nodePropsForType =  dbMaps.NodeProps.get(nType);
783                                 Iterator <String> propIter = nodePropsForType.iterator();
784                                 while( propIter.hasNext() ){
785                                         String propName = propIter.next();
786                                         String infoKey = nType + "|" + propName;
787                                         if( ! dbMaps.PropertyVersionInfoMap.containsKey(infoKey) ){
788                                                 // We haven't seen this one yet -- add it in.
789                                                 dbMaps.PropertyVersionInfoMap.put(infoKey, apiVersion);
790                                         }
791                                 }
792                         }
793                         dbMapsContainer.put(apiVersion, dbMaps);
794                         previousApiVersion = apiVersion;
795                 }
796         }
797
798         /**
799          * As sorted list.
800          *
801          * @param <T> the generic type
802          * @param c the c
803          * @return the list
804          */
805         private <T extends Comparable<? super T>> List<T> asSortedList(Collection<T> c) {
806                 List<T> list = new ArrayList<T>(c);
807                 java.util.Collections.sort(list);
808                 return list;
809         }
810
811         /**
812          * From comma separated string.
813          *
814          * @param string the string
815          * @return the iterable
816          */
817         public Iterable<String> fromCommaSeparatedString( String string ) {
818                 Iterable<String> split = Splitter.on( "," ).omitEmptyStrings().trimResults().split( string );
819                 return split;
820         }
821
822         /**
823          * Pretty print map.
824          *
825          * @param map the map
826          * @return the string
827          */
828         public String prettyPrintMap(Multimap<String, String> map) {
829                 StringBuilder sb = new StringBuilder();
830                 sb.append('\n');
831                 for (String key:map.keySet()) {
832                         sb.append('\t');
833                         sb.append(key);
834                         sb.append('=').append('"');
835                         sb.append(map.get(key));
836                         sb.append('"');
837                         sb.append('\n');
838                 }
839                 return sb.toString();
840         }
841
842         /**
843          * Pretty print map.
844          *
845          * @param map the map
846          * @return the string
847          */
848         public String prettyPrintMap(Map<?, String> map) {
849                 StringBuilder sb = new StringBuilder();
850                 sb.append('\n');
851                 for (Object key:map.keySet()) {
852                         sb.append('\t');
853                         sb.append(key);
854                         sb.append('=').append('"');
855                         sb.append(map.get(key));
856                         sb.append('"');
857                         sb.append('\n');
858                 }
859                 return sb.toString();
860         }
861
862 }