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