47f00dc421f029b779d4708f28e2644c1141cc09
[sdc.git] /
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.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46 import java.util.stream.Collectors;
47
48 /**
49  * Created by chaya on 7/18/2017.
50  */
51 public class ModuleJsonTask extends ServiceValidationTask {
52
53     private TopologyTemplateOperation topologyTemplateOperation;
54
55     @Autowired
56     public ModuleJsonTask(TopologyTemplateOperation topologyTemplateOperation) {
57         this.topologyTemplateOperation = topologyTemplateOperation;
58         this.name = "Service Module json Validation Task";
59     }
60
61     @Override
62     public VertexResult validate(GraphVertex vertex, String outputFilePath) {
63         if (!isAfterSubmitForTesting(vertex)) {
64             return new VertexResult(true);
65         }
66
67         ComponentParametersView paramView = new ComponentParametersView();
68         paramView.disableAll();
69         paramView.setIgnoreArtifacts(false);
70         paramView.setIgnoreGroups(false);
71         paramView.setIgnoreComponentInstances(false);
72         Either<ToscaElement, StorageOperationStatus> toscaElementEither = topologyTemplateOperation
73             .getToscaElement(vertex.getUniqueId(), paramView);
74         if (toscaElementEither.isRight()) {
75             return new VertexResult(false);
76         }
77         TopologyTemplate element = (TopologyTemplate) toscaElementEither.left().value();
78         Map<String, MapGroupsDataDefinition> instGroups = element.getInstGroups();
79         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts = element.getInstDeploymentArtifacts();
80
81         for (Map.Entry<String, MapGroupsDataDefinition> pair : Optional.ofNullable(instGroups)
82             .orElse(Collections.emptyMap()).entrySet()) {
83             MapGroupsDataDefinition groups = pair.getValue();
84             if (groups != null && !groups.getMapToscaDataDefinition().isEmpty()) {
85                 return new VertexResult(findCoordinateModuleJson(pair, instDeploymentArtifacts, vertex, outputFilePath));
86             }
87             return new VertexResult(true);
88         }
89         return new VertexResult(true);
90     }
91
92     private boolean findCoordinateModuleJson(Map.Entry<String, MapGroupsDataDefinition> pair,
93         Map<String, MapArtifactDataDefinition> instDeploymentArtifacts, GraphVertex vertex, String outputFilePath) {
94         String groupKey = pair.getKey();
95         String[] split = groupKey.split("\\.");
96         String instanceName = split[split.length - 1];
97         MapArtifactDataDefinition deploymentsArtifacts = instDeploymentArtifacts.get(groupKey);
98         if (deploymentsArtifacts != null && !deploymentsArtifacts.getMapToscaDataDefinition().isEmpty()) {
99             List<ArtifactDataDefinition> moduleJsonArtifacts = deploymentsArtifacts.getMapToscaDataDefinition().values()
100                 .stream().filter(artifact -> {
101                     String artifactName = artifact.getArtifactName();
102                     if (artifactName.startsWith(instanceName) && artifactName.endsWith("modules.json")) {
103                         return true;
104                     }
105                     return false;
106                 }).collect(Collectors.toList());
107             if (moduleJsonArtifacts.size() > 0) {
108                 String status =
109                     "Instance " + instanceName + " has a corresponding modules.json file: " + moduleJsonArtifacts.get(0)
110                         .getArtifactName();
111                 ReportManager.writeReportLineToFile(status, outputFilePath);
112                 return true;
113             }
114         }
115         String status = "Instance " + instanceName + " doesn't have a corresponding modules.json file";
116         ReportManager.writeReportLineToFile(status, outputFilePath);
117         ReportManager.addFailedVertex(getTaskName(), vertex.getUniqueId());
118         return false;
119     }
120
121     private boolean isAfterSubmitForTesting(GraphVertex vertex) {
122         List allowedStates = new ArrayList<>(Arrays.asList(LifecycleStateEnum.CERTIFIED.name()));
123         return allowedStates.contains(vertex.getMetadataProperty(GraphPropertyEnum.STATE));
124     }
125 }