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.impl;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.utilities.CommonMethods;
26 import org.openecomp.core.utilities.json.JsonSchemaDataGenerator;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.CompositionEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspQuestionnaireEntity;
50 import org.openecomp.sdc.vendorsoftwareproduct.services.schemagenerator.SchemaGenerator;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityId;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComputeData;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.NetworkType;
61 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
62 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateContext;
63 import org.openecomp.sdc.vendorsoftwareproduct.types.schemagenerator.SchemaTemplateInput;
64 import org.openecomp.sdc.versioning.dao.types.Version;
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.HashMap;
70 import java.util.HashSet;
71 import java.util.List;
73 import java.util.Objects;
76 public class CompositionEntityDataManagerImpl implements CompositionEntityDataManager {
78 private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR =
79 "COMPOSITION_ENTITY_DATA_MANAGER_ERR";
80 private static final String COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG =
81 "Invalid input: %s may not be null";
82 private static final String MISSING_OR_INVALID_QUESTIONNAIRE_MSG =
83 "Data is missing/invalid for this %s. Please refill and resubmit.";
85 private static final Logger logger =
86 LoggerFactory.getLogger(CompositionEntityDataManagerImpl.class);
87 private Map<CompositionEntityId, CompositionEntityData> entities = new HashMap<>();
88 private Map<CompositionEntityType, String> nonDynamicSchemas = new HashMap<>();
89 private List<CompositionEntityValidationData> roots = new ArrayList<>();
91 private VendorSoftwareProductInfoDao vspInfoDao;
92 private ComponentDao componentDao;
93 private NicDao nicDao;
94 private NetworkDao networkDao;
95 private ImageDao imageDao;
96 private ComputeDao computeDao;
97 private DeploymentFlavorDao deploymentFlavorDao;
99 public CompositionEntityDataManagerImpl(VendorSoftwareProductInfoDao vspInfoDao,
100 ComponentDao componentDao,
101 NicDao nicDao, NetworkDao networkDao,
102 ImageDao imageDao, ComputeDao computeDao,
103 DeploymentFlavorDao deploymentFlavorDao) {
104 this.vspInfoDao = vspInfoDao;
105 this.componentDao = componentDao;
106 this.nicDao = nicDao;
107 this.networkDao = networkDao;
108 this.imageDao = imageDao;
109 this.computeDao = computeDao;
110 this.deploymentFlavorDao = deploymentFlavorDao;
114 * Validate entity composition entity validation data.
116 * @param entity the entity
117 * @param schemaTemplateContext the schema template context
118 * @param schemaTemplateInput the schema template input
119 * @return the composition entity validation data
122 public CompositionEntityValidationData validateEntity(CompositionEntity entity,
123 SchemaTemplateContext schemaTemplateContext,
124 SchemaTemplateInput schemaTemplateInput) {
125 if (entity == null) {
126 throw new CoreException(
127 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
128 .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
129 String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
132 if (schemaTemplateContext == null) {
133 throw new CoreException(
134 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
135 .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
136 String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "schema template context"))
140 CompositionEntityValidationData validationData =
141 new CompositionEntityValidationData(entity.getType(), entity.getId());
143 schemaTemplateContext == SchemaTemplateContext.composition ? entity.getCompositionData()
144 : entity.getQuestionnaireData();
145 validationData.setErrors(JsonUtil.validate(
146 json == null ? JsonUtil.object2Json(new Object()) : json,
147 generateSchema(schemaTemplateContext, entity.getType(), schemaTemplateInput)));
148 return validationData;
154 * @param entity the entity
155 * @param schemaTemplateInput the schema template input
158 public void addEntity(CompositionEntity entity, SchemaTemplateInput schemaTemplateInput) {
159 if (entity == null) {
160 throw new CoreException(
161 new ErrorCode.ErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION)
162 .withId(COMPOSITION_ENTITY_DATA_MANAGER_ERR).withMessage(
163 String.format(COMPOSITION_ENTITY_DATA_MANAGER_ERR_MSG, "composition entity"))
166 entities.put(entity.getCompositionEntityId(),
167 new CompositionEntityData(entity, schemaTemplateInput));
171 * Validate entities questionnaire map.
176 public Map<CompositionEntityId, Collection<String>> validateEntitiesQuestionnaire() {
177 Map<CompositionEntityId, Collection<String>> errorsByEntityId = new HashMap<>();
178 entities.entrySet().forEach(entry -> {
179 Collection<String> errors = validateQuestionnaire(entry.getValue());
180 if (errors != null) {
181 errorsByEntityId.put(entry.getKey(), errors);
184 return errorsByEntityId;
191 public void buildTrees() {
192 Map<CompositionEntityId, CompositionEntityValidationData> entitiesValidationData =
194 entities.entrySet().forEach(
195 entry -> addValidationDataEntity(entitiesValidationData, entry.getKey(),
196 entry.getValue().entity));
199 public Collection<CompositionEntityValidationData> getTrees() {
204 public void saveCompositionData(String vspId, Version version, CompositionData compositionData) {
205 if (Objects.isNull(compositionData)) {
209 Map<String, String> networkIdByName = saveNetworks(vspId, version, compositionData);
210 saveComponents(vspId, version, compositionData, networkIdByName);
214 public Set<CompositionEntityValidationData> getAllErrorsByVsp(String vspId) {
215 CompositionEntityValidationData matchVsp = null;
216 Set<CompositionEntityValidationData> entitiesWithErrors = new HashSet<>();
217 for (CompositionEntityValidationData root : roots) {
218 if (root.getEntityId().equals(vspId)) {
224 getEntityListWithErrors(matchVsp, entitiesWithErrors);
225 if (CollectionUtils.isNotEmpty(entitiesWithErrors)) {
226 updateValidationCompositionEntityName(entitiesWithErrors);
227 return entitiesWithErrors;
233 private boolean isThereErrorsInSubTree(CompositionEntityValidationData entity) {
234 if (Objects.isNull(entity)) {
238 if (CollectionUtils.isNotEmpty(entity.getErrors())) {
242 Collection<CompositionEntityValidationData> subEntitiesValidationData =
243 entity.getSubEntitiesValidationData();
244 return !CollectionUtils.isEmpty(subEntitiesValidationData) &&
245 checkForErrorsInChildren(subEntitiesValidationData);
249 private boolean checkForErrorsInChildren(
250 Collection<CompositionEntityValidationData> subEntitiesValidationData) {
251 boolean result = false;
252 for (CompositionEntityValidationData subEntity : subEntitiesValidationData) {
253 if (CollectionUtils.isNotEmpty(subEntity.getErrors())) {
257 result = isThereErrorsInSubTree(subEntity) || result;
265 private void saveComponents(String vspId, Version version, CompositionData compositionData,
266 Map<String, String> networkIdByName) {
267 if (CollectionUtils.isNotEmpty(compositionData.getComponents())) {
268 for (Component component : compositionData.getComponents()) {
269 ComponentEntity componentEntity = new ComponentEntity(vspId, version, null);
270 componentEntity.setComponentCompositionData(component.getData());
272 String componentId = createComponent(componentEntity).getId();
274 saveImagesByComponent(vspId, version, component, componentId);
275 saveComputesFlavorByComponent(vspId, version, component, componentId);
277 saveNicsByComponent(vspId, version, networkIdByName, component, componentId);
282 private void saveNicsByComponent(String vspId, Version version,
283 Map<String, String> networkIdByName, Component component,
284 String componentId) {
285 if (CollectionUtils.isNotEmpty(component.getNics())) {
286 for (Nic nic : component.getNics()) {
287 if (nic.getNetworkName() != null && MapUtils.isNotEmpty(networkIdByName)) {
288 nic.setNetworkId(networkIdByName.get(nic.getNetworkName()));
290 nic.setNetworkName(null);
291 //For heat flow set network type to be internal by default for NIC
292 nic.setNetworkType(NetworkType.Internal);
294 NicEntity nicEntity = new NicEntity(vspId, version, componentId, null);
295 nicEntity.setNicCompositionData(nic);
296 createNic(nicEntity);
301 private Map<String, String> saveNetworks(String vspId, Version version,
302 CompositionData compositionData) {
303 Map<String, String> networkIdByName = new HashMap<>();
304 if (CollectionUtils.isNotEmpty(compositionData.getNetworks())) {
305 for (Network network : compositionData.getNetworks()) {
307 NetworkEntity networkEntity = new NetworkEntity(vspId, version, null);
308 networkEntity.setNetworkCompositionData(network);
310 if (network.getName() != null) {
311 networkIdByName.put(network.getName(), createNetwork(networkEntity).getId());
315 return networkIdByName;
318 private NetworkEntity createNetwork(NetworkEntity network) {
319 //network.setId(CommonMethods.nextUuId()); will be set by the dao
320 networkDao.create(network);
325 public ComponentEntity createComponent(ComponentEntity component) {
326 //component.setId(CommonMethods.nextUuId()); will be set by the dao
327 component.setQuestionnaireData(
328 new JsonSchemaDataGenerator(
329 generateSchema(SchemaTemplateContext.questionnaire, CompositionEntityType.component,
333 componentDao.create(component);
338 public NicEntity createNic(NicEntity nic) {
339 //nic.setId(CommonMethods.nextUuId()); will be set by the dao
340 nic.setQuestionnaireData(
341 new JsonSchemaDataGenerator(
342 generateSchema(SchemaTemplateContext.questionnaire, CompositionEntityType.nic, null))
350 public void addErrorsToTrees(Map<CompositionEntityId, Collection<String>> errors) {
351 roots.forEach(root -> addErrorsToTree(root, null, errors));
355 * get a flat list of all questionnaire entities that have validation errors
357 public Set<CompositionEntityValidationData> getEntityListWithErrors() {
358 Set<CompositionEntityValidationData> treeAsList = new HashSet<>();
360 for (CompositionEntityValidationData entity : roots) {
361 if (CollectionUtils.isNotEmpty(entity.getErrors())) {
362 addNodeWithErrors(entity, treeAsList);
364 getEntityListWithErrors(entity, treeAsList);
367 updateValidationCompositionEntityName(treeAsList);
371 private void getEntityListWithErrors(CompositionEntityValidationData entity,
372 Set<CompositionEntityValidationData> compositionSet) {
373 if (CollectionUtils.isNotEmpty(entity.getErrors())) {
374 addNodeWithErrors(entity, compositionSet);
377 if (CollectionUtils.isEmpty(entity.getSubEntitiesValidationData())) {
381 for (CompositionEntityValidationData child : entity.getSubEntitiesValidationData()) {
382 getEntityListWithErrors(child, compositionSet);
387 private void addNodeWithErrors(CompositionEntityValidationData node,
388 Set<CompositionEntityValidationData> entitiesWithErrors) {
389 CompositionEntityValidationData compositionNodeToAdd = new CompositionEntityValidationData(node
390 .getEntityType(), node.getEntityId());
391 compositionNodeToAdd.setErrors(node.getErrors());
392 compositionNodeToAdd.setSubEntitiesValidationData(null);
394 entitiesWithErrors.add(compositionNodeToAdd);
397 public void removeNodesWithoutErrors() {
398 roots.forEach(root -> removeNodesWithoutErrors(root, null));
402 private CompositionEntityData getCompositionEntityDataById(CompositionEntityValidationData
404 for (Map.Entry<CompositionEntityId, CompositionEntityData> entityEntry : entities
406 if (entityEntry.getKey().getId().equals(entity.getEntityId())) {
407 return entityEntry.getValue();
414 private void updateValidationCompositionEntityName(Set<CompositionEntityValidationData>
416 for (CompositionEntityValidationData entity : compositionSet) {
417 String compositionData = getCompositionDataAsString(entity);
418 if (entity.getEntityType().equals(CompositionEntityType.vsp) ||
419 Objects.nonNull(compositionData)) {
420 entity.setEntityName(getEntityNameByEntityType(compositionData, entity));
425 private String getCompositionDataAsString(CompositionEntityValidationData entity) {
426 CompositionEntityData compositionEntityData = getCompositionEntityDataById(entity);
427 return compositionEntityData == null ? null : compositionEntityData.entity.getCompositionData();
431 private String getEntityNameByEntityType(String compositionData,
432 CompositionEntityValidationData entity) {
433 switch (entity.getEntityType()) {
435 ComponentData component = JsonUtil.json2Object(compositionData, ComponentData.class);
436 return component.getDisplayName();
439 Nic nic = JsonUtil.json2Object(compositionData, Nic.class);
440 return nic.getName();
443 Network network = JsonUtil.json2Object(compositionData, Network.class);
444 return network.getName();
447 Image image = JsonUtil.json2Object(compositionData, Image.class);
448 return image.getFileName();
451 CompositionEntityData vspEntity = getCompositionEntityDataById(entity);
452 VspQuestionnaireEntity vspQuestionnaireEntity = (VspQuestionnaireEntity) vspEntity.entity;
453 VspDetails vspDetails =
454 vspInfoDao.get(new VspDetails(vspQuestionnaireEntity.getId(),
455 vspQuestionnaireEntity.getVersion()));
456 return vspDetails.getName();
462 private void removeNodesWithoutErrors(CompositionEntityValidationData node,
463 CompositionEntityValidationData parent) {
465 if (Objects.isNull(node)) {
469 if (hasChildren(node)) {
470 Collection<CompositionEntityValidationData> subNodes =
471 new ArrayList<>(node.getSubEntitiesValidationData());
472 subNodes.forEach(subNode -> removeNodesWithoutErrors(subNode, node));
473 node.setSubEntitiesValidationData(subNodes);
475 if (canNodeGetRemovedFromValidationDataTree(node)) {
476 removeNodeFromChildren(parent, node);
478 } else if (canNodeGetRemovedFromValidationDataTree(node)) {
479 removeNodeFromChildren(parent, node);
483 private void removeNodeFromChildren(CompositionEntityValidationData parent,
484 CompositionEntityValidationData childToRemove) {
485 if (!Objects.isNull(parent)) {
486 parent.getSubEntitiesValidationData().remove(childToRemove);
490 private boolean hasChildren(CompositionEntityValidationData node) {
491 return !CollectionUtils.isEmpty(node.getSubEntitiesValidationData());
494 private boolean canNodeGetRemovedFromValidationDataTree(CompositionEntityValidationData node) {
495 return !hasChildren(node) && CollectionUtils.isEmpty(node.getErrors());
499 private void addValidationDataEntity(
500 Map<CompositionEntityId, CompositionEntityValidationData> entitiesValidationData,
501 CompositionEntityId entityId, CompositionEntity entity) {
502 if (entitiesValidationData.containsKey(entityId)) {
506 CompositionEntityValidationData validationData =
507 new CompositionEntityValidationData(entity.getType(), entity.getId());
508 entitiesValidationData.put(entityId, validationData);
510 CompositionEntityId parentEntityId = entityId.getParentId();
511 if (parentEntityId == null) {
512 roots.add(validationData);
514 CompositionEntityData parentEntity = entities.get(parentEntityId);
515 if (parentEntity == null) {
516 roots.add(validationData);
518 addValidationDataEntity(entitiesValidationData, parentEntityId, parentEntity.entity);
519 entitiesValidationData.get(parentEntityId).addSubEntityValidationData(validationData);
524 private void addErrorsToTree(CompositionEntityValidationData node,
525 CompositionEntityId parentNodeId,
526 Map<CompositionEntityId, Collection<String>> errors) {
530 CompositionEntityId nodeId = new CompositionEntityId(node.getEntityId(), parentNodeId);
531 node.setErrors(errors.get(nodeId));
533 if (node.getSubEntitiesValidationData() != null) {
534 node.getSubEntitiesValidationData()
535 .forEach(subNode -> addErrorsToTree(subNode, nodeId, errors));
539 private Collection<String> validateQuestionnaire(CompositionEntityData compositionEntityData) {
540 logger.debug(String.format("validateQuestionnaire start: " +
541 "[entity.type]=%s, [entity.id]=%s, [entity.questionnaireString]=%s",
542 compositionEntityData.entity.getType().name(),
543 compositionEntityData.entity.getCompositionEntityId().toString(),
544 compositionEntityData.entity.getQuestionnaireData()));
546 if (Objects.isNull(compositionEntityData.entity.getQuestionnaireData()) ||
547 !JsonUtil.isValidJson(compositionEntityData.entity.getQuestionnaireData())) {
548 return Collections.singletonList(String
549 .format(MISSING_OR_INVALID_QUESTIONNAIRE_MSG, compositionEntityData.entity.getType()));
552 return JsonUtil.validate(
553 compositionEntityData.entity.getQuestionnaireData() == null
554 ? JsonUtil.object2Json(new Object())
555 : compositionEntityData.entity.getQuestionnaireData(),
556 getSchema(compositionEntityData.entity.getType(), SchemaTemplateContext.questionnaire,
557 compositionEntityData.schemaTemplateInput));
560 private String getSchema(CompositionEntityType compositionEntityType,
561 SchemaTemplateContext schemaTemplateContext,
562 SchemaTemplateInput schemaTemplateInput) {
563 return schemaTemplateInput == null
564 ? nonDynamicSchemas.computeIfAbsent(compositionEntityType,
565 k -> generateSchema(schemaTemplateContext, compositionEntityType, null))
566 : generateSchema(schemaTemplateContext, compositionEntityType, schemaTemplateInput);
569 private static class CompositionEntityData {
570 private CompositionEntity entity;
571 private SchemaTemplateInput schemaTemplateInput;
573 CompositionEntityData(CompositionEntity entity, SchemaTemplateInput schemaTemplateInput) {
574 this.entity = entity;
575 this.schemaTemplateInput = schemaTemplateInput;
580 // todo - make SchemaGenerator non static and mock it in UT instead of mocking this method (and
581 // make the method private
582 protected String generateSchema(SchemaTemplateContext schemaTemplateContext,
583 CompositionEntityType compositionEntityType,
584 SchemaTemplateInput schemaTemplateInput) {
585 return SchemaGenerator
586 .generate(schemaTemplateContext, compositionEntityType, schemaTemplateInput);
590 public DeploymentFlavorEntity createDeploymentFlavor(DeploymentFlavorEntity deploymentFlavor) {
591 deploymentFlavor.setId(CommonMethods.nextUuId());
592 deploymentFlavorDao.create(deploymentFlavor);
593 return deploymentFlavor;
597 public ImageEntity createImage(ImageEntity image) {
598 image.setId(CommonMethods.nextUuId());
600 image.setQuestionnaireData(
601 new JsonSchemaDataGenerator(SchemaGenerator
602 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.image, null))
605 imageDao.create(image);
609 public void saveComputesFlavorByComponent(String vspId, Version version, Component component,
610 String componentId) {
611 if (CollectionUtils.isNotEmpty(component.getCompute())) {
612 for (ComputeData flavor : component.getCompute()) {
613 ComputeEntity computeEntity = new ComputeEntity(vspId, version, componentId, null);
614 computeEntity.setComputeCompositionData(flavor);
615 computeEntity.setQuestionnaireData(
616 new JsonSchemaDataGenerator(SchemaGenerator
617 .generate(SchemaTemplateContext.questionnaire, CompositionEntityType.compute,
618 null)).generateData());
620 computeDao.create(computeEntity);
625 public void saveImagesByComponent(String vspId, Version version, Component component, String
627 if (CollectionUtils.isNotEmpty(component.getImages())) {
628 for (Image img : component.getImages()) {
629 ImageEntity imageEntity = new ImageEntity(vspId, version, componentId, null);
630 imageEntity.setImageCompositionData(img);
631 createImage(imageEntity);