56eada68c10388216ae67a48743e380b625e14c7
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / DbAdapter.java
1 package org.akraino.validation.ui.service;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import javax.annotation.Nonnull;
10
11 import org.akraino.validation.ui.dao.BlueprintDAO;
12 import org.akraino.validation.ui.dao.BlueprintInstanceDAO;
13 import org.akraino.validation.ui.dao.BlueprintLayerDAO;
14 import org.akraino.validation.ui.dao.LabDAO;
15 import org.akraino.validation.ui.dao.TimeslotDAO;
16 import org.akraino.validation.ui.dao.ValidationTestResultDAO;
17 import org.akraino.validation.ui.dao.WRobotTestResultDAO;
18 import org.akraino.validation.ui.data.JnksJobNotify;
19 import org.akraino.validation.ui.entity.Blueprint;
20 import org.akraino.validation.ui.entity.BlueprintInstance;
21 import org.akraino.validation.ui.entity.BlueprintLayer;
22 import org.akraino.validation.ui.entity.LabInfo;
23 import org.akraino.validation.ui.entity.Submission;
24 import org.akraino.validation.ui.entity.Timeslot;
25 import org.akraino.validation.ui.entity.ValidationDbTestResult;
26 import org.akraino.validation.ui.entity.WRobotDbTestResult;
27 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import org.springframework.transaction.annotation.Transactional;
31
32 import com.fasterxml.jackson.core.JsonParseException;
33 import com.fasterxml.jackson.databind.JsonMappingException;
34
35 @Service
36 @Transactional
37 public class DbAdapter {
38
39     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(DbAdapter.class);
40     private static final Object LOCK = new Object();
41
42     @Autowired
43     private ValidationTestResultDAO vTestResultDAO;
44
45     @Autowired
46     private WRobotTestResultDAO wRobotDAO;
47
48     @Autowired
49     private LabDAO labDAO;
50
51     @Autowired
52     private BlueprintLayerDAO layerDAO;
53
54     @Autowired
55     private BlueprintInstanceDAO bluInstDao;
56
57     @Autowired
58     private BlueprintDAO blueprintDAO;
59
60     @Autowired
61     private TimeslotDAO timeslotDAO;
62
63     @Autowired
64     DbSubmissionAdapter subService;
65
66     public void associateSubmissionWithValidationResult(Submission submission)
67             throws JsonParseException, JsonMappingException, IOException {
68         synchronized (LOCK) {
69             if (!compareBluInstances(submission.getValidationDbTestResult().getBlueprintInstance(),
70                     bluInstDao.getBlueprintInstance(
71                             submission.getValidationDbTestResult().getBlueprintInstance().getBlueprintInstanceId()))) {
72                 throw new RuntimeException("Blueprint instance data changed.");
73             }
74             if (!compareTimeslots(submission.getTimeslot(),
75                     timeslotDAO.getTimeslot(submission.getTimeslot().getTimeslotId()))) {
76                 throw new RuntimeException("Timeslot data changed.");
77             }
78
79             boolean canContinue = false;
80             for (Timeslot timeslot : bluInstDao
81                     .getBlueprintInstance(
82                             submission.getValidationDbTestResult().getBlueprintInstance().getBlueprintInstanceId())
83                     .getTimeslots()) {
84                 if (compareTimeslots(timeslot, submission.getTimeslot())) {
85                     canContinue = true;
86                     break;
87                 }
88             }
89             if (!canContinue) {
90                 throw new RuntimeException("Configured timeslot is no longer assigned to the blueprint instance.");
91             }
92             submission.getValidationDbTestResult().setSubmission(submission);
93             vTestResultDAO.saveOrUpdate(submission.getValidationDbTestResult());
94             if (submission.getValidationDbTestResult().getWRobotDbTestResults() != null) {
95                 for (WRobotDbTestResult vRobotDbResult : submission.getValidationDbTestResult()
96                         .getWRobotDbTestResults()) {
97                     vRobotDbResult.setValidationDbTestResult(submission.getValidationDbTestResult());
98                     wRobotDAO.saveOrUpdate(vRobotDbResult);
99                 }
100             }
101         }
102     }
103
104     public void storeResultsInDb(List<ValidationDbTestResult> vNexusResults) {
105         synchronized (LOCK) {
106             if (vNexusResults == null || vNexusResults.size() < 1) {
107                 return;
108             }
109             for (ValidationDbTestResult vNexusResult : vNexusResults) {
110                 if (vNexusResult.getWRobotDbTestResults() == null) {
111                     continue;
112                 }
113                 if (!checkValidityOfNexusResult(vNexusResult)) {
114                     continue;
115                 }
116                 LabInfo labInfo = labDAO.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
117                 ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(labInfo,
118                         vNexusResult.getTimestamp());
119                 if (vDbResult == null) {
120                     vDbResult = vNexusResult;
121                     vDbResult.setLab(labInfo);
122                     Blueprint blueprint = blueprintDAO
123                             .getBlueprint(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
124                     if (blueprint == null) {
125                         blueprint = vNexusResult.getBlueprintInstance().getBlueprint();
126                         blueprintDAO.saveOrUpdate(blueprint);
127                     }
128                     BlueprintInstance blueprintInst = bluInstDao.getBlueprintInstance(blueprint,
129                             (vNexusResult.getBlueprintInstance().getVersion()));
130                     if (blueprintInst == null) {
131                         blueprintInst = vNexusResult.getBlueprintInstance();
132                         blueprintInst.setBlueprint(blueprint);
133                         bluInstDao.saveOrUpdate(blueprintInst);
134                     }
135                     vDbResult.setBlueprintInstance(blueprintInst);
136                 }
137                 updateBlueInstLayers(vNexusResult);
138                 vDbResult.setResult(vNexusResult.getResult());
139                 vDbResult.setDateStorage(vNexusResult.getDateStorage());
140                 LOGGER.debug(EELFLoggerDelegate.debugLogger,
141                         "Storing validation test result with keys: blueprint name: "
142                                 + vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName() + ", version: "
143                                 + vNexusResult.getBlueprintInstance().getVersion() + ", lab: "
144                                 + vNexusResult.getLab().getSilo() + ", timestamp: " + vNexusResult.getTimestamp());
145                 vTestResultDAO.saveOrUpdate(vDbResult);
146                 List<org.akraino.validation.ui.entity.WRobotDbTestResult> wRobotDbResults = wRobotDAO
147                         .getWRobotTestResult(vDbResult);
148                 if (wRobotDbResults == null) {
149                     // Store the new wrapper robot rest results in db
150                     for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
151                         wNexusResult.setValidationDbTestResult(vDbResult);
152                         wRobotDAO.saveOrUpdate(wNexusResult);
153                     }
154                 } else if (vDbResult.getSubmission() != null) {
155                     // update validation result related to submission
156                     for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
157                         WRobotDbTestResult wRobotDbResult = wRobotDAO.getWRobotTestResult(wNexusResult.getLayer(),
158                                 vDbResult);
159                         wRobotDbResult.setRobotTestResults(wNexusResult.getRobotTestResults());
160                         wRobotDAO.saveOrUpdate(wRobotDbResult);
161                     }
162                 }
163             }
164         }
165     }
166
167     public void updateTimestamp(JnksJobNotify jnksJobNotify) {
168         synchronized (LOCK) {
169             if (!checkValidityOfJenkinsNotification(jnksJobNotify)) {
170                 return;
171             }
172             ValidationDbTestResult vDbSubmission = vTestResultDAO
173                     .getValidationTestResult(subService.getSubmission(String.valueOf(jnksJobNotify.getSubmissionId())));
174             ValidationDbTestResult vDbTimestamp = vTestResultDAO.getValidationTestResult(vDbSubmission.getLab(),
175                     jnksJobNotify.getTimestamp());
176             if (vDbTimestamp == null) {
177                 vDbSubmission.setTimestamp(jnksJobNotify.getTimestamp());
178                 vTestResultDAO.saveOrUpdate(vDbSubmission);
179                 return;
180             }
181             // Delete the wrobot results associated with submission validation result
182             List<WRobotDbTestResult> wRobotResults = wRobotDAO.getWRobotTestResult(vDbSubmission);
183             if (wRobotResults != null && wRobotResults.size() > 0) {
184                 for (WRobotDbTestResult wRobotResult : wRobotResults) {
185                     wRobotDAO.deleteWRobotTestResult(wRobotResult.getWRobotResultId());
186                 }
187             }
188             // Change the timestamp wrobot results to point to submission validation result
189             wRobotResults = wRobotDAO.getWRobotTestResult(vDbTimestamp);
190             if (wRobotResults != null && wRobotResults.size() > 0) {
191                 for (WRobotDbTestResult wRobotResult : wRobotResults) {
192                     wRobotResult.setValidationDbTestResult(vDbSubmission);
193                     wRobotDAO.saveOrUpdate(wRobotResult);
194                 }
195             }
196             vTestResultDAO.deleteValidationTestResult(vDbTimestamp);
197             // Now vDbSubmission can be updated
198             vDbSubmission.setDateStorage(vDbTimestamp.getDateStorage());
199             vDbSubmission.setResult(vDbTimestamp.getResult());
200             vDbSubmission.setTimestamp(vDbTimestamp.getTimestamp());
201             vTestResultDAO.saveOrUpdate(vDbSubmission);
202         }
203     }
204
205     public List<ValidationDbTestResult> readResultFromDb(String blueprintName, String version, String lab,
206             List<String> layers, Boolean allLayers, Boolean optional, Boolean outcome)
207                     throws JsonParseException, JsonMappingException, IOException {
208         synchronized (LOCK) {
209             LabInfo actualLabInfo = null;
210             if (lab != null) {
211                 actualLabInfo = labDAO.getLab(lab);
212                 if (actualLabInfo == null) {
213                     return null;
214                 }
215             }
216             Blueprint blueprint = null;
217             if (blueprintName != null) {
218                 blueprint = blueprintDAO.getBlueprint(blueprintName);
219                 if (blueprint == null) {
220                     return null;
221                 }
222             }
223             BlueprintInstance blueprintInst = bluInstDao.getBlueprintInstance(blueprint, version);
224             if (blueprintInst == null) {
225                 return null;
226             }
227             List<ValidationDbTestResult> vDbResults = vTestResultDAO.getValidationTestResults(blueprintInst,
228                     actualLabInfo, allLayers, optional, outcome);
229             if (vDbResults == null || vDbResults.size() < 1) {
230                 return null;
231             }
232             List<ValidationDbTestResult> actualResults = new ArrayList<ValidationDbTestResult>();
233             for (ValidationDbTestResult vDbResult : vDbResults) {
234                 if (layers != null && layers.size() > 0) {
235                     List<String> storedLayers = new ArrayList<String>();
236                     List<WRobotDbTestResult> wDbResults = wRobotDAO.getWRobotTestResult(vDbResult);
237                     if (wDbResults == null || wDbResults.size() < 1) {
238                         continue;
239                     }
240                     for (WRobotDbTestResult wRobot : wDbResults) {
241                         storedLayers.add(wRobot.getLayer());
242                     }
243                     if (!new HashSet<>(storedLayers).equals(new HashSet<>(layers))) {
244                         continue;
245                     }
246                 }
247                 actualResults.add(vDbResult);
248             }
249             return actualResults;
250         }
251     }
252
253     public ValidationDbTestResult readResultFromDb(@Nonnull String lab, @Nonnull String timestamp)
254             throws JsonParseException, JsonMappingException, IOException {
255         synchronized (LOCK) {
256             LabInfo actualLabInfo = labDAO.getLab(lab);
257             ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(actualLabInfo, timestamp);
258             if (vDbResult == null) {
259                 return null;
260             }
261             return vDbResult;
262         }
263     }
264
265     public ValidationDbTestResult readResultFromDb(@Nonnull String submissionId)
266             throws JsonParseException, JsonMappingException, IOException {
267         synchronized (LOCK) {
268             return vTestResultDAO.getValidationTestResult(subService.getSubmission(submissionId));
269         }
270     }
271
272     public void deleteUnreferencedEntries(List<ValidationDbTestResult> vNexusResults) {
273         synchronized (LOCK) {
274             if (vNexusResults == null || vNexusResults.size() < 1) {
275                 return;
276             }
277             LabInfo labInfo = labDAO.getLabBasedOnSilo(vNexusResults.get(0).getLab().getSilo());
278             if (labInfo == null) {
279                 return;
280             }
281             Blueprint blueprint = blueprintDAO
282                     .getBlueprint(vNexusResults.get(0).getBlueprintInstance().getBlueprint().getBlueprintName());
283             if (blueprint == null) {
284                 return;
285             }
286             BlueprintInstance blueInst = bluInstDao.getBlueprintInstance(blueprint,
287                     vNexusResults.get(0).getBlueprintInstance().getVersion());
288             if (blueInst == null) {
289                 return;
290             }
291             List<ValidationDbTestResult> vDbResults = vTestResultDAO.getValidationTestResults(blueInst, labInfo, null,
292                     null, null);
293             if (vDbResults == null || vDbResults.size() < 1) {
294                 return;
295             }
296             for (ValidationDbTestResult vDbResult : vDbResults) {
297                 if (vDbResult.getSubmission() != null) {
298                     continue;
299                 }
300                 boolean deletion = true;
301                 String dbTimestamp = vDbResult.getTimestamp();
302                 LabInfo dbLabInfo = vDbResult.getLab();
303                 for (ValidationDbTestResult vNexusResult : vNexusResults) {
304                     LabInfo nexusLabInfo = labDAO.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
305                     if (nexusLabInfo == null) {
306                         continue;
307                     }
308                     if (vNexusResult.getTimestamp().equals(dbTimestamp) && nexusLabInfo.equals(dbLabInfo)) {
309                         deletion = false;
310                         break;
311                     }
312                 }
313                 if (deletion) {
314                     LOGGER.debug(EELFLoggerDelegate.debugLogger,
315                             "Deleting unreferenced validation result with id: " + vDbResult.getResultId());
316                     // Delete old associated wrapper robot rest results from db
317                     for (WRobotDbTestResult wRobotDbResult : wRobotDAO.getWRobotTestResult(vDbResult)) {
318                         wRobotDAO.deleteWRobotTestResult(wRobotDbResult.getWRobotResultId());
319                     }
320                     vTestResultDAO.deleteValidationTestResult(vDbResult);
321                 }
322             }
323         }
324     }
325
326     public List<ValidationDbTestResult> getValidationTestResults() {
327         synchronized (LOCK) {
328             return vTestResultDAO.getValidationTestResults();
329         }
330     }
331
332     public ValidationDbTestResult getValidationTestResult(Integer resultId) {
333         synchronized (LOCK) {
334             return vTestResultDAO.getValidationTestResult(resultId);
335         }
336     }
337
338     public List<ValidationDbTestResult> getValidationTestResults(String blueprintName, @Nonnull String version,
339             LabInfo labInfo, Boolean allLayers, Boolean optional, Boolean outcome) {
340         synchronized (LOCK) {
341             Blueprint blueprint = null;
342             if (blueprintName != null) {
343                 blueprint = blueprintDAO.getBlueprint(blueprintName);
344                 if (blueprint == null) {
345                     return null;
346                 }
347             }
348             BlueprintInstance bluInst = bluInstDao.getBlueprintInstance(blueprint, version);
349             if (bluInst == null) {
350                 return null;
351             }
352             return vTestResultDAO.getValidationTestResults(bluInst, labInfo, allLayers, optional, outcome);
353         }
354     }
355
356     public ValidationDbTestResult getValidationTestResult(LabInfo labInfo, String timestamp) {
357         synchronized (LOCK) {
358             return vTestResultDAO.getValidationTestResult(labInfo, timestamp);
359         }
360     }
361
362     public ValidationDbTestResult getValidationTestResult(String labSilo, String timestamp) {
363         synchronized (LOCK) {
364             return vTestResultDAO.getValidationTestResult(labDAO.getLabBasedOnSilo(labSilo), timestamp);
365         }
366     }
367
368     public ValidationDbTestResult getValidationTestResult(@Nonnull Submission submission) {
369         synchronized (LOCK) {
370             return vTestResultDAO.getValidationTestResult(submission);
371         }
372     }
373
374     public List<WRobotDbTestResult> getWRobotTestResults() {
375         synchronized (LOCK) {
376             return wRobotDAO.getWRobotTestResults();
377         }
378     }
379
380     public WRobotDbTestResult getWRobotTestResult(Integer wRobotResultId) {
381         synchronized (LOCK) {
382             return wRobotDAO.getWRobotTestResult(wRobotResultId);
383         }
384     }
385
386     public List<WRobotDbTestResult> getWRobotTestResult(ValidationDbTestResult vResult) {
387         synchronized (LOCK) {
388             return wRobotDAO.getWRobotTestResult(vResult);
389         }
390     }
391
392     public void saveLab(LabInfo lab) {
393         synchronized (LOCK) {
394             labDAO.saveOrUpdate(lab);
395         }
396     }
397
398     public void deleteLab(LabInfo lab) {
399         synchronized (LOCK) {
400             labDAO.deleteLab(lab);
401         }
402     }
403
404     public LabInfo getLab(String lab) {
405         synchronized (LOCK) {
406             return labDAO.getLab(lab);
407         }
408     }
409
410     public LabInfo getLabBasedOnSilo(String silo) {
411         synchronized (LOCK) {
412             return labDAO.getLabBasedOnSilo(silo);
413         }
414     }
415
416     public List<LabInfo> getLabs() {
417         synchronized (LOCK) {
418             return labDAO.getLabs();
419         }
420     }
421
422     public void deleteLabAll() {
423         synchronized (LOCK) {
424             labDAO.deleteAll();
425         }
426     }
427
428     public void saveBlueprintInstance(BlueprintInstance blueprintIns) {
429         synchronized (LOCK) {
430             Set<Timeslot> timeslots = blueprintIns.getTimeslots();
431             if (timeslots != null && timeslots.size() > 1) {
432                 for (Timeslot timeslot : timeslots) {
433                     if (!compareTimeslots(timeslot, timeslotDAO.getTimeslot(timeslot.getTimeslotId()))) {
434                         throw new RuntimeException("Timeslot instance data changed.");
435                     }
436                 }
437                 bluInstDao.merge(blueprintIns);
438                 return;
439             }
440             bluInstDao.saveOrUpdate(blueprintIns);
441         }
442     }
443
444     public List<BlueprintInstance> getBlueprintInstances() {
445         synchronized (LOCK) {
446             return bluInstDao.getBlueprintInstances();
447         }
448     }
449
450     public BlueprintInstance getBlueprintInstance(int instId) {
451         synchronized (LOCK) {
452             return bluInstDao.getBlueprintInstance(instId);
453         }
454     }
455
456     public BlueprintInstance getBlueprintInstance(Blueprint blueprint, String version) {
457         synchronized (LOCK) {
458             return bluInstDao.getBlueprintInstance(blueprint, version);
459         }
460     }
461
462     public void deleteBlueprintInstance(BlueprintInstance blueprintIns) {
463         synchronized (LOCK) {
464             bluInstDao.deleteBlueprintInstance(blueprintIns);
465         }
466     }
467
468     public void deleteBluInstAll() {
469         synchronized (LOCK) {
470             bluInstDao.deleteAll();
471         }
472     }
473
474     public void saveBlueprintLayer(BlueprintLayer layer) {
475         synchronized (LOCK) {
476             layerDAO.saveOrUpdate(layer);
477         }
478     }
479
480     public BlueprintLayer getBlueprintLayer(Integer layerId) {
481         synchronized (LOCK) {
482             return layerDAO.getBlueprintLayer(layerId);
483         }
484     }
485
486     public BlueprintLayer getBlueprintLayer(String layerData) {
487         synchronized (LOCK) {
488             return layerDAO.getBlueprintLayer(layerData);
489         }
490     }
491
492     public List<BlueprintLayer> getBlueprintLayers() {
493         synchronized (LOCK) {
494             return layerDAO.getBlueprintLayers();
495         }
496     }
497
498     public void deleteBlueprintLayer(BlueprintLayer layer) {
499         synchronized (LOCK) {
500             layerDAO.deleteBlueprintLayer(layer);
501         }
502     }
503
504     public void deleteBluLayersAll() {
505         synchronized (LOCK) {
506             layerDAO.deleteAll();
507         }
508     }
509
510     public void saveBlueprint(Blueprint blueprint) {
511         synchronized (LOCK) {
512             blueprintDAO.saveOrUpdate(blueprint);
513         }
514     }
515
516     public Blueprint getBlueprint(String name) {
517         synchronized (LOCK) {
518             return blueprintDAO.getBlueprint(name);
519         }
520     }
521
522     public List<Blueprint> getBlueprints() {
523         synchronized (LOCK) {
524             return blueprintDAO.getBlueprints();
525         }
526     }
527
528     public void deleteBlueprint(Blueprint blueprint) {
529         synchronized (LOCK) {
530             blueprintDAO.deleteBlueprint(blueprint);
531         }
532     }
533
534     public void deleteBluAll() {
535         synchronized (LOCK) {
536             blueprintDAO.deleteAll();
537         }
538     }
539
540     public void saveTimeslot(Timeslot timeslot) {
541         synchronized (LOCK) {
542             LabInfo labInfo = timeslot.getLabInfo();
543             if (labInfo != null) {
544                 if (!compareLabInfos(labInfo, labDAO.getLab(labInfo.getLabId()))) {
545                     throw new RuntimeException("Lab data changed.");
546                 }
547             }
548             timeslotDAO.saveOrUpdate(timeslot);
549         }
550     }
551
552     public List<Timeslot> getTimeslots() {
553         synchronized (LOCK) {
554             return timeslotDAO.getTimeslots();
555         }
556     }
557
558     public void deleteTimeslot(Timeslot timeslot) {
559         synchronized (LOCK) {
560             timeslotDAO.deleteTimeslot(timeslot);
561         }
562     }
563
564     public void deleteTimeslotAll() {
565         synchronized (LOCK) {
566             timeslotDAO.deleteAll();
567         }
568     }
569
570     public boolean checkValidityOfNexusResult(ValidationDbTestResult vNexusResult) {
571         if (vNexusResult == null) {
572             return true;
573         }
574         LabInfo labInfo = labDAO.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
575         if (labInfo == null) {
576             throw new RuntimeException("Lab silo : " + vNexusResult.getLab().getSilo() + " not found");
577         }
578         ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(
579                 labDAO.getLabBasedOnSilo(vNexusResult.getLab().getSilo()), vNexusResult.getTimestamp());
580         Blueprint blueprint = null;
581         BlueprintInstance bluInst = null;
582         List<WRobotDbTestResult> wRobotDbResults = null;
583         if (vDbResult != null) {
584             blueprint = vDbResult.getBlueprintInstance().getBlueprint();
585             labInfo = vDbResult.getLab();
586             wRobotDbResults = wRobotDAO.getWRobotTestResult(vDbResult);
587         } else {
588             blueprint = blueprintDAO
589                     .getBlueprint(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
590         }
591         if (blueprint != null) {
592             if (vDbResult != null) {
593                 bluInst = vDbResult.getBlueprintInstance();
594             } else {
595                 bluInst = bluInstDao.getBlueprintInstance(blueprint, vNexusResult.getBlueprintInstance().getVersion());
596             }
597         }
598         // Start comparison, be elastic with allLayers and optional
599         if (!labInfo.getSilo().equals(vNexusResult.getLab().getSilo())) {
600             LOGGER.error(EELFLoggerDelegate.errorLogger,
601                     "Nexus has different data for blueprint : "
602                             + vDbResult.getBlueprintInstance().getBlueprint().getBlueprintName() + ", version: "
603                             + vDbResult.getBlueprintInstance().getVersion() + " and lab: " + vDbResult.getLab().getLab()
604                             + ". Lab inconsistency : " + vDbResult.getLab() + " " + labInfo);
605             return false;
606         }
607         if (blueprint != null) {
608             if (!blueprint.getBlueprintName()
609                     .equals(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName())) {
610                 LOGGER.error(EELFLoggerDelegate.errorLogger,
611                         "Nexus has different data for blueprint : " + blueprint.getBlueprintName()
612                         + ". Name inconsistency : " + blueprint.getBlueprintName() + " "
613                         + vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
614                 return false;
615             }
616         }
617         if (bluInst != null) {
618             if (!bluInst.getVersion().equals(vNexusResult.getBlueprintInstance().getVersion())) {
619                 LOGGER.error(EELFLoggerDelegate.errorLogger,
620                         "Nexus has different data for blueprint : " + bluInst.getBlueprint().getBlueprintName()
621                         + ", version: " + bluInst.getVersion() + ". Version inconsistency : "
622                         + bluInst.getVersion() + " " + vNexusResult.getBlueprintInstance().getVersion());
623                 return false;
624             }
625         }
626         if (wRobotDbResults != null) {
627             List<String> storedLayers1 = new ArrayList<String>();
628             for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
629                 storedLayers1.add(wNexusResult.getLayer());
630             }
631             List<String> storedLayers2 = new ArrayList<String>();
632             for (WRobotDbTestResult wDbResult : wRobotDbResults) {
633                 storedLayers2.add(wDbResult.getLayer());
634             }
635             if (!new HashSet<>(storedLayers1).equals(new HashSet<>(storedLayers2))) {
636                 LOGGER.error(EELFLoggerDelegate.errorLogger,
637                         "Nexus has different layer results for validation result id: " + vDbResult.getResultId());
638                 return false;
639             }
640         }
641         return true;
642     }
643
644     private boolean checkValidityOfJenkinsNotification(JnksJobNotify jnksJobNotify) {
645         ValidationDbTestResult vDbSubmission = vTestResultDAO
646                 .getValidationTestResult(subService.getSubmission(String.valueOf(jnksJobNotify.getSubmissionId())));
647         if (vDbSubmission == null) {
648             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received timestamp for submission id : "
649                     + jnksJobNotify.getSubmissionId() + " which has not validation result associated with it");
650             return false;
651         }
652         if (!vDbSubmission.getAllLayers() && (vDbSubmission.getWRobotDbTestResults() == null
653                 || vDbSubmission.getWRobotDbTestResults().size() < 1)) {
654             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received timestamp for submission id : "
655                     + jnksJobNotify.getSubmissionId() + " which is not stored correctly");
656             return false;
657         }
658         ValidationDbTestResult vDbTimestamp = vTestResultDAO.getValidationTestResult(vDbSubmission.getLab(),
659                 jnksJobNotify.getTimestamp());
660         if (vDbTimestamp == null) {
661             return true;
662         }
663         if (vDbTimestamp.equals(vDbSubmission) || (vDbTimestamp.getSubmission() != null
664                 && !jnksJobNotify.getSubmissionId().equals(vDbTimestamp.getSubmission().getSubmissionId()))) {
665             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received same timestamp: " + jnksJobNotify.getTimestamp()
666             + " from nexus for submission id: " + jnksJobNotify.getSubmissionId());
667             return false;
668         }
669         if (!vDbSubmission.getAllLayers()) {
670             if (wRobotDAO.getWRobotTestResult(vDbSubmission).size() != wRobotDAO.getWRobotTestResult(vDbTimestamp)
671                     .size()) {
672                 LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in stored layers records.");
673                 return false;
674             }
675             List<String> storedLayers1 = new ArrayList<String>();
676             List<String> storedLayers2 = new ArrayList<String>();
677             List<WRobotDbTestResult> wDbResults = wRobotDAO.getWRobotTestResult(vDbSubmission);
678             for (WRobotDbTestResult wRobot : wDbResults) {
679                 storedLayers1.add(wRobot.getLayer());
680             }
681             wDbResults = wRobotDAO.getWRobotTestResult(vDbTimestamp);
682             for (WRobotDbTestResult wRobot : wDbResults) {
683                 storedLayers2.add(wRobot.getLayer());
684             }
685             if (!new HashSet<>(storedLayers1).equals(new HashSet<>(storedLayers2))) {
686                 LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in stored layers records.");
687                 return false;
688             }
689         }
690         // Be elastic with allLayers and optional
691         if (!vDbSubmission.getBlueprintInstance().getBlueprint().getBlueprintName()
692                 .equals(vDbTimestamp.getBlueprintInstance().getBlueprint().getBlueprintName())
693                 || !vDbSubmission.getBlueprintInstance().getVersion()
694                 .equals(vDbTimestamp.getBlueprintInstance().getVersion())
695                 || !compareLabInfos(vDbSubmission.getLab(), vDbTimestamp.getLab())) {
696             LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in database records.");
697             return false;
698         }
699         return true;
700     }
701
702     private void updateBlueInstLayers(ValidationDbTestResult vNexusResult) {
703         for (BlueprintInstance blueprintInst : bluInstDao.getBlueprintInstances()) {
704             if (!blueprintInst.getBlueprint().getBlueprintName()
705                     .equals(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName())) {
706                 continue;
707             }
708             Set<BlueprintLayer> blueprintLayers = blueprintInst.getBlueprintLayers();
709             if (blueprintLayers == null) {
710                 blueprintLayers = new HashSet<BlueprintLayer>();
711             }
712             for (WRobotDbTestResult nexusResult : vNexusResult.getWRobotDbTestResults()) {
713                 BlueprintLayer layer = layerDAO.getBlueprintLayer(nexusResult.getLayer());
714                 if (layer == null) {
715                     layer = new BlueprintLayer();
716                     layer.setLayer(nexusResult.getLayer());
717                     layerDAO.saveOrUpdate(layer);
718                 }
719                 if (!blueprintLayers.contains(layer)) {
720                     blueprintLayers.add(layer);
721                 }
722             }
723             blueprintInst.setBlueprintLayers(blueprintLayers);
724             bluInstDao.saveOrUpdate(blueprintInst);
725         }
726     }
727
728     private boolean compareBluInstances(BlueprintInstance inst1, BlueprintInstance inst2) {
729         if (!inst1.getVersion().equals(inst2.getVersion())) {
730             return false;
731         }
732         if (inst1.getBlueprintInstanceId() != inst2.getBlueprintInstanceId()) {
733             return false;
734         }
735         Set<BlueprintLayer> layers1 = inst1.getBlueprintLayers();
736         Set<BlueprintLayer> layers2 = inst2.getBlueprintLayers();
737         if (!(layers1 == null && layers2 == null)) {
738             if (layers1 != null && layers2 == null) {
739                 return false;
740             }
741             if (layers1 == null && layers2 != null) {
742                 return false;
743             }
744             if (!(layers1.size() == layers2.size())) {
745                 return false;
746             }
747             boolean overallLayerEquality = true;
748             for (BlueprintLayer blulayer1 : layers1) {
749                 boolean layerEquality = false;
750                 for (BlueprintLayer blulayer2 : layers2) {
751                     if (blulayer1.getLayer().equals(blulayer2.getLayer())) {
752                         layerEquality = true;
753                     }
754                 }
755                 if (!layerEquality) {
756                     overallLayerEquality = false;
757                     break;
758                 }
759             }
760             if (!overallLayerEquality) {
761                 return false;
762             }
763         }
764         if (!compareBlueprints(inst1.getBlueprint(), inst2.getBlueprint())) {
765             return false;
766         }
767         return true;
768     }
769
770     private boolean compareTimeslots(Timeslot timeslot1, Timeslot timeslot2) {
771         if (!timeslot1.getStartDateTime().equals(timeslot2.getStartDateTime())
772                 || timeslot1.getTimeslotId() != timeslot2.getTimeslotId()) {
773             return false;
774         }
775         if (!compareLabInfos(timeslot1.getLabInfo(), timeslot2.getLabInfo())) {
776             return false;
777         }
778         return true;
779     }
780
781     private boolean compareBlueprints(Blueprint blueprint1, Blueprint blueprint2) {
782         if (blueprint1 != null || blueprint2 != null) {
783             if (blueprint1 != null && blueprint2 == null) {
784                 return false;
785             }
786             if (blueprint1 == null && blueprint2 != null) {
787                 return false;
788             }
789             if (blueprint1.getBlueprintId() != blueprint2.getBlueprintId()) {
790                 return false;
791             }
792             if (!blueprint1.getBlueprintName().equals(blueprint2.getBlueprintName())) {
793                 return false;
794             }
795         }
796         return true;
797     }
798
799     private boolean compareLabInfos(LabInfo labInfo1, LabInfo labInfo2) {
800         if (labInfo1 != null || labInfo2 != null) {
801             if (labInfo1 != null && labInfo2 == null) {
802                 return false;
803             }
804             if (labInfo1 == null && labInfo2 != null) {
805                 return false;
806             }
807             if (labInfo1.getLabId() != labInfo2.getLabId()) {
808                 return false;
809             }
810             if (!labInfo1.getSilo().equals(labInfo2.getSilo())) {
811                 return false;
812             }
813             if (!labInfo1.getLab().equals(labInfo2.getLab())) {
814                 return false;
815             }
816         }
817         return true;
818     }
819
820 }