From eae2ba3f5ccfb20a899262562bd91129b6d53423 Mon Sep 17 00:00:00 2001 From: wejs Date: Wed, 7 Feb 2018 17:19:59 +0100 Subject: Sonar fixes in ArtifactValidatorExecuter Blocker sonar issues fix and CheckStyle formating corrections. Change-Id: I0882722f66bb0bb80b6eb85fe11657d10fe86d37 Issue-ID: APPC-525 Signed-off-by: wejs --- .../executers/ArtifactValidatorExecuter.java | 212 ++++++++++----------- 1 file changed, 102 insertions(+), 110 deletions(-) (limited to 'asdctool') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java index 4b9764d2d5..7f85d9d972 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java @@ -1,5 +1,6 @@ package org.openecomp.sdc.asdctool.impl.validator.executers; +import fj.data.Either; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; @@ -30,116 +31,107 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import fj.data.Either; +public class ArtifactValidatorExecuter { + + @Autowired + protected TitanDao titanDao; + + @Autowired + private ToscaOperationFacade toscaOperationFacade; + private static Logger log = LoggerFactory.getLogger(ArtifactValidatorExecuter.class.getName()); + + protected String name; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Map> getVerticesToValidate(VertexTypeEnum type, + Map hasProps) { + + Map> result = new HashMap<>(); + Either, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps); + if (resultsEither.isRight()) { + System.out.println("getVerticesToValidate failed " + resultsEither.right().value()); + return result; + } + System.out.println("getVerticesToValidate: " + resultsEither.left().value().size() + " vertices to scan"); + List componentsList = resultsEither.left().value(); + componentsList.forEach(vertex -> { + String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); + if (!result.containsKey(ivariantUuid)) { + result.put(ivariantUuid, new ArrayList<>()); + } + List compList = result.get(ivariantUuid); + + ComponentParametersView filter = new ComponentParametersView(true); + filter.setIgnoreArtifacts(false); + + Either toscaElement + = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter); + if (toscaElement.isRight()) { + System.out.println("getVerticesToValidate: failed to find element" + + vertex.getUniqueId() + " staus is" + toscaElement.right().value()); + } else { + compList.add(toscaElement.left().value()); + } + }); + return result; + } + + public boolean validate(Map> vertices) { + boolean result = true; + long time = System.currentTimeMillis(); + String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_" + time + ".csv"; + + try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { + writer.write("name, UUID, invariantUUID, state, version\n"); + Collection> collection = vertices.values(); + for (List compList: collection) { + Set artifactEsId = new HashSet<>(); + for (Component component: compList) { + Map toscaArtifacts = component.getToscaArtifacts(); + Optional op = toscaArtifacts.values() + .stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny(); + if (op.isPresent()) { + result = false; + writeModuleResultToFile(writer, compList); + writer.flush(); + break; + } else { + artifactEsId.addAll(toscaArtifacts.values() + .stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList())); + } + } + } + } catch (Exception e) { + log.info("Failed to fetch vf resources ", e); + return false; + } finally { + titanDao.commit(); + } + return result; + } -public class ArtifactValidatorExecuter{ - - @Autowired - protected TitanDao titanDao; - - @Autowired - private ToscaOperationFacade toscaOperationFacade; - private static Logger log = LoggerFactory.getLogger(ArtifactValidatorExecuter.class.getName()); - - protected String name; - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - - - public Map> getVerticesToValidate(VertexTypeEnum type, Map hasProps){ - Map> result = new HashMap<>(); - Either, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps); - if (resultsEither.isRight()) { - System.out.println("getVerticesToValidate failed "+ resultsEither.right().value()); - return result; - } - System.out.println("getVerticesToValidate: "+resultsEither.left().value().size()+" vertices to scan"); - List componentsList = resultsEither.left().value(); - componentsList.forEach(vertex -> { - String ivariantUuid = (String)vertex.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID); - if(!result.containsKey(ivariantUuid)){ - List compList = new ArrayList(); - result.put(ivariantUuid, compList); - } - List compList = result.get(ivariantUuid); - - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreArtifacts(false); - - Either toscaElement = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter); - if (toscaElement.isRight()) { - System.out.println("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value()); - }else{ - compList.add(toscaElement.left().value()); - } - - }); - - return result; - } - - public boolean validate( Map> vertices) { - boolean result = true; - long time = System.currentTimeMillis(); - String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_"+ time + ".csv"; - Writer writer = null; - try { - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); - writer.write("name, UUID, invariantUUID, state, version\n"); - Collection> collection = vertices.values(); - for(List compList: collection ){ - Set artifactEsId = new HashSet<>(); - for(Component component: compList ){ - Map toscaArtifacts = component.getToscaArtifacts(); - Optional op = toscaArtifacts.values(). - stream().filter(a -> artifactEsId.contains(a.getEsId())).findAny(); - if(op.isPresent()){ - result = false; - writeModuleResultToFile(writer, compList); - writer.flush(); - break; - }else{ - artifactEsId.addAll(toscaArtifacts.values().stream().map(ArtifactDefinition::getEsId).collect(Collectors.toList())) ; - } - } - - } - - } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); - return false; - } finally { - titanDao.commit(); - try { - writer.flush(); - writer.close(); - } catch (Exception ex) { - /* ignore */} - } - return result; - } - - private void writeModuleResultToFile(Writer writer, List components) { - try { - // "service name, service id, state, version - for(Component component: components ){ - StringBuffer sb = new StringBuffer(component.getName()); - sb.append(",").append(component.getUniqueId()).append(",").append(component.getInvariantUUID()).append(",").append(component.getLifecycleState()).append(",").append(component.getVersion()); - - sb.append("\n"); - writer.write(sb.toString()); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } + private void writeModuleResultToFile(Writer writer, List components) { + try { + // "service name, service id, state, version + for (Component component: components ) { + StringBuilder sb = new StringBuilder(component.getName()); + sb.append(",").append(component.getUniqueId()) + .append(",").append(component.getInvariantUUID()) + .append(",").append(component.getLifecycleState()) + .append(",").append(component.getVersion()) + .append("\n"); + writer.write(sb.toString()); + } + } catch (IOException e) { + log.debug("Cannot write module result to file", e); + } + } } -- cgit 1.2.3-korg