2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services;
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;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
38 * The type Composition entity data manager.
40 public class CompositionEntityDataManager {
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";
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<>();
54 * Validate entity composition entity validation data.
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
61 public static CompositionEntityValidationData validateEntity(
62 org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity,
63 SchemaTemplateContext schemaTemplateContext,
64 SchemaTemplateInput schemaTemplateInput) {
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"))
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"))
80 CompositionEntityValidationData validationData =
81 new CompositionEntityValidationData(entity.getType(), entity.getId());
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)));
89 return validationData;
95 * @param entity the entity
96 * @param schemaTemplateInput the schema template input
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"))
107 entities.put(entity.getCompositionEntityId(),
108 new CompositionEntityData(entity, schemaTemplateInput));
112 * Validate entities questionnaire map.
116 public Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
117 Collection<String>> validateEntitiesQuestionnaire() {
118 Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
120 errorsByEntityId = new HashMap<>();
122 entities.entrySet().stream().forEach(entry -> {
123 Collection<String> errors = validateQuestionnaire(entry.getValue());
124 if (errors != null) {
125 errorsByEntityId.put(entry.getKey(), errors);
129 return errorsByEntityId;
135 public void buildTrees() {
136 Map<org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId,
137 CompositionEntityValidationData>
138 entitiesValidationData =
140 entities.entrySet().stream().forEach(
141 entry -> addValidationDataEntity(entitiesValidationData, entry.getKey(),
142 entry.getValue().entity));
150 public Collection<CompositionEntityValidationData> getTrees() {
155 * Add errors to trees.
157 * @param errors the errors
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));
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)) {
174 CompositionEntityValidationData validationData =
175 new CompositionEntityValidationData(entity.getType(), entity.getId());
176 entitiesValidationData.put(entityId, validationData);
178 org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId parentEntityId =
179 entityId.getParentId();
180 if (parentEntityId == null) {
181 roots.add(validationData);
183 CompositionEntityData parentEntity = entities.get(parentEntityId);
184 if (parentEntity == null) {
185 roots.add(validationData);
187 addValidationDataEntity(entitiesValidationData, parentEntityId, parentEntity.entity);
188 entitiesValidationData.get(parentEntityId).addSubEntityValidationData(validationData);
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) {
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));
205 if (node.getSubEntitiesValidationData() != null) {
206 node.getSubEntitiesValidationData().stream()
207 .forEach(subNode -> addErrorsToTree(subNode, nodeId, errors));
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));
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);
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);
240 private static class CompositionEntityData {
241 private org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity;
242 private SchemaTemplateInput schemaTemplateInput;
245 * Instantiates a new Composition entity data.
247 * @param entity the entity
248 * @param schemaTemplateInput the schema template input
250 public CompositionEntityData(
251 org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity entity,
252 SchemaTemplateInput schemaTemplateInput) {
253 this.entity = entity;
254 this.schemaTemplateInput = schemaTemplateInput;