[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / CompositionEntityDataManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.vendorsoftwareproduct.services;
22
23 import org.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.errors.ErrorCategory;
26 import org.openecomp.sdc.common.errors.ErrorCode;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityValidationData;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
30
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 /**
38  * The type Composition entity data manager.
39  */
40 public class CompositionEntityDataManager {
41
42   private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR =
43       "COMPOSITION_ENTITY_DATA_MANAGER_ERR";
44   private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG =
45       "Invalid input: %s may not be null";
46
47   private Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
48       CompositionEntityData> entities = new HashMap<>();
49   private Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType,
50       String> nonDynamicSchemas = new HashMap<>();
51   private List<CompositionEntityValidationData> roots = new ArrayList<>();
52
53   /**
54    * Validate entity composition entity validation data.
55    *
56    * @param entity                the entity
57    * @param schemaTemplateContext the schema template context
58    * @param schemaTemplateInput   the schema template input
59    * @return the composition entity validation data
60    */
61   public static CompositionEntityValidationData validateEntity(
62       org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity,
63       SchemaTemplateContext schemaTemplateContext,
64       SchemaTemplateInput schemaTemplateInput) {
65     if (entity == null) {
66       throw new CoreException(
67           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
68               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
69               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
70               .build());
71     }
72     if (schemaTemplateContext == null) {
73       throw new CoreException(
74           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
75               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
76               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "schema template context"))
77               .build());
78     }
79
80     CompositionEntityValidationData validationData =
81         new CompositionEntityValidationData(entity.getType(), entity.getId());
82     String json =
83         schemaTemplateContext == SchemaTemplateContext.composition ? entity.getCompositionData()
84             : entity.getQuestionnaireData();
85     validationData.setErrors(JsonUtil.validate(
86         json == null ? JsonUtil.object2Json(new Object()) : json,
87         SchemaGenerator.generate(schemaTemplateContext, entity.getType(), schemaTemplateInput)));
88
89     return validationData;
90   }
91
92   /**
93    * Add entity.
94    *
95    * @param entity              the entity
96    * @param schemaTemplateInput the schema template input
97    */
98   public void addEntity(org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity,
99                         SchemaTemplateInput schemaTemplateInput) {
100     if (entity == null) {
101       throw new CoreException(
102           new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
103               .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
104               String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
105               .build());
106     }
107     entities.put(entity.getCompositionEntityId(),
108         new CompositionEntityData(entity, schemaTemplateInput));
109   }
110
111   /**
112    * Validate entities questionnaire map.
113    *
114    * @return the map
115    */
116   public Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
117       Collection<String>> validateEntitiesQuestionnaire() {
118     Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
119         Collection<String>>
120         errorsByEntityId = new HashMap<>();
121
122     entities.entrySet().stream().forEach(entry -> {
123       Collection<String> errors = validateQuestionnaire(entry.getValue());
124       if (errors != null) {
125         errorsByEntityId.put(entry.getKey(), errors);
126       }
127     });
128
129     return errorsByEntityId;
130   }
131
132   /**
133    * Build trees.
134    */
135   public void buildTrees() {
136     Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
137         CompositionEntityValidationData>
138         entitiesValidationData =
139         new HashMap<>();
140     entities.entrySet().stream().forEach(
141         entry -> addValidationDataEntity(entitiesValidationData, entry.getKey(),
142             entry.getValue().entity));
143   }
144
145   /**
146    * Gets trees.
147    *
148    * @return the trees
149    */
150   public Collection<CompositionEntityValidationData> getTrees() {
151     return roots;
152   }
153
154   /**
155    * Add errors to trees.
156    *
157    * @param errors the errors
158    */
159   public void addErrorsToTrees(
160       Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
161           Collection<String>> errors) {
162     roots.stream().forEach(root -> addErrorsToTree(root, null, errors));
163   }
164
165   private void addValidationDataEntity(
166       Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
167           CompositionEntityValidationData> entitiesValidationData,
168       org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId entityId,
169       org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity) {
170     if (entitiesValidationData.containsKey(entityId)) {
171       return;
172     }
173
174     CompositionEntityValidationData validationData =
175         new CompositionEntityValidationData(entity.getType(), entity.getId());
176     entitiesValidationData.put(entityId, validationData);
177
178     org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId parentEntityId =
179         entityId.getParentId();
180     if (parentEntityId == null) {
181       roots.add(validationData);
182     } else {
183       CompositionEntityData parentEntity = entities.get(parentEntityId);
184       if (parentEntity == null) {
185         roots.add(validationData);
186       } else {
187         addValidationDataEntity(entitiesValidationData, parentEntityId, parentEntity.entity);
188         entitiesValidationData.get(parentEntityId).addSubEntityValidationData(validationData);
189       }
190     }
191   }
192
193   private void addErrorsToTree(CompositionEntityValidationData node,
194        org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId parentNodeId,
195        Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
196            Collection<String>> errors) {
197     if (node == null) {
198       return;
199     }
200     org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId
201         nodeId = new org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId(
202         node.getEntityId(), parentNodeId);
203     node.setErrors(errors.get(nodeId));
204
205     if (node.getSubEntitiesValidationData() != null) {
206       node.getSubEntitiesValidationData().stream()
207           .forEach(subNode -> addErrorsToTree(subNode, nodeId, errors));
208     }
209   }
210
211   private Collection<String> validateQuestionnaire(CompositionEntityData compositionEntityData) {
212     return JsonUtil.validate(
213         compositionEntityData.entity.getQuestionnaireData() == null ? JsonUtil
214             .object2Json(new Object()) : compositionEntityData.entity.getQuestionnaireData(),
215         getSchema(compositionEntityData.entity.getType(), SchemaTemplateContext.questionnaire,
216             compositionEntityData.schemaTemplateInput));
217   }
218
219   private String getSchema(
220       org.openecomp.sdc.vendorsoftwareproduct.types
221           .composition.CompositionEntityType compositionEntityType,
222       SchemaTemplateContext schemaTemplateContext,
223       SchemaTemplateInput schemaTemplateInput) {
224     return schemaTemplateInput == null ? getNonDynamicSchema(schemaTemplateContext,
225         compositionEntityType) : SchemaGenerator
226         .generate(schemaTemplateContext, compositionEntityType, schemaTemplateInput);
227   }
228
229   private String getNonDynamicSchema(SchemaTemplateContext schemaTemplateContext,
230       org.openecomp.sdc.vendorsoftwareproduct.types.composition
231         .CompositionEntityType compositionEntityType) {
232     String schema = nonDynamicSchemas.get(compositionEntityType);
233     if (schema == null) {
234       schema = SchemaGenerator.generate(schemaTemplateContext, compositionEntityType, null);
235       nonDynamicSchemas.put(compositionEntityType, schema);
236     }
237     return schema;
238   }
239
240   private static class CompositionEntityData {
241     private org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity;
242     private SchemaTemplateInput schemaTemplateInput;
243
244     /**
245      * Instantiates a new Composition entity data.
246      *
247      * @param entity              the entity
248      * @param schemaTemplateInput the schema template input
249      */
250     public CompositionEntityData(
251         org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity,
252         SchemaTemplateInput schemaTemplateInput) {
253       this.entity = entity;
254       this.schemaTemplateInput = schemaTemplateInput;
255     }
256   }
257 }