dcb3bac84c761299e88e2ef81f8ec83b6654a6bc
[validation.git] / ui / src / main / java / org / akraino / validation / ui / service / DbResultAdapter.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.ValidationTestResultDAO;
12 import org.akraino.validation.ui.dao.WRobotTestResultDAO;
13 import org.akraino.validation.ui.data.JnksJobNotify;
14 import org.akraino.validation.ui.entity.Blueprint;
15 import org.akraino.validation.ui.entity.BlueprintInstance;
16 import org.akraino.validation.ui.entity.BlueprintLayer;
17 import org.akraino.validation.ui.entity.LabInfo;
18 import org.akraino.validation.ui.entity.Submission;
19 import org.akraino.validation.ui.entity.ValidationDbTestResult;
20 import org.akraino.validation.ui.entity.WRobotDbTestResult;
21 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.stereotype.Service;
24 import org.springframework.transaction.annotation.Transactional;
25
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28
29 @Service
30 @Transactional
31 public class DbResultAdapter {
32
33     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(DbResultAdapter.class);
34     private static final Object LOCK = new Object();
35
36     @Autowired
37     LabService labService;
38
39     @Autowired
40     private ValidationTestResultDAO vTestResultDAO;
41
42     @Autowired
43     private WRobotTestResultDAO wRobotDAO;
44
45     @Autowired
46     DbSubmissionAdapter subService;
47
48     @Autowired
49     BlueprintService blueprintService;
50
51     @Autowired
52     BlueprintInstanceService blueprintInstService;
53
54     @Autowired
55     BlueprintLayerService layerService;
56
57     public void associateSubmissionWithValidationResult(Submission submission)
58             throws JsonParseException, JsonMappingException, IOException {
59         synchronized (LOCK) {
60             if (!compareBluInstances(submission.getValidationDbTestResult().getBlueprintInstance(),
61                     blueprintInstService.getBlueprintInstance(
62                             submission.getValidationDbTestResult().getBlueprintInstance().getBlueprintInstanceId()))) {
63                 throw new RuntimeException("Blueprint instance data changed.");
64             }
65             submission.getValidationDbTestResult().setSubmission(submission);
66             vTestResultDAO.saveOrUpdate(submission.getValidationDbTestResult());
67             if (submission.getValidationDbTestResult().getWRobotDbTestResults() != null) {
68                 for (WRobotDbTestResult vRobotDbResult : submission.getValidationDbTestResult()
69                         .getWRobotDbTestResults()) {
70                     vRobotDbResult.setValidationDbTestResult(submission.getValidationDbTestResult());
71                     wRobotDAO.saveOrUpdate(vRobotDbResult);
72                 }
73             }
74         }
75     }
76
77     public void storeResultsInDb(List<ValidationDbTestResult> vNexusResults) {
78         synchronized (LOCK) {
79             if (vNexusResults == null || vNexusResults.size() < 1) {
80                 return;
81             }
82             for (ValidationDbTestResult vNexusResult : vNexusResults) {
83                 if (vNexusResult.getWRobotDbTestResults() == null) {
84                     continue;
85                 }
86                 if (!checkValidityOfNexusResult(vNexusResult)) {
87                     continue;
88                 }
89                 LabInfo labInfo = labService.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
90                 ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(labInfo,
91                         vNexusResult.getTimestamp());
92                 if (vDbResult == null) {
93                     vDbResult = vNexusResult;
94                     vDbResult.setLab(labInfo);
95                     Blueprint blueprint = blueprintService
96                             .getBlueprint(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
97                     if (blueprint == null) {
98                         blueprint = vNexusResult.getBlueprintInstance().getBlueprint();
99                         blueprintService.saveBlueprint(blueprint);
100                     }
101                     BlueprintInstance blueprintInst = blueprintInstService.getBlueprintInstance(blueprint,
102                             (vNexusResult.getBlueprintInstance().getVersion()));
103                     if (blueprintInst == null) {
104                         blueprintInst = vNexusResult.getBlueprintInstance();
105                         blueprintInst.setBlueprint(blueprint);
106                         blueprintInstService.saveBlueprintInstance(blueprintInst);
107                     }
108                     vDbResult.setBlueprintInstance(blueprintInst);
109                 }
110                 updateBlueInstLayers(vNexusResult);
111                 vDbResult.setResult(vNexusResult.getResult());
112                 vDbResult.setDateStorage(vNexusResult.getDateStorage());
113                 LOGGER.debug(EELFLoggerDelegate.debugLogger,
114                         "Storing validation test result with keys: blueprint name: "
115                                 + vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName() + ", version: "
116                                 + vNexusResult.getBlueprintInstance().getVersion() + ", lab: "
117                                 + vNexusResult.getLab().getSilo() + ", timestamp: " + vNexusResult.getTimestamp());
118                 vTestResultDAO.saveOrUpdate(vDbResult);
119                 List<org.akraino.validation.ui.entity.WRobotDbTestResult> wRobotDbResults = wRobotDAO
120                         .getWRobotTestResult(vDbResult);
121                 if (wRobotDbResults == null) {
122                     // Store the new wrapper robot rest results in db
123                     for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
124                         wNexusResult.setValidationDbTestResult(vDbResult);
125                         wRobotDAO.saveOrUpdate(wNexusResult);
126                     }
127                 } else if (vDbResult.getSubmission() != null) {
128                     // update validation result related to submission
129                     for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
130                         WRobotDbTestResult wRobotDbResult = wRobotDAO.getWRobotTestResult(wNexusResult.getLayer(),
131                                 vDbResult);
132                         wRobotDbResult.setRobotTestResults(wNexusResult.getRobotTestResults());
133                         wRobotDAO.saveOrUpdate(wRobotDbResult);
134                     }
135                 }
136             }
137         }
138     }
139
140     public void updateTimestamp(JnksJobNotify jnksJobNotify) {
141         synchronized (LOCK) {
142             if (!checkValidityOfJenkinsNotification(jnksJobNotify)) {
143                 return;
144             }
145             ValidationDbTestResult vDbSubmission = vTestResultDAO
146                     .getValidationTestResult(subService.getSubmission(String.valueOf(jnksJobNotify.getSubmissionId())));
147             ValidationDbTestResult vDbTimestamp = vTestResultDAO.getValidationTestResult(vDbSubmission.getLab(),
148                     jnksJobNotify.getTimestamp());
149             if (vDbTimestamp == null) {
150                 vDbSubmission.setTimestamp(jnksJobNotify.getTimestamp());
151                 vTestResultDAO.saveOrUpdate(vDbSubmission);
152                 return;
153             }
154             // Delete the wrobot results associated with submission validation result
155             List<WRobotDbTestResult> wRobotResults = wRobotDAO.getWRobotTestResult(vDbSubmission);
156             if (wRobotResults != null && wRobotResults.size() > 0) {
157                 for (WRobotDbTestResult wRobotResult : wRobotResults) {
158                     wRobotDAO.deleteWRobotTestResult(wRobotResult.getWRobotResultId());
159                 }
160             }
161             // Change the timestamp wrobot results to point to submission validation result
162             wRobotResults = wRobotDAO.getWRobotTestResult(vDbTimestamp);
163             if (wRobotResults != null && wRobotResults.size() > 0) {
164                 for (WRobotDbTestResult wRobotResult : wRobotResults) {
165                     wRobotResult.setValidationDbTestResult(vDbSubmission);
166                     wRobotDAO.saveOrUpdate(wRobotResult);
167                 }
168             }
169             vTestResultDAO.deleteValidationTestResult(vDbTimestamp);
170             // Now vDbSubmission can be updated
171             vDbSubmission.setDateStorage(vDbTimestamp.getDateStorage());
172             vDbSubmission.setResult(vDbTimestamp.getResult());
173             vDbSubmission.setTimestamp(vDbTimestamp.getTimestamp());
174             vTestResultDAO.saveOrUpdate(vDbSubmission);
175         }
176     }
177
178     public List<ValidationDbTestResult> readResultFromDb(String blueprintName, String version, String lab,
179             List<String> layers, Boolean allLayers, Boolean optional, Boolean outcome)
180                     throws JsonParseException, JsonMappingException, IOException {
181         synchronized (LOCK) {
182             LabInfo actualLabInfo = null;
183             if (lab != null) {
184                 actualLabInfo = labService.getLab(lab);
185                 if (actualLabInfo == null) {
186                     return null;
187                 }
188             }
189             Blueprint blueprint = null;
190             if (blueprintName != null) {
191                 blueprint = blueprintService.getBlueprint(blueprintName);
192                 if (blueprint == null) {
193                     return null;
194                 }
195             }
196             BlueprintInstance blueprintInst = blueprintInstService.getBlueprintInstance(blueprint, version);
197             if (blueprintInst == null) {
198                 return null;
199             }
200             List<ValidationDbTestResult> vDbResults = vTestResultDAO.getValidationTestResults(blueprintInst,
201                     actualLabInfo, allLayers, optional, outcome);
202             if (vDbResults == null || vDbResults.size() < 1) {
203                 return null;
204             }
205             List<ValidationDbTestResult> actualResults = new ArrayList<ValidationDbTestResult>();
206             for (ValidationDbTestResult vDbResult : vDbResults) {
207                 if (layers != null && layers.size() > 0) {
208                     List<String> storedLayers = new ArrayList<String>();
209                     List<WRobotDbTestResult> wDbResults = wRobotDAO.getWRobotTestResult(vDbResult);
210                     if (wDbResults == null || wDbResults.size() < 1) {
211                         continue;
212                     }
213                     for (WRobotDbTestResult wRobot : wDbResults) {
214                         storedLayers.add(wRobot.getLayer());
215                     }
216                     if (!new HashSet<>(storedLayers).equals(new HashSet<>(layers))) {
217                         continue;
218                     }
219                 }
220                 actualResults.add(vDbResult);
221             }
222             return actualResults;
223         }
224     }
225
226     public ValidationDbTestResult readResultFromDb(@Nonnull String lab, @Nonnull String timestamp)
227             throws JsonParseException, JsonMappingException, IOException {
228         synchronized (LOCK) {
229             LabInfo actualLabInfo = labService.getLab(lab);
230             ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(actualLabInfo, timestamp);
231             if (vDbResult == null) {
232                 return null;
233             }
234             return vDbResult;
235         }
236     }
237
238     public ValidationDbTestResult readResultFromDb(@Nonnull String submissionId)
239             throws JsonParseException, JsonMappingException, IOException {
240         synchronized (LOCK) {
241             return vTestResultDAO.getValidationTestResult(subService.getSubmission(submissionId));
242         }
243     }
244
245     public void deleteUnreferencedEntries(List<ValidationDbTestResult> vNexusResults) {
246         synchronized (LOCK) {
247             if (vNexusResults == null || vNexusResults.size() < 1) {
248                 return;
249             }
250             LabInfo labInfo = labService.getLabBasedOnSilo(vNexusResults.get(0).getLab().getSilo());
251             if (labInfo == null) {
252                 return;
253             }
254             Blueprint blueprint = blueprintService
255                     .getBlueprint(vNexusResults.get(0).getBlueprintInstance().getBlueprint().getBlueprintName());
256             if (blueprint == null) {
257                 return;
258             }
259             BlueprintInstance blueInst = blueprintInstService.getBlueprintInstance(blueprint,
260                     vNexusResults.get(0).getBlueprintInstance().getVersion());
261             if (blueInst == null) {
262                 return;
263             }
264             List<ValidationDbTestResult> vDbResults = vTestResultDAO.getValidationTestResults(blueInst, labInfo, null,
265                     null, null);
266             if (vDbResults == null || vDbResults.size() < 1) {
267                 return;
268             }
269             for (ValidationDbTestResult vDbResult : vDbResults) {
270                 if (vDbResult.getSubmission() != null) {
271                     continue;
272                 }
273                 boolean deletion = true;
274                 String dbTimestamp = vDbResult.getTimestamp();
275                 LabInfo dbLabInfo = vDbResult.getLab();
276                 for (ValidationDbTestResult vNexusResult : vNexusResults) {
277                     LabInfo nexusLabInfo = labService.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
278                     if (nexusLabInfo == null) {
279                         continue;
280                     }
281                     if (vNexusResult.getTimestamp().equals(dbTimestamp) && nexusLabInfo.equals(dbLabInfo)) {
282                         deletion = false;
283                         break;
284                     }
285                 }
286                 if (deletion) {
287                     LOGGER.debug(EELFLoggerDelegate.debugLogger,
288                             "Deleting unreferenced validation result with id: " + vDbResult.getResultId());
289                     // Delete old associated wrapper robot rest results from db
290                     for (WRobotDbTestResult wRobotDbResult : wRobotDAO.getWRobotTestResult(vDbResult)) {
291                         wRobotDAO.deleteWRobotTestResult(wRobotDbResult.getWRobotResultId());
292                     }
293                     vTestResultDAO.deleteValidationTestResult(vDbResult);
294                 }
295             }
296         }
297     }
298
299     public List<ValidationDbTestResult> getValidationTestResults() {
300         synchronized (LOCK) {
301             return vTestResultDAO.getValidationTestResults();
302         }
303     }
304
305     public ValidationDbTestResult getValidationTestResult(Integer resultId) {
306         synchronized (LOCK) {
307             return vTestResultDAO.getValidationTestResult(resultId);
308         }
309     }
310
311     public List<ValidationDbTestResult> getValidationTestResults(String blueprintName, @Nonnull String version,
312             LabInfo labInfo, Boolean allLayers, Boolean optional, Boolean outcome) {
313         synchronized (LOCK) {
314             Blueprint blueprint = null;
315             if (blueprintName != null) {
316                 blueprint = blueprintService.getBlueprint(blueprintName);
317                 if (blueprint == null) {
318                     return null;
319                 }
320             }
321             BlueprintInstance bluInst = blueprintInstService.getBlueprintInstance(blueprint, version);
322             if (bluInst == null) {
323                 return null;
324             }
325             return vTestResultDAO.getValidationTestResults(bluInst, labInfo, allLayers, optional, outcome);
326         }
327     }
328
329     public ValidationDbTestResult getValidationTestResult(LabInfo labInfo, String timestamp) {
330         synchronized (LOCK) {
331             return vTestResultDAO.getValidationTestResult(labInfo, timestamp);
332         }
333     }
334
335     public ValidationDbTestResult getValidationTestResult(String labSilo, String timestamp) {
336         synchronized (LOCK) {
337             return vTestResultDAO.getValidationTestResult(labService.getLabBasedOnSilo(labSilo), timestamp);
338         }
339     }
340
341     public ValidationDbTestResult getValidationTestResult(@Nonnull Submission submission) {
342         synchronized (LOCK) {
343             return vTestResultDAO.getValidationTestResult(submission);
344         }
345     }
346
347     public List<WRobotDbTestResult> getWRobotTestResults() {
348         synchronized (LOCK) {
349             return wRobotDAO.getWRobotTestResults();
350         }
351     }
352
353     public WRobotDbTestResult getWRobotTestResult(Integer wRobotResultId) {
354         synchronized (LOCK) {
355             return wRobotDAO.getWRobotTestResult(wRobotResultId);
356         }
357     }
358
359     public List<WRobotDbTestResult> getWRobotTestResult(ValidationDbTestResult vResult) {
360         synchronized (LOCK) {
361             return wRobotDAO.getWRobotTestResult(vResult);
362         }
363     }
364
365     public boolean checkValidityOfNexusResult(ValidationDbTestResult vNexusResult) {
366         if (vNexusResult == null) {
367             return true;
368         }
369         LabInfo labInfo = labService.getLabBasedOnSilo(vNexusResult.getLab().getSilo());
370         if (labInfo == null) {
371             throw new RuntimeException("Lab silo : " + vNexusResult.getLab().getSilo() + " not found");
372         }
373         ValidationDbTestResult vDbResult = vTestResultDAO.getValidationTestResult(
374                 labService.getLabBasedOnSilo(vNexusResult.getLab().getSilo()), vNexusResult.getTimestamp());
375         Blueprint blueprint = null;
376         BlueprintInstance bluInst = null;
377         List<WRobotDbTestResult> wRobotDbResults = null;
378         if (vDbResult != null) {
379             blueprint = vDbResult.getBlueprintInstance().getBlueprint();
380             labInfo = vDbResult.getLab();
381             wRobotDbResults = wRobotDAO.getWRobotTestResult(vDbResult);
382         } else {
383             blueprint = blueprintService
384                     .getBlueprint(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
385         }
386         if (blueprint != null) {
387             if (vDbResult != null) {
388                 bluInst = vDbResult.getBlueprintInstance();
389             } else {
390                 bluInst = blueprintInstService.getBlueprintInstance(blueprint,
391                         vNexusResult.getBlueprintInstance().getVersion());
392             }
393         }
394         // Start comparison, be elastic with allLayers and optional
395         if (!labInfo.getSilo().equals(vNexusResult.getLab().getSilo())) {
396             LOGGER.error(EELFLoggerDelegate.errorLogger,
397                     "Nexus has different data for blueprint : "
398                             + vDbResult.getBlueprintInstance().getBlueprint().getBlueprintName() + ", version: "
399                             + vDbResult.getBlueprintInstance().getVersion() + " and lab: " + vDbResult.getLab().getLab()
400                             + ". Lab inconsistency : " + vDbResult.getLab() + " " + labInfo);
401             return false;
402         }
403         if (blueprint != null) {
404             if (!blueprint.getBlueprintName()
405                     .equals(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName())) {
406                 LOGGER.error(EELFLoggerDelegate.errorLogger,
407                         "Nexus has different data for blueprint : " + blueprint.getBlueprintName()
408                         + ". Name inconsistency : " + blueprint.getBlueprintName() + " "
409                         + vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName());
410                 return false;
411             }
412         }
413         if (bluInst != null) {
414             if (!bluInst.getVersion().equals(vNexusResult.getBlueprintInstance().getVersion())) {
415                 LOGGER.error(EELFLoggerDelegate.errorLogger,
416                         "Nexus has different data for blueprint : " + bluInst.getBlueprint().getBlueprintName()
417                         + ", version: " + bluInst.getVersion() + ". Version inconsistency : "
418                         + bluInst.getVersion() + " " + vNexusResult.getBlueprintInstance().getVersion());
419                 return false;
420             }
421         }
422         if (wRobotDbResults != null) {
423             List<String> storedLayers1 = new ArrayList<String>();
424             for (WRobotDbTestResult wNexusResult : vNexusResult.getWRobotDbTestResults()) {
425                 storedLayers1.add(wNexusResult.getLayer());
426             }
427             List<String> storedLayers2 = new ArrayList<String>();
428             for (WRobotDbTestResult wDbResult : wRobotDbResults) {
429                 storedLayers2.add(wDbResult.getLayer());
430             }
431             if (!new HashSet<>(storedLayers1).equals(new HashSet<>(storedLayers2))) {
432                 LOGGER.error(EELFLoggerDelegate.errorLogger,
433                         "Nexus has different layer results for validation result id: " + vDbResult.getResultId());
434                 return false;
435             }
436         }
437         return true;
438     }
439
440     private boolean checkValidityOfJenkinsNotification(JnksJobNotify jnksJobNotify) {
441         ValidationDbTestResult vDbSubmission = vTestResultDAO
442                 .getValidationTestResult(subService.getSubmission(String.valueOf(jnksJobNotify.getSubmissionId())));
443         if (vDbSubmission == null) {
444             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received timestamp for submission id : "
445                     + jnksJobNotify.getSubmissionId() + " which has not validation result associated with it");
446             return false;
447         }
448         if (!vDbSubmission.getAllLayers() && (vDbSubmission.getWRobotDbTestResults() == null
449                 || vDbSubmission.getWRobotDbTestResults().size() < 1)) {
450             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received timestamp for submission id : "
451                     + jnksJobNotify.getSubmissionId() + " which is not stored correctly");
452             return false;
453         }
454         ValidationDbTestResult vDbTimestamp = vTestResultDAO.getValidationTestResult(vDbSubmission.getLab(),
455                 jnksJobNotify.getTimestamp());
456         if (vDbTimestamp == null) {
457             return true;
458         }
459         if (vDbTimestamp.equals(vDbSubmission) || (vDbTimestamp.getSubmission() != null
460                 && !jnksJobNotify.getSubmissionId().equals(vDbTimestamp.getSubmission().getSubmissionId()))) {
461             LOGGER.error(EELFLoggerDelegate.errorLogger, "Received same timestamp: " + jnksJobNotify.getTimestamp()
462             + " from nexus for submission id: " + jnksJobNotify.getSubmissionId());
463             return false;
464         }
465         if (!vDbSubmission.getAllLayers()) {
466             if (wRobotDAO.getWRobotTestResult(vDbSubmission).size() != wRobotDAO.getWRobotTestResult(vDbTimestamp)
467                     .size()) {
468                 LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in stored layers records.");
469                 return false;
470             }
471             List<String> storedLayers1 = new ArrayList<String>();
472             List<String> storedLayers2 = new ArrayList<String>();
473             List<WRobotDbTestResult> wDbResults = wRobotDAO.getWRobotTestResult(vDbSubmission);
474             for (WRobotDbTestResult wRobot : wDbResults) {
475                 storedLayers1.add(wRobot.getLayer());
476             }
477             wDbResults = wRobotDAO.getWRobotTestResult(vDbTimestamp);
478             for (WRobotDbTestResult wRobot : wDbResults) {
479                 storedLayers2.add(wRobot.getLayer());
480             }
481             if (!new HashSet<>(storedLayers1).equals(new HashSet<>(storedLayers2))) {
482                 LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in stored layers records.");
483                 return false;
484             }
485         }
486         // Be elastic with allLayers and optional
487         if (!vDbSubmission.getBlueprintInstance().getBlueprint().getBlueprintName()
488                 .equals(vDbTimestamp.getBlueprintInstance().getBlueprint().getBlueprintName())
489                 || !vDbSubmission.getBlueprintInstance().getVersion()
490                 .equals(vDbTimestamp.getBlueprintInstance().getVersion())
491                 || !vDbSubmission.getLab().equals(vDbTimestamp.getLab())) {
492             LOGGER.error(EELFLoggerDelegate.errorLogger, "No consistency exists in database records.");
493             return false;
494         }
495         return true;
496     }
497
498     private void updateBlueInstLayers(ValidationDbTestResult vNexusResult) {
499         for (BlueprintInstance blueprintInst : blueprintInstService.getBlueprintInstances()) {
500             if (!blueprintInst.getBlueprint().getBlueprintName()
501                     .equals(vNexusResult.getBlueprintInstance().getBlueprint().getBlueprintName())) {
502                 continue;
503             }
504             Set<BlueprintLayer> blueprintLayers = blueprintInst.getBlueprintLayers();
505             if (blueprintLayers == null) {
506                 blueprintLayers = new HashSet<BlueprintLayer>();
507             }
508             for (WRobotDbTestResult nexusResult : vNexusResult.getWRobotDbTestResults()) {
509                 BlueprintLayer layer = layerService.getBlueprintLayer(nexusResult.getLayer());
510                 if (layer == null) {
511                     layer = new BlueprintLayer();
512                     layer.setLayer(nexusResult.getLayer());
513                     layerService.saveBlueprintLayer(layer);
514                 }
515                 if (!blueprintLayers.contains(layer)) {
516                     blueprintLayers.add(layer);
517                 }
518             }
519             blueprintInst.setBlueprintLayers(blueprintLayers);
520             blueprintInstService.saveBlueprintInstance(blueprintInst);
521         }
522     }
523
524     private boolean compareBluInstances(BlueprintInstance inst1, BlueprintInstance inst2) {
525         if (!inst1.getVersion().equals(inst2.getVersion())) {
526             return false;
527         }
528         if (inst1.getBlueprintInstanceId() != inst2.getBlueprintInstanceId()) {
529             return false;
530         }
531         Set<BlueprintLayer> layers1 = inst1.getBlueprintLayers();
532         Set<BlueprintLayer> layers2 = inst2.getBlueprintLayers();
533         if (!(layers1 == null && layers2 == null)) {
534             if (layers1 != null && layers2 == null) {
535                 return false;
536             }
537             if (layers1 == null && layers2 != null) {
538                 return false;
539             }
540             if (!(layers1.size() == layers2.size())) {
541                 return false;
542             }
543             boolean overallLayerEquality = true;
544             for (BlueprintLayer blulayer1 : layers1) {
545                 boolean layerEquality = false;
546                 for (BlueprintLayer blulayer2 : layers2) {
547                     if (blulayer1.getLayer().equals(blulayer2.getLayer())) {
548                         layerEquality = true;
549                     }
550                 }
551                 if (!layerEquality) {
552                     overallLayerEquality = false;
553                     break;
554                 }
555             }
556             if (!overallLayerEquality) {
557                 return false;
558             }
559         }
560         Blueprint blueprint1 = inst1.getBlueprint();
561         Blueprint blueprint2 = inst2.getBlueprint();
562         if (blueprint1.getBlueprintId() != blueprint2.getBlueprintId()) {
563             return false;
564         }
565         if (!blueprint1.getBlueprintName().equals(blueprint2.getBlueprintName())) {
566             return false;
567         }
568         return true;
569     }
570
571 }