Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / validator / tasks / moduleJson / ModuleJsonTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.impl.validator.tasks.moduleJson;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.asdctool.impl.validator.tasks.ServiceValidationTask;
25 import org.openecomp.sdc.asdctool.impl.validator.utils.ReportManager;
26 import org.openecomp.sdc.asdctool.impl.validator.utils.VertexResult;
27 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
28 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
32 import org.openecomp.sdc.be.model.ComponentParametersView;
33 import org.openecomp.sdc.be.model.LifecycleStateEnum;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.TopologyTemplate;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 import java.util.*;
41 import java.util.stream.Collectors;
42
43 /**
44  * Created by chaya on 7/18/2017.
45  */
46 public class ModuleJsonTask extends ServiceValidationTask {
47
48     @Autowired
49     private TopologyTemplateOperation topologyTemplateOperation;
50
51     public ModuleJsonTask() {
52         this.name = "Service Module json Validation Task";
53     }
54
55     @Override
56     public VertexResult validate(GraphVertex vertex) {
57         if (!isAfterSubmitForTesting(vertex)) {
58             return new VertexResult(true);
59         }
60
61         ComponentParametersView paramView = new ComponentParametersView();
62         paramView.disableAll();
63         paramView.setIgnoreArtifacts(false);
64         paramView.setIgnoreGroups(false);
65         paramView.setIgnoreComponentInstances(false);
66         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation.getToscaElement(vertex.getUniqueId(), paramView);
67         if (toscaElementEither.isRight()) {
68             return new VertexResult(false);
69         }
70         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
71         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
72         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
73
74         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups).orElse(Collections.emptyMap()).entrySet()) {
75             MapGroupsDataDefinition groups = pair.getValue();
76             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
77                 return new VertexResult(findCoordinateModuleJson(pair, instDeploymentArtifacts, vertex));
78             }
79             return new VertexResult(true);
80         }
81         return new VertexResult(true);
82     }
83
84     private boolean findCoordinateModuleJson(Map.Entry<String, MapGroupsDataDefinition> pair, Map<String, MapArtifactDataDefinition> instDeploymentArtifacts, GraphVertex vertex) {
85         String groupKey = pair.getKey();
86         String[] split = groupKey.split("\\.");
87         String instanceName = split[split.length-1];
88         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
89         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
90             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values().stream().filter(artifact -> {
91                 String artifactName = artifact.getArtifactName();
92                 if (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json")) {
93                     return true;
94                 }
95                 return false;
96             }).collect(Collectors.toList());
97             if (moduleJsonArtifacts.size() > 0) {
98                 String status = "Instance "+instanceName+" has a corresponding modules.json file: "+moduleJsonArtifacts.get(0).getArtifactName();
99                 ReportManager.writeReportLineToFile(status);
100                 return true;
101             }
102         }
103         String status = "Instance "+instanceName+" doesn't have a corresponding modules.json file";
104         ReportManager.writeReportLineToFile(status);
105         ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
106         return false;
107     }
108
109     private boolean isAfterSubmitForTesting(GraphVertex vertex){
110         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.READY_FOR_CERTIFICATION.name(),
111                 LifecycleStateEnum.CERTIFICATION_IN_PROGRESS.name(), LifecycleStateEnum.CERTIFIED.name()));
112         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
113     }
114 }