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.be.components.impl;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.EnumMap;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.Optional;
35 import java.util.function.Function;
36 import java.util.regex.Pattern;
37 import java.util.stream.Collectors;
39 import org.apache.commons.collections.CollectionUtils;
40 import org.apache.commons.collections.MapUtils;
41 import org.apache.commons.io.FilenameUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.commons.lang3.math.NumberUtils;
44 import org.apache.commons.lang3.tuple.ImmutablePair;
45 import org.codehaus.jackson.JsonGenerationException;
46 import org.codehaus.jackson.map.JsonMappingException;
47 import org.openecomp.sdc.be.config.BeEcompErrorManager;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
51 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
52 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
53 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
56 import org.openecomp.sdc.be.info.ArtifactDefinitionInfo;
57 import org.openecomp.sdc.be.info.ArtifactTemplateInfo;
58 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
59 import org.openecomp.sdc.be.model.ArtifactDefinition;
60 import org.openecomp.sdc.be.model.Component;
61 import org.openecomp.sdc.be.model.ComponentInstance;
62 import org.openecomp.sdc.be.model.ComponentParametersView;
63 import org.openecomp.sdc.be.model.DataTypeDefinition;
64 import org.openecomp.sdc.be.model.GroupDefinition;
65 import org.openecomp.sdc.be.model.GroupInstance;
66 import org.openecomp.sdc.be.model.GroupInstanceProperty;
67 import org.openecomp.sdc.be.model.GroupProperty;
68 import org.openecomp.sdc.be.model.GroupTypeDefinition;
69 import org.openecomp.sdc.be.model.PropertyDefinition;
70 import org.openecomp.sdc.be.model.PropertyDefinition.GroupInstancePropertyValueUpdateBehavior;
71 import org.openecomp.sdc.be.model.PropertyDefinition.PropertyNames;
72 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
73 import org.openecomp.sdc.be.model.jsontitan.operations.ArtifactsOperations;
74 import org.openecomp.sdc.be.model.jsontitan.operations.GroupsOperation;
75 import org.openecomp.sdc.be.model.Resource;
76 import org.openecomp.sdc.be.model.User;
77 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
78 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
79 import org.openecomp.sdc.be.model.operations.impl.GroupOperation;
80 import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
81 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
82 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
83 import org.openecomp.sdc.common.api.Constants;
84 import org.openecomp.sdc.exception.ResponseFormat;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
87 import org.springframework.beans.factory.annotation.Autowired;
89 import com.google.gson.Gson;
90 import com.google.gson.GsonBuilder;
91 import com.google.gson.JsonSyntaxException;
93 import fj.data.Either;
95 @org.springframework.stereotype.Component("groupBusinessLogic")
96 public class GroupBusinessLogic extends BaseBusinessLogic {
97 private static String ADDING_GROUP = "AddingGroup";
99 public static final String INITIAL_VERSION = "1";
101 private static final String CREATE_GROUP = "CreateGroup";
103 private static final String UPDATE_GROUP = "UpdateGroup";
105 private static final String GET_GROUP = "GetGroup";
107 private static Logger log = LoggerFactory.getLogger(GroupBusinessLogic.class.getName());
109 public GroupBusinessLogic() {
113 @javax.annotation.Resource
114 private GroupTypeOperation groupTypeOperation;
117 ArtifactsOperations artifactsOperation;
120 private GroupsOperation groupsOperation;
122 private ApplicationDataTypeCache dataTypeCache;
126 * 1. validate user exist
128 * 2. validate component can be edited
130 * 3. verify group not already exist
132 * 4. verify type of group exist
134 * 5. verify Component instances exist under the component
136 * 6. verify the component instances type are allowed according to the member types in the group type
138 * 7. verify the artifacts belongs to the component
142 * @param componentType
143 * @param groupDefinition
144 * @param inTransaction
148 * public Either<GroupDefinition, ResponseFormat> createGroup(String componentId, String userId, ComponentTypeEnum componentType, GroupDefinition groupDefinition, boolean inTransaction) {
150 * Either<GroupDefinition, ResponseFormat> result = null;
152 * try { Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, CREATE_GROUP, inTransaction);
154 * if (validateUserExists.isRight()) { result = Either.right(validateUserExists.right().value()); return result; }
156 * User user = validateUserExists.left().value(); // 5. check service/resource existence // 6. check service/resource check out // 7. user is owner of checkout state org.openecomp.sdc.be.model.Component component = null;
158 * // String realComponentId = componentType == // ComponentTypeEnum.RESOURCE_INSTANCE ? parentId : componentId; String realComponentId = componentId;
160 * ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); componentParametersView.setIgnoreGroups(false); componentParametersView.setIgnoreArtifacts(false);
161 * componentParametersView.setIgnoreUsers(false); componentParametersView.setIgnoreComponentInstances(false);
163 * Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(realComponentId, componentType, componentParametersView);
165 * if (validateComponent.isRight()) { result = Either.right(validateComponent.right().value()); return result; } component = validateComponent.left().value(); Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component,
166 * userId); if (canWork.isRight()) { result = Either.right(canWork.right().value()); return result; }
168 * result = this.createGroup(component, user, componentType, groupDefinition, inTransaction); return result;
172 * if (false == inTransaction) {
174 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
181 private String getComponentTypeForResponse(org.openecomp.sdc.be.model.Component component) {
182 String componentTypeForResponse = "SERVICE";
183 if (component instanceof Resource) {
184 componentTypeForResponse = ((Resource) component).getResourceType().name();
186 return componentTypeForResponse;
190 * Verify that the artifact members belongs to the component
196 private Either<Boolean, ResponseFormat> verifyArtifactsBelongsToComponent(Component component, List<String> artifacts, String context) {
198 if (artifacts == null || true == artifacts.isEmpty()) {
199 return Either.left(true);
202 Map<String, ArtifactDefinition> deploymentArtifacts = component.getDeploymentArtifacts();
203 if (deploymentArtifacts == null || true == deploymentArtifacts.isEmpty()) {
204 BeEcompErrorManager.getInstance().logInvalidInputError(context, "No deployment artifact found under component " + component.getNormalizedName(), ErrorSeverity.INFO);
205 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
208 List<String> currentArtifacts = deploymentArtifacts.values().stream().map(p -> p.getUniqueId()).collect(Collectors.toList());
209 log.debug("The deployment artifacts of component {} are {}", component.getNormalizedName(), deploymentArtifacts);
210 if (false == currentArtifacts.containsAll(artifacts)) {
211 BeEcompErrorManager.getInstance().logInvalidInputError(context, "Not all artifacts belongs to component " + component.getNormalizedName(), ErrorSeverity.INFO);
212 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
215 return Either.left(true);
220 * verify that the members are component instances of the component
223 * @param componentType
224 * @param groupMembers
225 * @param memberToscaTypes
228 private Either<Boolean, ResponseFormat> verifyComponentInstancesAreValidMembers(Component component, ComponentTypeEnum componentType, String groupName, String groupType, Map<String, String> groupMembers, List<String> memberToscaTypes) {
230 if (groupMembers == null || true == groupMembers.isEmpty()) {
231 return Either.left(true);
234 if (memberToscaTypes == null || true == memberToscaTypes.isEmpty()) {
235 return Either.left(true);
238 List<ComponentInstance> componentInstances = component.getComponentInstances();
239 if (componentInstances != null && false == componentInstances.isEmpty()) {
240 Map<String, ComponentInstance> compInstUidToCompInstMap = componentInstances.stream().collect(Collectors.toMap(p -> p.getUniqueId(), p -> p));
242 Set<String> allCompInstances = compInstUidToCompInstMap.keySet();
244 for (Entry<String, String> groupMember : groupMembers.entrySet()) {
245 String compName = groupMember.getKey();
246 String compUid = groupMember.getValue();
248 if (false == allCompInstances.contains(compUid)) {
250 * %1 - member name %2 - group name %3 - VF name %4 - component type [VF ]
252 String componentTypeForResponse = getComponentTypeForResponse(component);
254 BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_GROUP, "Not all group members exists under the component", ErrorSeverity.INFO);
255 return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_INVALID_COMPONENT_INSTANCE, compName, groupName, component.getNormalizedName(), componentTypeForResponse));
259 // ComponentOperation componentOperation = getComponentOperationByParentComponentType(componentType);
260 // if (componentOperation instanceof ResourceOperation) {
261 // ResourceOperation resourceOperation = (ResourceOperation) componentOperation;
263 // for (Entry<String, String> groupMember : groupMembers.entrySet()) {
265 // String componentInstName = groupMember.getKey();
266 // String componentInstUid = groupMember.getValue();
268 // ComponentInstance componentInstance = compInstUidToCompInstMap.get(componentInstUid);
269 // String componentUid = componentInstance.getComponentUid();
270 // List<String> componentToscaNames = new ArrayList<>();
271 // TitanOperationStatus status = resourceOperation.fillResourceDerivedListFromGraph(componentUid, componentToscaNames);
272 // if (status != TitanOperationStatus.OK) {
273 // BeEcompErrorManager.getInstance().logInternalFlowError(CREATE_GROUP, "Cannot find tosca list of component id " + componentUid, ErrorSeverity.ERROR);
274 // return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
277 // log.debug("The tosca names of component id {} are {}", componentUid, memberToscaTypes);
279 // boolean found = false;
280 // for (String memberToscaType : memberToscaTypes) {
281 // if (componentToscaNames.contains(memberToscaType)) {
286 // if (found == false) {
287 // BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_GROUP,
288 // "No tosca types from " + memberToscaTypes + " can be found in the tosca list " + componentToscaNames + " of component " + componentInstance.getNormalizedName(), ErrorSeverity.INFO);
290 // * # %1 - member name # %2 - group name # %3 - group type
292 // return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_INVALID_TOSCA_NAME_OF_COMPONENT_INSTANCE, componentInstName, groupName, groupType));
294 // log.debug("Component instance {} fits to one of the required tosca types", componentInstance.getNormalizedName());
298 // BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_GROUP, "Cannot find tosca list since it is not supported for product", ErrorSeverity.ERROR);
299 // return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
304 return Either.left(true);
308 * Update specific group version
310 * @param groupDefinition
311 * @param inTransaction
315 * public Either<GroupDefinition, StorageOperationStatus> updateGroupVersion(GroupDefinition groupDefinition, boolean inTransaction) { Either<GroupDefinition, StorageOperationStatus> result = null; List<String> groupIdsToUpdateVersion = new
316 * ArrayList<>(); groupIdsToUpdateVersion.add(groupDefinition.getUniqueId()); Either<List<GroupDefinition>, StorageOperationStatus> updateGroupVersion = updateGroupVersion(groupIdsToUpdateVersion, inTransaction); if (updateGroupVersion.isLeft())
317 * { result = Either.left(updateGroupVersion.left().value().get(0)); } else { log.debug("Failed to update group version. Status is {} ", updateGroupVersion.right().value()); result = Either.right(updateGroupVersion.right().value()); } return
322 * Update list of groups versions
324 * @param groupsUniqueId
325 * @param inTransaction
329 * public Either<List<GroupDefinition>, StorageOperationStatus> updateGroupVersion(List<String> groupsUniqueId, boolean inTransaction) {
331 * Either<List<GroupDefinition>, StorageOperationStatus> result = null;
335 * result = groupOperation.updateGroupVersion(groupsUniqueId, true);
341 * if (false == inTransaction) {
343 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
353 * Update GroupDefinition metadata
357 * @param componentType
358 * @param updatedGroup
359 * @param inTransaction
362 public Either<GroupDefinition, ResponseFormat> validateAndUpdateGroupMetadata(String componentId, User user, ComponentTypeEnum componentType, GroupDefinition updatedGroup, boolean inTransaction , boolean shouldLock) {
364 Either<GroupDefinition, ResponseFormat> result = null;
366 // Validate user exist
367 Either<User, ResponseFormat> validateUserExists = validateUserExists(user.getUserId(), UPDATE_GROUP, inTransaction);
368 if (validateUserExists.isRight()) {
369 result = Either.right(validateUserExists.right().value());
372 // Validate component exist
373 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(componentId, componentType, null);
374 if (validateComponent.isRight()) {
375 result = Either.right(validateComponent.right().value());
378 org.openecomp.sdc.be.model.Component component = validateComponent.left().value();
379 // validate we can work on component
380 Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, user.getUserId());
381 if (canWork.isRight()) {
382 result = Either.right(canWork.right().value());
385 List<GroupDefinition> currentGroups = component.getGroups();
386 if (CollectionUtils.isEmpty(currentGroups)) {
387 log.error("Failed to update the metadata of group {} on component {}. The status is {}. ", updatedGroup.getName(), component.getName(), ActionStatus.GROUP_IS_MISSING);
388 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_IS_MISSING, updatedGroup.getName(), component.getName(), component.getComponentType().getValue()));
391 // Validate groups exists in the component
392 Optional<GroupDefinition> currentGroupOpt = currentGroups.stream().filter(g -> g.getUniqueId().equals(updatedGroup.getUniqueId())).findAny();
393 if (!currentGroupOpt.isPresent()) {
394 log.error("Failed to update the metadata of group {} on component {}. The status is {}. ", updatedGroup.getName(), component.getName(), ActionStatus.GROUP_IS_MISSING);
395 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_IS_MISSING, updatedGroup.getName(), component.getName(), component.getComponentType().getValue()));
398 GroupDefinition currentGroup = currentGroupOpt.get();
400 Either<Boolean, ResponseFormat> lockResult = lockComponent(componentId, component, "Update GroupDefinition Metadata");
401 if (lockResult.isRight()) {
402 result = Either.right(lockResult.right().value());
406 // Validate group type is vfModule
407 if (!currentGroup.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE)) {
408 log.error("Failed to update the metadata of group {}. Group type is {} and different then: {}", currentGroup.getName(), currentGroup.getType(), Constants.DEFAULT_GROUP_VF_MODULE);
409 ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_TYPE, updatedGroup.getType());
410 result = Either.right(responseFormat);
413 result = updateGroupMetadata(component, currentGroup, updatedGroup);
417 if (result.isLeft()) {
423 graphLockOperation.unlockComponent(componentId, componentType.getNodeType());
427 private Either<GroupDefinition, ResponseFormat> updateGroupMetadata(Component component, GroupDefinition currentGroup, GroupDefinition updatedGroup) {
428 String currentGroupName = currentGroup.getName();
429 Either<GroupDefinition, ResponseFormat> result = validateAndUpdateGroupMetadata(currentGroup, updatedGroup);
431 if (result.isRight()) {
432 log.debug("Failed to validate a metadata of the group {} on component {}. ", updatedGroup.getName(), component.getName());
434 if (result.isLeft()) {
435 result = updateGroup(component, currentGroup, currentGroupName);
440 private Either<GroupDefinition, ResponseFormat> updateGroup(Component component, GroupDefinition updatedGroup, String currentGroupName) {
441 Either<GroupDefinition, StorageOperationStatus> handleGroupRes;
442 Either<GroupDefinition, ResponseFormat> result = null;
443 if (updatedGroup.getName().equals(currentGroupName)) {
444 handleGroupRes = groupsOperation.updateGroup(component, updatedGroup);
445 if (handleGroupRes.isRight()) {
446 log.debug("Failed to update a metadata of the group {} on component {}. ", updatedGroup.getName(), component.getName());
447 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(handleGroupRes.right().value())));
450 StorageOperationStatus deleteStatus = groupsOperation.deleteGroup(component, currentGroupName);
451 if (deleteStatus != StorageOperationStatus.OK) {
452 log.debug("Failed to delete the group {} from component {}. ", updatedGroup.getName(), component.getName());
453 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(deleteStatus)));
455 handleGroupRes = groupsOperation.addGroup(component, updatedGroup);
456 if (handleGroupRes.isRight()) {
457 log.debug("Failed to add the group {} to component {}. ", updatedGroup.getName(), component.getName());
458 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(handleGroupRes.right().value())));
461 if (result == null) {
462 result = Either.left(updatedGroup);
468 * Validate and Update Group Property
471 * @param groupUniqueId
473 * @param componentType
474 * @param groupPropertiesToUpdate
475 * @param inTransaction
478 public Either<List<GroupProperty>, ResponseFormat> validateAndUpdateGroupProperties(String componentId, String groupUniqueId, User user, ComponentTypeEnum componentType, List<GroupProperty> groupPropertiesToUpdate, boolean inTransaction) {
480 Either<List<GroupProperty>, ResponseFormat> result = Either.left(groupPropertiesToUpdate);
482 Optional<GroupDefinition> optionalGroupConnectedToVf = null;
483 GroupDefinition currentGroup = null;
484 StorageOperationStatus lockResult = graphLockOperation.lockComponent(componentId, componentType.getNodeType());
485 if (lockResult != StorageOperationStatus.OK) {
486 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockResult, componentType), componentId));
488 if (result.isLeft()) {
489 // VF exist because lock succedded
490 Resource vf = (Resource) toscaOperationFacade.getToscaElement(componentId).left().value();
491 optionalGroupConnectedToVf =
492 // All groups on resource
493 vf.getGroups().stream().
494 // Filter in group sent is part of VF groups
495 filter(e -> e.getUniqueId().equals(groupUniqueId)).
498 if (!optionalGroupConnectedToVf.isPresent()) {
499 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_IS_MISSING, groupUniqueId, vf.getName(), ComponentTypeEnum.RESOURCE.getValue()));
503 if (result.isLeft()) {
504 currentGroup = optionalGroupConnectedToVf.get();
505 result = validateGroupPropertyAndResetEmptyValue(currentGroup, groupPropertiesToUpdate);
507 if (result.isLeft()) {
508 result = updateGroupPropertiesValue(componentId, currentGroup, groupPropertiesToUpdate, inTransaction);
509 if (result.isRight()) {
510 BeEcompErrorManager.getInstance().logBeSystemError("Update GroupProperties");
511 log.debug("failed to update Vf {}", componentId);
515 } catch (Exception e) {
516 log.debug("Error in validateAndUpdateGroupProperty {}", e);
517 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
519 graphLockOperation.unlockComponent(componentId, componentType.getNodeType());
524 private void resetEmptyValueWithDefaults(List<GroupProperty> groupPropertiesToUpdate, GroupDefinition originalGroup) {
525 Map<String, GroupProperty> originalProperties =
526 // Stream of original properties from group
527 originalGroup.convertToGroupProperties().stream().
528 // Collecting to map with name as key
529 collect(Collectors.toMap(e -> e.getName(), e -> e));
530 for (GroupProperty gp : groupPropertiesToUpdate) {
531 if (StringUtils.isEmpty(gp.getValue())) {
532 gp.setValue(originalProperties.get(gp.getName()).getDefaultValue());
538 private Either<List<GroupProperty>, ResponseFormat> validateGroupPropertyAndResetEmptyValue(GroupDefinition originalGroup, List<GroupProperty> groupPropertiesToUpdate) {
540 Either<List<GroupProperty>, ResponseFormat> ret = validateOnlyValueChanged(groupPropertiesToUpdate, originalGroup);
542 resetEmptyValueWithDefaults(groupPropertiesToUpdate, originalGroup);
545 // Validate Type Match Value
546 Optional<StorageOperationStatus> optionalError =
547 // Stream of group properties
548 groupPropertiesToUpdate.stream().
549 // Validate each and map to returned Strorage status value
550 map(e -> groupOperation.validateAndUpdatePropertyValue(e)).
551 // Keep only failed result if there is such
552 filter(e -> e != StorageOperationStatus.OK).
555 if (optionalError.isPresent()) {
556 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(optionalError.get());
557 ret = Either.right(componentsUtils.getResponseFormat(actionStatus));
562 // Validate min max ect...
563 ret = validatePropertyBusinessLogic(groupPropertiesToUpdate, originalGroup);
569 private Either<List<GroupProperty>, ResponseFormat> validatePropertyBusinessLogic(List<GroupProperty> groupPropertiesToUpdate, GroupDefinition originalGroup) {
571 Either<List<GroupProperty>, ResponseFormat> ret = Either.left(groupPropertiesToUpdate);
573 Map<PropertyNames, String> nameValueMap = new HashMap<>();
574 for (GroupProperty gp : groupPropertiesToUpdate) {
575 // Filter out non special properties which does not have Enum
576 final PropertyNames gpEnum = PropertyNames.findName(gp.getName());
577 if (gpEnum != null) {
578 nameValueMap.put(gpEnum, gp.getValue());
582 if (!MapUtils.isEmpty(nameValueMap)) {
584 if (nameValueMap.containsKey(PropertyNames.INITIAL_COUNT) || nameValueMap.containsKey(PropertyNames.MAX_INSTANCES) || nameValueMap.containsKey(PropertyNames.MIN_INSTANCES)) {
586 Map<PropertyNames, String> oldValueMap = prepareMapWithOriginalProperties(originalGroup);
588 Either<Boolean, ResponseFormat> eitherValid = validateMinMaxAndInitialCountPropertyLogicVF(nameValueMap, oldValueMap);
589 if (eitherValid.isRight()) {
590 ret = Either.right(eitherValid.right().value());
593 if (ret.isLeft() && (nameValueMap.containsKey(PropertyNames.VF_MODULE_DESCRIPTION) || nameValueMap.containsKey(PropertyNames.VF_MODULE_LABEL))) {
595 Optional<ResponseFormat> optionalError =
596 // Stream of group Properties
597 groupPropertiesToUpdate.stream().
598 // Filter in only properties that needs text validation
599 filter(e -> enumHasValueFilter(e.getName(), enumName -> PropertyNames.findName(enumName), PropertyNames.VF_MODULE_DESCRIPTION, PropertyNames.VF_MODULE_LABEL)).
600 // validate text properties
601 map(e -> validateFreeText(e)).
602 // filter in only errors if exist
603 filter(e -> e.isRight()).
604 // map the Either value to the Error
605 map(e -> e.right().value())
608 if (optionalError.isPresent()) {
609 ret = Either.right(optionalError.get());
618 private Map<PropertyNames, String> prepareMapWithOriginalProperties(GroupDefinition originalGroup) {
619 Map<PropertyNames, String> oldValueMap = new HashMap<>();
620 PropertyNames[] propertiesToCheck = new PropertyNames[] { PropertyNames.INITIAL_COUNT, PropertyNames.MAX_INSTANCES, PropertyNames.MIN_INSTANCES };
622 for (GroupProperty gp : originalGroup.convertToGroupProperties()) {
623 if (enumHasValueFilter(gp.getName(), enumName -> PropertyNames.findName(enumName), propertiesToCheck)) {
624 oldValueMap.put(PropertyNames.findName(gp.getName()), gp.getValue());
627 if (StringUtils.isEmpty(oldValueMap.get(PropertyNames.MAX_INSTANCES))) {
628 oldValueMap.put(PropertyNames.MAX_INSTANCES, String.valueOf(Integer.MAX_VALUE));
633 private Either<List<GroupProperty>, ResponseFormat> validateOnlyValueChanged(List<GroupProperty> groupPropertiesToUpdate, GroupDefinition originalGroup) {
635 Either<List<GroupProperty>, ResponseFormat> ret = Either.left(groupPropertiesToUpdate);
636 if (CollectionUtils.isEmpty(groupPropertiesToUpdate)) {
637 ret = Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, StringUtils.EMPTY));
638 } else if (CollectionUtils.isEmpty(originalGroup.getProperties())) {
639 ret = Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, groupPropertiesToUpdate.get(NumberUtils.INTEGER_ZERO).getName()));
641 Map<String, GroupProperty> namePropertyMap =
642 // Original Group Properties Stream
643 originalGroup.convertToGroupProperties().stream().
644 // Collect to map with name as key
645 collect(Collectors.toMap(e -> e.getName(), e -> e));
647 Optional<GroupProperty> optionalMissingProperty =
648 // Group Properties to be updated Stream
649 groupPropertiesToUpdate.stream().
650 // Filter in property that is not contained in original if there is such
651 filter(e -> !namePropertyMap.containsKey(e.getName())).
655 if (optionalMissingProperty.isPresent()) {
656 ret = Either.right(componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, optionalMissingProperty.get().getName()));
658 Optional<GroupProperty> optionalNonValueChange =
659 // groups to be updated stream
660 groupPropertiesToUpdate.stream().
661 // filter in only properties with non-value (illegal) change
662 filter(e -> !isOnlyGroupPropertyValueChanged(e, namePropertyMap.get(e.getName()))).
665 if (optionalNonValueChange.isPresent()) {
666 ret = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY, optionalNonValueChange.get().getName()));
676 * if groupProperty are the same or if only value is different returns true, otherwise returns false.
678 * @param groupProperty
679 * @param groupProperty2
682 private boolean isOnlyGroupPropertyValueChanged(GroupProperty groupProperty, GroupProperty groupProperty2) {
683 // Create 2 duplicates for groupPropery and reset their values
684 Gson gson = new GsonBuilder().setPrettyPrinting().create();
686 GroupProperty groupPropertyDuplicate = new GroupProperty(groupProperty);
687 groupPropertyDuplicate.setValue(null);
688 groupPropertyDuplicate.setSchema(null);
689 groupPropertyDuplicate.setParentUniqueId(null);
690 // GroupProperty groupProperty2Duplicate = gson.fromJson(JsonParserUtils.jsonToString(groupProperty2), GroupProperty.class);
691 GroupProperty groupProperty2Duplicate = new GroupProperty(groupProperty2);
692 groupProperty2Duplicate.setValue(null);
693 groupProperty2Duplicate.setSchema(null);
694 groupProperty2Duplicate.setParentUniqueId(null);
695 return groupPropertyDuplicate.equals(groupProperty2Duplicate) && StringUtils.equals(groupPropertyDuplicate.getValueUniqueUid(), groupProperty2Duplicate.getValueUniqueUid());
696 } catch (Exception e) {
697 log.debug("Failed validate group properties. ", e);
703 * Validate and update GroupDefinition metadata
706 * @param currentGroup
710 private Either<GroupDefinition, ResponseFormat> validateAndUpdateGroupMetadata(GroupDefinition currentGroup, GroupDefinition groupUpdate) {
711 // Check if to update, and update GroupDefinition name.
712 Either<Boolean, ResponseFormat> response = validateAndUpdateGroupName(currentGroup, groupUpdate);
713 if (response.isRight()) {
714 ResponseFormat errorResponse = response.right().value();
715 return Either.right(errorResponse);
718 // Do not allow to update GroupDefinition version directly.
719 String versionUpdated = groupUpdate.getVersion();
720 String versionCurrent = currentGroup.getVersion();
721 if (versionUpdated != null && !versionCurrent.equals(versionUpdated)) {
722 log.info("update Group: recived request to update version to {} the field is not updatable ignoring.", versionUpdated);
725 return Either.left(currentGroup);
729 * Validate and update GroupDefinition name
732 * @param currentGroup
736 private Either<Boolean, ResponseFormat> validateAndUpdateGroupName(GroupDefinition currentGroup, GroupDefinition groupUpdate) {
737 String nameUpdated = groupUpdate.getName();
738 String nameCurrent = currentGroup.getName();
739 if (!nameCurrent.equals(nameUpdated)) {
740 Either<Boolean, ResponseFormat> validatNameResponse = validateGroupName(currentGroup.getName(), groupUpdate.getName() ,true);
741 if (validatNameResponse.isRight()) {
742 ResponseFormat errorRespons = validatNameResponse.right().value();
743 return Either.right(errorRespons);
745 currentGroup.setName(groupUpdate.getName());
747 return Either.left(true);
751 * Validate that group name to update is valid (same as current group name except for middle part). For example: Current group name: MyResource..MyDesc..Module-1 Group to update: MyResource..MyDesc2..Module-1 Verify that only the second part
752 * MyDesc was changed.
754 * @param currentGroupName
755 * @param groupUpdateName
758 private Either<Boolean, ResponseFormat> validateGroupName(String currentGroupName, String groupUpdateName , boolean isforceNameModification) {
760 // Check if the group name is in old format.
761 if (Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(groupUpdateName).matches()) {
762 log.error("Group name {} is in old format", groupUpdateName);
763 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME, groupUpdateName));
766 // Check that name pats 1 and 3 did not changed (only the second
767 // part can be changed)
768 // But verify before that the current group format is the new one
769 if (!Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(currentGroupName).matches()) {
770 String[] split1 = currentGroupName.split("\\.\\.");
771 String currentResourceName = split1[0];
772 String currentCounter = split1[2];
774 String[] split2 = groupUpdateName.split("\\.\\.");
775 String groupUpdateResourceName = split2[0];
776 String groupUpdateCounter = split2[2];
777 if (!isforceNameModification){ //if not forced ,allow name prefix&suffix validation [no changes]
778 if (!currentResourceName.equals(groupUpdateResourceName)) {
779 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME_MODIFICATION, currentResourceName));
782 if (!currentCounter.equals(groupUpdateCounter)) {
783 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME_MODIFICATION, currentCounter));
789 return Either.left(true);
790 } catch (Exception e) {
791 log.error("Error valiadting group name", e);
792 return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
797 * associate artifacts to a given group
801 * @param componentType
803 * @param shouldLockComp
804 * @param inTransaction
808 * public Either<List<GroupDefinition>, ResponseFormat> associateArtifactsToGroup(String componentId, String userId, ComponentTypeEnum componentType, List<GroupDefinition> groups, boolean shouldLockComp, boolean inTransaction) {
810 * Either<List<GroupDefinition>, ResponseFormat> result = null;
812 * if (shouldLockComp == true && inTransaction == true) { BeEcompErrorManager.getInstance().logInternalFlowError("dissociateArtifactsFromGroup", "Cannot lock component since we are inside a transaction", ErrorSeverity.ERROR); // Cannot lock
813 * component since we are in a middle of another // transaction. ActionStatus actionStatus = ActionStatus.INVALID_CONTENT; result = Either.right(componentsUtils.getResponseFormat(actionStatus)); return result; }
815 * Component component = null; try {
817 * if (groups == null || groups.isEmpty()) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.OK)); }
819 * Either<Component, ResponseFormat> validateGroupsBeforeUpdate = validateGroupsBeforeUpdate(componentId, userId, componentType, groups, inTransaction); if (validateGroupsBeforeUpdate.isRight()) { result =
820 * Either.right(validateGroupsBeforeUpdate.right().value()); return result; }
822 * component = validateGroupsBeforeUpdate.left().value();
824 * if (shouldLockComp) { Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, "Group - Associate Artifacts"); if (lockComponent.isRight()) { return Either.right(lockComponent.right().value()); } }
826 * List<GroupDefinition> updatedGroups = new ArrayList<>();
828 * List<GroupDefinition> componentGroups = component.getGroups();
830 * // per group, associate to it the artifacts for (GroupDefinition groupDefinition : groups) {
832 * GroupDefinition componentGroup = componentGroups.stream().filter(p -> p.getUniqueId().equals(groupDefinition.getUniqueId())).findFirst().orElse(null); if (componentGroup != null) { List<String> componentArtifacts =
833 * componentGroup.getArtifacts(); int artifactsSizeInGroup = componentArtifacts == null ? 0 : componentArtifacts.size(); if (artifactsSizeInGroup > 0) { List<String> artifactsToAssociate = groupDefinition.getArtifacts();
835 * // if no artifcats sent if (artifactsToAssociate == null || true == artifactsToAssociate.isEmpty()) { continue; }
837 * boolean isChanged = componentArtifacts.removeAll(artifactsToAssociate); if (isChanged) {// I.e. At least one artifact is already // associated to the group log.debug("Some of the artifacts already associated to group {}" ,
838 * groupDefinition.getUniqueId()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_ARTIFACT_ALREADY_ASSOCIATED, componentGroup.getName())); } } }
840 * Either<GroupDefinition, StorageOperationStatus> associateArtifactsToGroup = groupOperation.associateArtifactsToGroup(groupDefinition.getUniqueId(), groupDefinition.getArtifacts(), true);
842 * if (associateArtifactsToGroup.isRight()) { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(associateArtifactsToGroup.right().value()); result = Either.right(componentsUtils.getResponseFormat(actionStatus));
843 * log.debug("Failed to update group {} under component {}, error: {}", groupDefinition.getName(), component.getNormalizedName(), actionStatus.name()); return result; } updatedGroups.add(associateArtifactsToGroup.left().value());
847 * result = Either.left(updatedGroups); return result;
851 * if (false == inTransaction) {
853 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
857 * // unlock resource if (shouldLockComp && component != null) { graphLockOperation.unlockComponent(componentId, componentType.getNodeType()); }
863 * public Either<List<GroupDefinition>, ResponseFormat> associateMembersToGroup(String componentId, String userId, ComponentTypeEnum componentType, List<GroupDefinition> groups, boolean shouldLockComp, boolean inTransaction) {
865 * Either<List<GroupDefinition>, ResponseFormat> result = null;
867 * if (shouldLockComp == true && inTransaction == true) { BeEcompErrorManager.getInstance().logInternalFlowError("dissociateArtifactsFromGroup", "Cannot lock component since we are inside a transaction", ErrorSeverity.ERROR); // Cannot lock
868 * component since we are in a middle of another // transaction. ActionStatus actionStatus = ActionStatus.INVALID_CONTENT; result = Either.right(componentsUtils.getResponseFormat(actionStatus)); return result; }
870 * Component component = null; try {
872 * if (groups == null || groups.isEmpty()) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.OK)); }
874 * Either<Component, ResponseFormat> validateGroupsBeforeUpdate = validateGroupsBeforeUpdate(componentId, userId, componentType, groups, inTransaction); if (validateGroupsBeforeUpdate.isRight()) { result =
875 * Either.right(validateGroupsBeforeUpdate.right().value()); return result; }
877 * component = validateGroupsBeforeUpdate.left().value();
879 * if (shouldLockComp) { Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, "Group - Associate Members"); if (lockComponent.isRight()) { return Either.right(lockComponent.right().value()); } }
881 * List<GroupDefinition> updatedGroups = new ArrayList<>();
883 * // per group, associate to it the members for (GroupDefinition groupDefinition : groups) {
885 * Either<GroupDefinition, StorageOperationStatus> associateMembersToGroup = groupOperation.associateMembersToGroup(groupDefinition.getUniqueId(), groupDefinition.getMembers(), true);
887 * if (associateMembersToGroup.isRight()) { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(associateMembersToGroup.right().value()); result = Either.right(componentsUtils.getResponseFormat(actionStatus));
888 * log.debug("Failed to update group {} under component {}, error: {}", groupDefinition.getName(), component.getNormalizedName(), actionStatus.name()); return result; } else { updatedGroups.add(associateMembersToGroup.left().value()); }
892 * result = Either.left(updatedGroups); return result;
896 * if (false == inTransaction) {
898 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
902 * // unlock resource if (shouldLockComp && component != null) { graphLockOperation.unlockComponent(componentId, componentType.getNodeType()); }
908 * associate artifacts to a given group
912 * @param componentType
914 * @param shouldLockComp
915 * @param inTransaction
918 public Either<GroupDefinitionInfo, ResponseFormat> getGroupWithArtifactsById(ComponentTypeEnum componentType, String componentId, String groupId, String userId, boolean inTransaction) {
920 Either<GroupDefinitionInfo, ResponseFormat> result = null;
922 // Validate user exist
923 Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, UPDATE_GROUP, true);
925 if (validateUserExists.isRight()) {
926 result = Either.right(validateUserExists.right().value());
930 // Validate component exist
931 org.openecomp.sdc.be.model.Component component = null;
932 String realComponentId = componentId;
935 ComponentParametersView componentParametersView = new ComponentParametersView();
936 componentParametersView.disableAll();
937 componentParametersView.setIgnoreGroups(false);
938 componentParametersView.setIgnoreArtifacts(false);
939 componentParametersView.setIgnoreUsers(false);
941 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(realComponentId, componentType, componentParametersView);
942 if (validateComponent.isRight()) {
943 result = Either.right(validateComponent.right().value());
946 component = validateComponent.left().value();
948 Either<GroupDefinition, StorageOperationStatus> groupEither = findGroupOnComponent(component, groupId);
950 if (groupEither.isRight()) {
951 log.debug("Faild to find group {} under component {}", groupId, component.getUniqueId());
952 BeEcompErrorManager.getInstance().logInvalidInputError(GET_GROUP, "group " + groupId + " not found under component " + component.getUniqueId(), ErrorSeverity.INFO);
953 String componentTypeForResponse = getComponentTypeForResponse(component);
954 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_IS_MISSING, groupId, component.getSystemName(), componentTypeForResponse));
957 GroupDefinition group = groupEither.left().value();
959 Boolean isBase = null;// Constants.IS_BASE;
960 List<GroupProperty> props = group.convertToGroupProperties();
961 if (props != null && !props.isEmpty()) {
962 Optional<GroupProperty> isBasePropOp = props.stream().filter(p -> p.getName().equals(Constants.IS_BASE)).findAny();
963 if (isBasePropOp.isPresent()) {
964 GroupProperty propIsBase = isBasePropOp.get();
965 isBase = Boolean.parseBoolean(propIsBase.getValue());
968 BeEcompErrorManager.getInstance().logInvalidInputError(GET_GROUP, "failed to find prop isBase " + component.getNormalizedName(), ErrorSeverity.INFO);
970 // Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
975 List<ArtifactDefinitionInfo> artifacts = new ArrayList<>();
976 List<ArtifactDefinition> artifactsFromComponent = new ArrayList<>();
977 List<String> artifactsIds = group.getArtifacts();
979 Map<String, ArtifactDefinition> deploymentArtifacts = null;
980 if (MapUtils.isNotEmpty(component.getDeploymentArtifacts())) {
981 deploymentArtifacts = component.getDeploymentArtifacts().values().stream().collect(Collectors.toMap(a -> a.getUniqueId(), a -> a));
984 if (artifactsIds != null && !artifactsIds.isEmpty()) {
985 for (String id : artifactsIds) {
986 if (MapUtils.isEmpty(deploymentArtifacts) || !deploymentArtifacts.containsKey(id)) {
987 log.debug("Failed to get artifact {} . Status is {} ", id, StorageOperationStatus.NOT_FOUND);
988 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND));
989 result = Either.right(responseFormat);
992 artifactsFromComponent.add(deploymentArtifacts.get(id));
994 if (!artifactsFromComponent.isEmpty()) {
995 for (ArtifactDefinition artifactDefinition : artifactsFromComponent) {
996 ArtifactDefinitionInfo artifactDefinitionInfo = new ArtifactDefinitionInfo(artifactDefinition);
997 artifacts.add(artifactDefinitionInfo);
1002 GroupDefinitionInfo resultInfo = new GroupDefinitionInfo(group);
1003 resultInfo.setIsBase(isBase);
1004 if (!artifacts.isEmpty())
1005 resultInfo.setArtifacts(artifacts);
1007 result = Either.left(resultInfo);
1013 if (false == inTransaction) {
1015 if (result == null || result.isRight()) {
1016 log.debug("Going to execute rollback on create group.");
1017 titanDao.rollback();
1019 log.debug("Going to execute commit on create group.");
1029 private Either<GroupDefinition, StorageOperationStatus> findGroupOnComponent(Component component, String groupId) {
1031 Either<GroupDefinition, StorageOperationStatus> result = null;
1032 if (CollectionUtils.isNotEmpty(component.getGroups())) {
1033 Optional<GroupDefinition> foundGroup = component.getGroups().stream().filter(g -> g.getUniqueId().equals(groupId)).findFirst();
1034 if (foundGroup.isPresent()) {
1035 result = Either.left(foundGroup.get());
1038 if (result == null) {
1039 result = Either.right(StorageOperationStatus.NOT_FOUND);
1045 * @param componentId
1047 * @param componentType
1049 * @param inTransaction
1052 private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateGroupsBeforeUpdate(String componentId, String userId, ComponentTypeEnum componentType, List<GroupDefinition> groups, boolean inTransaction) {
1054 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> result;
1056 // Validate user exist
1057 Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, UPDATE_GROUP, inTransaction);
1058 if (validateUserExists.isRight()) {
1059 result = Either.right(validateUserExists.right().value());
1063 // Validate component exist
1064 String realComponentId = componentId;
1066 ComponentParametersView componentParametersView = new ComponentParametersView();
1067 componentParametersView.disableAll();
1068 componentParametersView.setIgnoreGroups(false);
1069 componentParametersView.setIgnoreArtifacts(false);
1070 componentParametersView.setIgnoreUsers(false);
1071 componentParametersView.setIgnoreComponentInstances(false);
1073 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(realComponentId, componentType, componentParametersView);
1075 if (validateComponent.isRight()) {
1076 result = Either.right(validateComponent.right().value());
1079 org.openecomp.sdc.be.model.Component component = validateComponent.left().value();
1081 // validate we can work on component
1082 Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, userId);
1083 if (canWork.isRight()) {
1084 result = Either.right(canWork.right().value());
1088 // Validate groups exists in the component
1089 ResponseFormat validateGroupsInComponent = validateGroupsInComponentByFunc(groups, component, p -> p.getUniqueId());
1090 if (validateGroupsInComponent != null) {
1091 result = Either.right(validateGroupsInComponent);
1095 Set<String> artifacts = new HashSet<>();
1096 groups.forEach(p -> {
1097 if (p.getArtifacts() != null) {
1098 artifacts.addAll(p.getArtifacts());
1101 // validate all artifacts belongs to the component
1102 Either<Boolean, ResponseFormat> verifyArtifactsBelongsToComponent = verifyArtifactsBelongsToComponent(component, new ArrayList<>(artifacts), UPDATE_GROUP);
1103 if (verifyArtifactsBelongsToComponent.isRight()) {
1104 result = Either.right(verifyArtifactsBelongsToComponent.right().value());
1108 return Either.left(component);
1115 * - the method to fetch the key of the GroupDefinition(from groups) in order to compare to groups in the component
1118 private ResponseFormat validateGroupsInComponentByFunc(List<GroupDefinition> groups, org.openecomp.sdc.be.model.Component component, Function<GroupDefinition, String> getByParam) {
1119 ResponseFormat result = null;
1121 List<GroupDefinition> currentGroups = component.getGroups();
1123 boolean found = false;
1124 List<String> updatedGroupsName = groups.stream().map(getByParam).collect(Collectors.toList());
1126 List<String> missingGroupNames = updatedGroupsName;
1128 if (currentGroups != null && false == currentGroups.isEmpty()) {
1129 List<String> currentGroupsName = currentGroups.stream().map(getByParam).collect(Collectors.toList());
1131 if (currentGroupsName.containsAll(updatedGroupsName)) {
1134 currentGroupsName.removeAll(currentGroupsName);
1135 missingGroupNames = currentGroupsName;
1138 if (false == found) {
1139 String componentTypeForResponse = getComponentTypeForResponse(component);
1140 String listOfGroups = getAsString(missingGroupNames);
1141 result = componentsUtils.getResponseFormat(ActionStatus.GROUP_IS_MISSING, listOfGroups, component.getSystemName(), componentTypeForResponse);
1148 public String getAsString(List<String> list) {
1150 if (list == null || list.isEmpty()) {
1153 StringBuilder builder = new StringBuilder();
1154 list.forEach(p -> builder.append(p + ","));
1156 String result = builder.toString();
1157 return result.substring(0, result.length());
1162 * dissociate artifacts from a given group
1164 * @param componentId
1166 * @param componentType
1168 * @param shouldLockComp
1169 * @param inTransaction
1173 * public Either<List<GroupDefinition>, ResponseFormat> dissociateArtifactsFromGroup(String componentId, String userId, ComponentTypeEnum componentType, List<GroupDefinition> groups, boolean shouldLockComp, boolean inTransaction) {
1175 * Either<List<GroupDefinition>, ResponseFormat> result = null;
1177 * if (shouldLockComp == true && inTransaction == true) { BeEcompErrorManager.getInstance().logInternalFlowError("dissociateArtifactsFromGroup", "Cannot lock component since we are inside a transaction", ErrorSeverity.ERROR); // Cannot lock
1178 * component since we are in a middle of another // transaction. ActionStatus actionStatus = ActionStatus.INVALID_CONTENT; result = Either.right(componentsUtils.getResponseFormat(actionStatus)); return result; }
1180 * Component component = null;
1184 * if (groups == null || groups.isEmpty()) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.OK)); }
1186 * Either<Component, ResponseFormat> validateGroupsBeforeUpdate = validateGroupsBeforeUpdate(componentId, userId, componentType, groups, inTransaction); if (validateGroupsBeforeUpdate.isRight()) { result =
1187 * Either.right(validateGroupsBeforeUpdate.right().value()); return result; }
1189 * component = validateGroupsBeforeUpdate.left().value();
1191 * if (shouldLockComp) { Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, "Group - Dissociate Artifacts"); if (lockComponent.isRight()) { return Either.right(lockComponent.right().value()); } }
1193 * List<GroupDefinition> updatedGroups = new ArrayList<>();
1195 * List<GroupDefinition> componentGroups = component.getGroups(); // per group, associate to it the artifacts for (GroupDefinition groupDefinition : groups) {
1197 * GroupDefinition componentGroup = componentGroups.stream().filter(p -> p.getUniqueId().equals(groupDefinition.getUniqueId())).findFirst().orElse(null); if (componentGroup != null) { List<String> componentArtifacts =
1198 * componentGroup.getArtifacts(); int artifactsSizeInGroup = componentArtifacts == null ? 0 : componentArtifacts.size(); List<String> artifactsToDissociate = groupDefinition.getArtifacts();
1200 * // if no artifcats sent if (artifactsToDissociate == null || true == artifactsToDissociate.isEmpty()) { continue; }
1202 * if (artifactsSizeInGroup > 0) {
1204 * boolean containsAll = componentArtifacts.containsAll(artifactsToDissociate); if (false == containsAll) { // At least one artifact is // not associated to the // group log.debug("Some of the artifacts already dissociated to group {}" ,
1205 * groupDefinition.getUniqueId()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_ARTIFACT_ALREADY_DISSOCIATED, componentGroup.getName())); } } else { if (artifactsSizeInGroup == 0) { if (artifactsToDissociate != null &&
1206 * false == artifactsToDissociate.isEmpty()) { log.debug("No artifact is found under the group {}" , groupDefinition.getUniqueId()); return Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_ARTIFACT_ALREADY_DISSOCIATED,
1207 * componentGroup.getName())); } } } }
1209 * Either<GroupDefinition, StorageOperationStatus> associateArtifactsToGroup = groupOperation.dissociateArtifactsFromGroup(groupDefinition.getUniqueId(), groupDefinition.getArtifacts(), true);
1211 * if (associateArtifactsToGroup.isRight()) { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(associateArtifactsToGroup.right().value()); result = Either.right(componentsUtils.getResponseFormat(actionStatus));
1212 * log.debug("Failed to update group {} under component {}, error: {}", groupDefinition.getName(), component.getNormalizedName(), actionStatus.name()); return result; } updatedGroups.add(associateArtifactsToGroup.left().value());
1216 * result = Either.left(updatedGroups); return result;
1220 * if (false == inTransaction) {
1222 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
1224 * } // unlock resource if (shouldLockComp && component != null) { graphLockOperation.unlockComponent(componentId, componentType.getNodeType()); }
1232 * public Either<List<GroupDefinition>, ResponseFormat> createGroups(String componentId, String userId, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions, boolean shouldLockComp, boolean inTransaction) {
1234 * Either<List<GroupDefinition>, ResponseFormat> result = null;
1236 * List<GroupDefinition> groups = new ArrayList<>(); org.openecomp.sdc.be.model.Component component = null; try {
1238 * if (groupDefinitions != null && !groupDefinitions.isEmpty()) {
1240 * if (shouldLockComp && inTransaction) { BeEcompErrorManager.getInstance().logInternalFlowError("createGroups", "Cannot lock component since we are inside a transaction", ErrorSeverity.ERROR); // Cannot lock component since we are in a middle of
1241 * another // transaction. ActionStatus actionStatus = ActionStatus.INVALID_CONTENT; result = Either.right(componentsUtils.getResponseFormat(actionStatus)); return result; }
1243 * Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, CREATE_GROUP, true); if (validateUserExists.isRight()) { result = Either.right(validateUserExists.right().value()); return result; }
1245 * User user = validateUserExists.left().value();
1247 * ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); componentParametersView.setIgnoreGroups(false); componentParametersView.setIgnoreArtifacts(false);
1248 * componentParametersView.setIgnoreUsers(false); componentParametersView.setIgnoreComponentInstances(false);
1250 * Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(componentId, componentType, componentParametersView);
1252 * if (validateComponent.isRight()) { result = Either.right(validateComponent.right().value()); return result; } component = validateComponent.left().value();
1254 * if (shouldLockComp) { Either<Boolean, ResponseFormat> lockComponent = lockComponent(component, "CreateGroups"); if (lockComponent.isRight()) { return Either.right(lockComponent.right().value()); } }
1256 * Either<Boolean, ResponseFormat> canWork = validateCanWorkOnComponent(component, userId); if (canWork.isRight()) { result = Either.right(canWork.right().value()); return result; }
1258 * for (GroupDefinition groupDefinition : groupDefinitions) { Either<GroupDefinition, ResponseFormat> createGroup = this.createGroup(component, user, componentType, groupDefinition, true); if (createGroup.isRight()) {
1259 * log.debug("Failed to create group {}." , groupDefinition ); result = Either.right(createGroup.right().value()); return result; } GroupDefinition createdGroup = createGroup.left().value(); groups.add(createdGroup); } }
1261 * result = Either.left(groups); return result;
1265 * if (false == inTransaction) {
1267 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
1269 * } // unlock resource if (shouldLockComp && component != null) { graphLockOperation.unlockComponent(componentId, componentType.getNodeType()); }
1275 * public Either<GroupDefinition, ResponseFormat> createGroup(Component component, User user, ComponentTypeEnum componentType, GroupDefinition groupDefinition, boolean inTransaction) {
1277 * Either<GroupDefinition, ResponseFormat> result = null;
1279 * log.trace("Going to create group {}" , groupDefinition);
1283 * // 3. verify group not already exist List<GroupDefinition> groups = component.getGroups(); boolean found = false; if (groups != null && false == groups.isEmpty()) {
1285 * GroupDefinition existGroupDef = groups.stream().filter(p -> p.getName().equalsIgnoreCase(groupDefinition.getName())).findFirst().orElse(null);
1287 * found = existGroupDef != null; }
1289 * if (true == found) { String componentTypeForResponse = getComponentTypeForResponse(component); result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_ALREADY_EXIST, groupDefinition.getName(), component.getNormalizedName(),
1290 * componentTypeForResponse)); return result; }
1292 * // 4. verify type of group exist String groupType = groupDefinition.getType(); if (groupType == null || groupType.isEmpty()) { result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_MISSING_GROUP_TYPE,
1293 * groupDefinition.getName())); return result; } Either<GroupTypeDefinition, StorageOperationStatus> getGroupType = groupTypeOperation.getLatestGroupTypeByType(groupType, true); if (getGroupType.isRight()) { StorageOperationStatus status =
1294 * getGroupType.right().value(); if (status == StorageOperationStatus.NOT_FOUND) { BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_GROUP, "group type " + groupType + " cannot be found", ErrorSeverity.INFO); result =
1295 * Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_TYPE_IS_INVALID, groupType)); return result; } else { result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); return result; } }
1297 * // 6. verify the component instances type are allowed according to // the member types in the group type GroupTypeDefinition groupTypeDefinition = getGroupType.left().value();
1299 * Either<Boolean, ResponseFormat> areValidMembers = verifyComponentInstancesAreValidMembers(component, componentType, groupDefinition.getName(), groupType, groupDefinition.getMembers(), groupTypeDefinition.getMembers());
1301 * if (areValidMembers.isRight()) { ResponseFormat responseFormat = areValidMembers.right().value(); result = Either.right(responseFormat); return result; }
1303 * // 7. verify the artifacts belongs to the component Either<Boolean, ResponseFormat> areValidArtifacts = verifyArtifactsBelongsToComponent(component, groupDefinition.getArtifacts(), CREATE_GROUP); if (areValidArtifacts.isRight()) {
1304 * ResponseFormat responseFormat = areValidArtifacts.right().value(); result = Either.right(responseFormat); return result; }
1306 * NodeTypeEnum nodeTypeEnum = componentType.getNodeType();
1308 * // add invariantUUID String invariantUUID = UniqueIdBuilder.buildInvariantUUID(); groupDefinition.setInvariantUUID(invariantUUID);
1310 * // add groupUUID String groupUUID = UniqueIdBuilder.generateUUID(); groupDefinition.setGroupUUID(groupUUID);
1312 * // add version groupDefinition.setVersion(INITIAL_VERSION);
1314 * // set groupType uid groupDefinition.setTypeUid(groupTypeDefinition.getUniqueId());
1316 * Either<GroupDefinition, StorageOperationStatus> addGroupToGraph = groupOperation.addGroup(nodeTypeEnum, component.getUniqueId(), groupDefinition, true);
1318 * if (addGroupToGraph.isRight()) { StorageOperationStatus storageOperationStatus = addGroupToGraph.right().value(); ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus); result =
1319 * Either.right(componentsUtils.getResponseFormat(actionStatus)); log.debug("Failed to create group {} under component {}, error: {}", groupDefinition.getName(), component.getNormalizedName(), actionStatus.name()); } else { GroupDefinition
1320 * groupDefinitionCreated = addGroupToGraph.left().value(); result = Either.left(groupDefinitionCreated); }
1326 * if (false == inTransaction) {
1328 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
1336 * public Either<List<GroupDefinition>, ResponseFormat> updateVfModuleGroupNames(String resourceSystemName, List<GroupDefinition> groups, boolean inTransaction) { List<GroupDefinition> updatedGroups = new ArrayList<>();
1337 * Either<List<GroupDefinition>, ResponseFormat> updateGroupNamesRes = Either.left(updatedGroups); Either<GroupDefinition, StorageOperationStatus> updateGroupNameRes; Either<String, ResponseFormat> validateGenerateGroupNameRes; int counter; for
1338 * (GroupDefinition group : groups) { if (!group.getType().equals(Constants.DEFAULT_GROUP_VF_MODULE) && !Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(group.getName()).matches()) { continue; } counter =
1339 * Integer.parseInt(group.getName().split(Constants.MODULE_NAME_DELIMITER)[1]); validateGenerateGroupNameRes = validateGenerateVfModuleGroupName(resourceSystemName, group.getDescription(), counter); if (validateGenerateGroupNameRes.isRight()) {
1340 * updateGroupNamesRes = Either.right(validateGenerateGroupNameRes.right().value()); break; } updateGroupNameRes = groupOperation.updateGroupName(group.getUniqueId(), validateGenerateGroupNameRes.left().value(), inTransaction); if
1341 * (updateGroupNameRes.isRight()) { ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(updateGroupNameRes.right().value()); updateGroupNamesRes = Either.right(componentsUtils.getResponseFormat(actionStatus)); break; }
1342 * updatedGroups.add(updateGroupNameRes.left().value()); } return updateGroupNamesRes; }
1345 private Either<List<GroupProperty>, ResponseFormat> updateGroupPropertiesValue(String componentId, GroupDefinition currentGroup, List<GroupProperty> groupPropertyToUpdate, boolean inTransaction) {
1346 Either<List<GroupProperty>, ResponseFormat> result;
1348 Either<List<GroupProperty>, StorageOperationStatus> eitherUpdate = groupsOperation.updateGroupPropertiesOnComponent(componentId, currentGroup, groupPropertyToUpdate);
1349 if (eitherUpdate.isRight()) {
1350 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(eitherUpdate.right().value());
1351 result = Either.right(componentsUtils.getResponseFormat(actionStatus));
1353 result = Either.left(eitherUpdate.left().value());
1358 public Either<Boolean, ResponseFormat> validateGenerateVfModuleGroupNames(List<ArtifactTemplateInfo> allGroups, String resourceSystemName, int startGroupCounter) {
1359 Either<Boolean, ResponseFormat> validateGenerateGroupNamesRes = Either.left(true);
1360 Collections.sort(allGroups, (art1, art2) -> ArtifactTemplateInfo.compareByGroupName(art1, art2));
1361 for (ArtifactTemplateInfo group : allGroups) {
1362 Either<String, ResponseFormat> validateGenerateGroupNameRes = validateGenerateVfModuleGroupName(resourceSystemName, group.getDescription(), startGroupCounter++);
1363 if (validateGenerateGroupNameRes.isRight()) {
1364 validateGenerateGroupNamesRes = Either.right(validateGenerateGroupNameRes.right().value());
1367 group.setGroupName(validateGenerateGroupNameRes.left().value());
1369 return validateGenerateGroupNamesRes;
1373 * Generate module name from resourceName, description and counter
1375 * @param resourceSystemName
1376 * @param description
1377 * @param groupCounter
1380 private Either<String, ResponseFormat> validateGenerateVfModuleGroupName(String resourceSystemName, String description, int groupCounter) {
1381 Either<String, ResponseFormat> validateGenerateGroupNameRes;
1382 if (resourceSystemName != null && description != null && Pattern.compile(Constants.MODULE_DESC_PATTERN).matcher(description).matches()) {
1383 final String fileName = description.replaceAll("\\.\\.", "\\.");
1384 validateGenerateGroupNameRes = Either.left(String.format(Constants.MODULE_NAME_FORMAT, resourceSystemName, FilenameUtils.removeExtension(fileName), groupCounter));
1386 validateGenerateGroupNameRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_VF_MODULE_NAME));
1388 return validateGenerateGroupNameRes;
1391 public Either<Map<String, GroupDefinition>, ResponseFormat> validateUpdateVfGroupNames(Map<String, GroupDefinition> groups, String resourceSystemName) {
1393 Map<String, GroupDefinition> updatedNamesGroups = new HashMap<>();
1394 Either<Map<String, GroupDefinition>, ResponseFormat> result = Either.left(updatedNamesGroups);
1395 for (Entry<String, GroupDefinition> groupEntry : groups.entrySet()) {
1396 GroupDefinition curGroup = groupEntry.getValue();
1397 String groupType = curGroup.getType();
1398 String groupName = groupEntry.getKey();
1401 Either<String, ResponseFormat> newGroupNameRes;
1402 if (groupType.equals(Constants.DEFAULT_GROUP_VF_MODULE) && !Pattern.compile(Constants.MODULE_NEW_NAME_PATTERN).matcher(groupName).matches()) {
1404 if (Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(groupEntry.getKey()).matches()) {
1405 counter = Integer.parseInt(groupEntry.getKey().split(Constants.MODULE_NAME_DELIMITER)[1]);
1406 description = curGroup.getDescription();
1408 counter = getNextVfModuleNameCounter(updatedNamesGroups);
1409 description = groupName;
1411 newGroupNameRes = validateGenerateVfModuleGroupName(resourceSystemName, description, counter);
1412 if (newGroupNameRes.isRight()) {
1413 log.debug("Failed to generate new vf module group name. Status is {} ", newGroupNameRes.right().value());
1414 result = Either.right(newGroupNameRes.right().value());
1417 groupName = newGroupNameRes.left().value();
1418 curGroup.setName(groupName);
1420 updatedNamesGroups.put(groupName, curGroup);
1425 public int getNextVfModuleNameCounter(Map<String, GroupDefinition> groups) {
1427 if (groups != null && !groups.isEmpty()) {
1428 counter = getNextVfModuleNameCounter(groups.values());
1433 public int getNextVfModuleNameCounter(Collection<GroupDefinition> groups) {
1435 if (groups != null && !groups.isEmpty()) {
1436 List<Integer> counters = groups.stream().filter(group -> Pattern.compile(Constants.MODULE_NEW_NAME_PATTERN).matcher(group.getName()).matches() || Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(group.getName()).matches())
1437 .map(group -> Integer.parseInt(group.getName().split(Constants.MODULE_NAME_DELIMITER)[1])).collect(Collectors.toList());
1438 counter = (counters == null || counters.isEmpty()) ? 0 : counters.stream().max((a, b) -> Integer.compare(a, b)).get() + 1;
1443 public Either<List<GroupDefinition>, ResponseFormat> validateUpdateVfGroupNamesOnGraph(List<GroupDefinition> groups, Component component, boolean inTransaction) {
1444 List<GroupDefinition> updatedGroups = new ArrayList<>();
1445 Either<List<GroupDefinition>, ResponseFormat> result = Either.left(updatedGroups);
1447 for (GroupDefinition group : groups) {
1448 String groupType = group.getType();
1449 String oldGroupName = group.getName();
1450 String newGroupName;
1451 Either<String, ResponseFormat> newGroupNameRes;
1452 Either<GroupDefinition, StorageOperationStatus> updateGroupNameRes;
1454 if (groupType.equals(Constants.DEFAULT_GROUP_VF_MODULE) && Pattern.compile(Constants.MODULE_OLD_NAME_PATTERN).matcher(oldGroupName).matches()) {
1455 counter = Integer.parseInt(group.getName().split(Constants.MODULE_NAME_DELIMITER)[1]);
1456 newGroupNameRes = validateGenerateVfModuleGroupName(component.getSystemName(), group.getDescription(), counter);
1457 if (newGroupNameRes.isRight()) {
1458 log.debug("Failed to generate new vf module group name. Status is {} ", newGroupNameRes.right().value());
1459 result = Either.right(newGroupNameRes.right().value());
1462 newGroupName = newGroupNameRes.left().value();
1463 group.setName(newGroupName);
1466 updatedGroups.add(group);
1470 result = Either.left(updatedGroups);
1475 * public Either<List<GroupDefinition>, ResponseFormat> createGroups(Component component, User user, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions, boolean inTransaction) {
1477 * List<GroupDefinition> generatedGroups = new ArrayList<>(); Either<List<GroupDefinition>, ResponseFormat> result = Either.left(generatedGroups);
1481 * if (groupDefinitions != null && false == groupDefinitions.isEmpty()) { for (GroupDefinition groupDefinition : groupDefinitions) { Either<GroupDefinition, ResponseFormat> createGroup = this.createGroup(component, user, componentType,
1482 * groupDefinition, true); if (createGroup.isRight()) { result = Either.right(createGroup.right().value()); return result; } GroupDefinition generatedGroup = createGroup.left().value(); generatedGroups.add(generatedGroup); } }
1484 * return result; } finally {
1486 * if (false == inTransaction) {
1488 * if (result == null || result.isRight()) { log.debug("Going to execute rollback on create group."); titanDao.rollback(); } else { log.debug("Going to execute commit on create group."); titanDao.commit(); }
1497 public Either<GroupDefinitionInfo, ResponseFormat> getGroupInstWithArtifactsById(ComponentTypeEnum componentType, String componentId, String componentInstanceId, String groupInstId, String userId, boolean inTransaction) {
1498 Either<GroupDefinitionInfo, ResponseFormat> result = null;
1500 // Validate user exist
1501 Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, UPDATE_GROUP, true);
1503 if (validateUserExists.isRight()) {
1504 result = Either.right(validateUserExists.right().value());
1508 // Validate component exist
1509 org.openecomp.sdc.be.model.Component component = null;
1510 String realComponentId = componentId;
1513 ComponentParametersView componentParametersView = new ComponentParametersView();
1514 componentParametersView.disableAll();
1515 componentParametersView.setIgnoreUsers(false);
1516 componentParametersView.setIgnoreComponentInstances(false);
1517 componentParametersView.setIgnoreArtifacts(false);
1519 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponent = validateComponentExists(realComponentId, componentType, componentParametersView);
1520 if (validateComponent.isRight()) {
1521 result = Either.right(validateComponent.right().value());
1524 component = validateComponent.left().value();
1525 Either<ImmutablePair<ComponentInstance, GroupInstance>, StorageOperationStatus> findComponentInstanceAndGroupInstanceRes = findComponentInstanceAndGroupInstanceOnComponent(component, componentInstanceId, groupInstId);
1527 if (findComponentInstanceAndGroupInstanceRes.isRight()) {
1528 log.debug("Failed to get group {} . Status is {} ", groupInstId, findComponentInstanceAndGroupInstanceRes.right().value());
1529 ResponseFormat responseFormat = componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(findComponentInstanceAndGroupInstanceRes.right().value()));
1530 result = Either.right(responseFormat);
1534 GroupInstance group = findComponentInstanceAndGroupInstanceRes.left().value().getRight();
1535 ComponentInstance componentInstance = findComponentInstanceAndGroupInstanceRes.left().value().getLeft();
1537 Boolean isBase = null;// Constants.IS_BASE;
1538 List<? extends GroupProperty> props = group.convertToGroupInstancesProperties();
1539 if (props != null && !props.isEmpty()) {
1540 Optional<? extends GroupProperty> isBasePropOp = props.stream().filter(p -> p.getName().equals(Constants.IS_BASE)).findAny();
1541 if (isBasePropOp.isPresent()) {
1542 GroupProperty propIsBase = isBasePropOp.get();
1543 isBase = Boolean.parseBoolean(propIsBase.getValue());
1546 BeEcompErrorManager.getInstance().logInvalidInputError(GET_GROUP, "failed to find prop isBase " + component.getNormalizedName(), ErrorSeverity.INFO);
1548 // Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
1553 List<ArtifactDefinitionInfo> artifacts = new ArrayList<>();
1554 List<String> artifactsIds = group.getArtifacts();
1555 if (artifactsIds != null && !artifactsIds.isEmpty()) {
1557 List<ComponentInstance> instances = component.getComponentInstances();
1558 if (instances != null) {
1559 Optional<ComponentInstance> findFirst = instances.stream().filter(i -> i.getUniqueId().equals(componentInstanceId)).findFirst();
1560 if (findFirst.isPresent()) {
1561 ComponentInstance ci = findFirst.get();
1562 Map<String, ArtifactDefinition> deploymentArtifacts = ci.getDeploymentArtifacts();
1563 for (String id : artifactsIds) {
1564 Optional<ArtifactDefinition> artOp = deploymentArtifacts.values().stream().filter(a -> a.getUniqueId().equals(id)).findFirst();
1565 if (artOp.isPresent()) {
1566 artifacts.add(new ArtifactDefinitionInfo(artOp.get()));
1569 List<String> instArtifactsIds = group.getGroupInstanceArtifacts();
1570 for (String id : instArtifactsIds) {
1571 Optional<ArtifactDefinition> artOp = deploymentArtifacts.values().stream().filter(a -> a.getUniqueId().equals(id)).findFirst();
1572 if (artOp.isPresent()) {
1573 artifacts.add(new ArtifactDefinitionInfo(artOp.get()));
1580 GroupDefinitionInfo resultInfo = new GroupDefinitionInfo(group);
1581 resultInfo.setIsBase(isBase);
1582 if (!artifacts.isEmpty())
1583 resultInfo.setArtifacts(artifacts);
1585 result = Either.left(resultInfo);
1591 if (false == inTransaction) {
1593 if (result == null || result.isRight()) {
1594 log.debug("Going to execute rollback on create group.");
1595 titanDao.rollback();
1597 log.debug("Going to execute commit on create group.");
1606 private Either<ImmutablePair<ComponentInstance, GroupInstance>, StorageOperationStatus> findComponentInstanceAndGroupInstanceOnComponent(Component component, String componentInstanceId, String groupInstId) {
1608 Either<ImmutablePair<ComponentInstance, GroupInstance>, StorageOperationStatus> result = null;
1609 if (CollectionUtils.isNotEmpty(component.getComponentInstances())) {
1610 Optional<GroupInstance> foundGroup;
1611 Optional<ComponentInstance> foundComponent = component.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstanceId)).findFirst();
1612 if (foundComponent.isPresent() && CollectionUtils.isNotEmpty(foundComponent.get().getGroupInstances())) {
1613 foundGroup = foundComponent.get().getGroupInstances().stream().filter(gi -> gi.getUniqueId().equals(groupInstId)).findFirst();
1614 if (foundGroup.isPresent()) {
1615 result = Either.left(new ImmutablePair<>(foundComponent.get(), foundGroup.get()));
1619 if (result == null) {
1620 result = Either.right(StorageOperationStatus.NOT_FOUND);
1625 private int getLatestIntProperty(Map<PropertyNames, String> newValues, Map<PropertyNames, String> parentValues, PropertyNames propertyKey) {
1627 if (newValues.containsKey(propertyKey)) {
1628 value = newValues.get(propertyKey);
1630 value = parentValues.get(propertyKey);
1632 return Integer.valueOf(value);
1635 private boolean isPropertyChanged(Map<PropertyNames, String> newValues, Map<PropertyNames, String> parentValues, final PropertyNames minInstances) {
1636 return newValues.containsKey(minInstances) && newValues.containsKey(minInstances) && !newValues.get(minInstances).equals(parentValues.get(minInstances));
1639 private Either<Boolean, ResponseFormat> validateMinMaxAndInitialCountPropertyLogicVF(Map<PropertyNames, String> newValues, Map<PropertyNames, String> parentValues) {
1641 int latestMaxInstances = getLatestIntProperty(newValues, parentValues, PropertyNames.MAX_INSTANCES);
1642 int latestInitialCount = getLatestIntProperty(newValues, parentValues, PropertyNames.INITIAL_COUNT);
1643 int latestMinInstances = getLatestIntProperty(newValues, parentValues, PropertyNames.MIN_INSTANCES);
1644 Either<Boolean, ResponseFormat> result = Either.left(true);
1646 if (isPropertyChanged(newValues, parentValues, PropertyNames.INITIAL_COUNT) && result.isLeft()) {
1647 if (latestInitialCount > latestMaxInstances || latestInitialCount < latestMinInstances) {
1648 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_GROUP_INITIAL_COUNT_PROPERTY_VALUE, PropertyNames.INITIAL_COUNT.getPropertyName(), String.valueOf(latestMinInstances), String.valueOf(latestMaxInstances)));
1651 if (isPropertyChanged(newValues, parentValues, PropertyNames.MAX_INSTANCES) && result.isLeft()) {
1652 if (latestMaxInstances < latestInitialCount) {
1653 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER, PropertyNames.MAX_INSTANCES.getPropertyName(), "higher", String.valueOf(latestInitialCount)));
1657 if (isPropertyChanged(newValues, parentValues, PropertyNames.MIN_INSTANCES) && result.isLeft()) {
1658 if (latestMinInstances > latestInitialCount) {
1659 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_GROUP_PROPERTY_VALUE_LOWER_HIGHER, PropertyNames.MIN_INSTANCES.getPropertyName(), "lower", String.valueOf(latestInitialCount)));
1666 private Either<Boolean, ResponseFormat> validateMinMaxAndInitialCountPropertyLogic(Map<PropertyNames, String> newValues, Map<PropertyNames, String> currValues, Map<PropertyNames, String> parentValues) {
1668 Either<Boolean, ResponseFormat> result;
1669 for (Entry<PropertyNames, String> entry : newValues.entrySet()) {
1670 PropertyNames currPropertyName = entry.getKey();
1671 if (currPropertyName == PropertyNames.MIN_INSTANCES) {
1672 String minValue = parentValues.get(PropertyNames.MIN_INSTANCES);
1673 String maxValue = newValues.containsKey(PropertyNames.INITIAL_COUNT) ? newValues.get(PropertyNames.MAX_INSTANCES) : currValues.get(PropertyNames.INITIAL_COUNT);
1674 result = validateValueInRange(new ImmutablePair<PropertyNames, String>(currPropertyName, entry.getValue()), new ImmutablePair<PropertyNames, String>(PropertyNames.MIN_INSTANCES, minValue),
1675 new ImmutablePair<PropertyNames, String>(PropertyNames.MAX_INSTANCES, maxValue));
1676 if (result.isRight()) {
1679 } else if (currPropertyName == PropertyNames.INITIAL_COUNT) {
1680 String minValue = newValues.containsKey(PropertyNames.MIN_INSTANCES) ? newValues.get(PropertyNames.MIN_INSTANCES) : currValues.get(PropertyNames.MIN_INSTANCES);
1681 String maxValue = newValues.containsKey(PropertyNames.MAX_INSTANCES) ? newValues.get(PropertyNames.MAX_INSTANCES) : currValues.get(PropertyNames.MAX_INSTANCES);
1682 result = validateValueInRange(new ImmutablePair<PropertyNames, String>(currPropertyName, entry.getValue()), new ImmutablePair<PropertyNames, String>(PropertyNames.MIN_INSTANCES, minValue),
1683 new ImmutablePair<PropertyNames, String>(PropertyNames.MAX_INSTANCES, maxValue));
1684 if (result.isRight()) {
1687 } else if (currPropertyName == PropertyNames.MAX_INSTANCES) {
1688 String minValue = newValues.containsKey(PropertyNames.INITIAL_COUNT) ? newValues.get(PropertyNames.MIN_INSTANCES) : currValues.get(PropertyNames.INITIAL_COUNT);
1689 String maxValue = parentValues.get(PropertyNames.MAX_INSTANCES);
1690 result = validateValueInRange(new ImmutablePair<PropertyNames, String>(currPropertyName, entry.getValue()), new ImmutablePair<PropertyNames, String>(PropertyNames.MIN_INSTANCES, minValue),
1691 new ImmutablePair<PropertyNames, String>(PropertyNames.MAX_INSTANCES, maxValue));
1692 if (result.isRight()) {
1697 return Either.left(true);
1700 private Either<Boolean, ResponseFormat> validateValueInRange(ImmutablePair<PropertyNames, String> newValue, ImmutablePair<PropertyNames, String> min, ImmutablePair<PropertyNames, String> max) {
1701 Either<Boolean, ResponseFormat> result;
1702 final String warnMessage = "Failed to validate {} as property value of {}. It must be not higher than {}, and not lower than {}.";
1703 int newValueInt = parseIntValue(newValue.getValue(), newValue.getKey());
1704 int minInt = parseIntValue(min.getValue(), min.getKey());
1705 int maxInt = parseIntValue(max.getValue(), max.getKey());
1706 if (newValueInt < 0 || minInt < 0 || maxInt < 0) {
1707 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY));
1708 } else if (newValueInt < minInt || newValueInt > maxInt) {
1709 log.debug(warnMessage, newValue.getValue(), newValue.getKey().getPropertyName(), min.getValue(), max.getValue());
1711 .right(componentsUtils.getResponseFormat(ActionStatus.INVALID_GROUP_MIN_MAX_INSTANCES_PROPERTY_VALUE, newValue.getKey().getPropertyName(), maxInt == Integer.MAX_VALUE ? Constants.UNBOUNDED : max.getValue(), min.getValue()));
1713 result = Either.left(true);
1718 private int parseIntValue(String value, PropertyNames propertyName) {
1720 if (propertyName == PropertyNames.MAX_INSTANCES) {
1721 result = convertIfUnboundMax(value);
1722 } else if (NumberUtils.isNumber(value)) {
1723 result = Integer.parseInt(value);
1731 * validates received new property values and updates group instance in case of success
1733 * @param oldGroupInstance
1734 * @param groupInstanceId
1735 * @param newProperties
1736 * @param inTransaction
1739 public Either<GroupInstance, ResponseFormat> validateAndUpdateGroupInstancePropertyValues(String componentId, String instanceId, GroupInstance oldGroupInstance, List<GroupInstanceProperty> newProperties, boolean inTransaction) {
1741 Either<GroupInstance, ResponseFormat> actionResult = null;
1742 Either<GroupInstance, StorageOperationStatus> updateGroupInstanceResult = null;
1743 Either<List<GroupInstanceProperty>, ResponseFormat> validateRes = validateReduceGroupInstancePropertiesBeforeUpdate(oldGroupInstance, newProperties);
1744 if (validateRes.isRight()) {
1745 log.debug("Failed to validate group instance {} properties before update. ", oldGroupInstance.getName());
1746 actionResult = Either.right(validateRes.right().value());
1748 if (actionResult == null) {
1749 List<GroupInstanceProperty> validatedReducedNewProperties = validateRes.left().value();
1750 updateGroupInstanceResult = groupsOperation.updateGroupInstancePropertyValuesOnGraph(componentId, instanceId, oldGroupInstance, validatedReducedNewProperties);
1751 if (updateGroupInstanceResult.isRight()) {
1752 log.debug("Failed to update group instance {} property values. ", oldGroupInstance.getName());
1753 actionResult = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateGroupInstanceResult.right().value())));
1756 if (actionResult == null) {
1757 actionResult = Either.left(updateGroupInstanceResult.left().value());
1759 return actionResult;
1762 private Either<List<GroupInstanceProperty>, ResponseFormat> validateReduceGroupInstancePropertiesBeforeUpdate(GroupInstance oldGroupInstance, List<GroupInstanceProperty> newProperties) {
1764 Either<Boolean, ResponseFormat> validationRes = null;
1765 Either<List<GroupInstanceProperty>, ResponseFormat> actionResult;
1766 Map<String, GroupInstanceProperty> existingProperties = oldGroupInstance.convertToGroupInstancesProperties().stream().collect(Collectors.toMap(p -> p.getName(), p -> p));
1767 Map<PropertyNames, String> newPropertyValues = new EnumMap<>(PropertyNames.class);
1768 List<GroupInstanceProperty> reducedProperties = new ArrayList<>();
1769 String currPropertyName;
1771 for (GroupInstanceProperty currNewProperty : newProperties) {
1772 currPropertyName = currNewProperty.getName();
1773 validationRes = handleAndAddProperty(reducedProperties, newPropertyValues, currNewProperty, existingProperties.get(currPropertyName));
1774 if (validationRes.isRight()) {
1775 log.debug("Failed to handle property {} of group instance {}. ", currPropertyName, oldGroupInstance.getName());
1779 if (validationRes == null || validationRes.isLeft()) {
1780 Map<PropertyNames, String> existingPropertyValues = new EnumMap<>(PropertyNames.class);
1781 Map<PropertyNames, String> parentPropertyValues = new EnumMap<>(PropertyNames.class);
1782 fillValuesAndParentValuesFromExistingProperties(existingProperties, existingPropertyValues, parentPropertyValues);
1783 validationRes = validateMinMaxAndInitialCountPropertyLogic(newPropertyValues, existingPropertyValues, parentPropertyValues);
1785 if (validationRes.isLeft()) {
1786 actionResult = Either.left(reducedProperties);
1788 actionResult = Either.right(validationRes.right().value());
1790 } catch (Exception e) {
1791 log.error("Exception occured during validation and reducing group instance properties. The message is {}", e.getMessage(), e);
1792 actionResult = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1794 return actionResult;
1797 private void fillValuesAndParentValuesFromExistingProperties(Map<String, GroupInstanceProperty> existingProperties, Map<PropertyNames, String> propertyValues, Map<PropertyNames, String> parentPropertyValues) {
1798 PropertyNames[] allPropertyNames = PropertyNames.values();
1799 for (PropertyNames name : allPropertyNames) {
1800 if (isUpdatable(name)) {
1801 propertyValues.put(name, String.valueOf(existingProperties.get(name.getPropertyName()).getValue()));
1802 parentPropertyValues.put(name, String.valueOf(existingProperties.get(name.getPropertyName()).getParentValue()));
1807 private Either<Boolean, ResponseFormat> handleAndAddProperty(List<GroupInstanceProperty> reducedProperties, Map<PropertyNames, String> newPropertyValues, GroupInstanceProperty currNewProperty, GroupInstanceProperty currExistingProperty) {
1809 Either<Boolean, ResponseFormat> validationRes = null;
1810 String currPropertyName = currNewProperty.getName();
1811 PropertyNames propertyName = PropertyNames.findName(currPropertyName);
1813 if (currExistingProperty == null) {
1814 log.warn("The value of property with the name {} cannot be updated. The property not found on group instance. ", currPropertyName);
1815 } else if (isUpdatable(propertyName)) {
1816 validationRes = validateAndUpdatePropertyValue(currNewProperty, currExistingProperty);
1817 if (validationRes.isRight()) {
1818 log.debug("Failed to validate property value {} of property {}. ", currNewProperty.getValue(), currPropertyName);
1820 addPropertyUpdatedValues(reducedProperties, propertyName, newPropertyValues, currNewProperty, currExistingProperty);
1823 validateImmutableProperty(currExistingProperty, currNewProperty);
1825 if (validationRes == null) {
1826 validationRes = Either.left(true);
1828 } catch (Exception e) {
1829 log.error("Exception occured during handle and adding property. The message is {}", e.getMessage(), e);
1830 validationRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1832 return validationRes;
1835 private boolean isUpdatable(PropertyNames updatablePropertyName) {
1836 return updatablePropertyName != null && updatablePropertyName.getUpdateBehavior().getLevelNumber() >= GroupInstancePropertyValueUpdateBehavior.UPDATABLE_ON_SERVICE_LEVEL.getLevelNumber();
1839 private void addPropertyUpdatedValues(List<GroupInstanceProperty> reducedProperties, PropertyNames propertyName, Map<PropertyNames, String> newPropertyValues, GroupInstanceProperty newProperty, GroupInstanceProperty existingProperty) {
1841 String newValue = newProperty.getValue();
1842 if (!newValue.equals(String.valueOf(existingProperty.getValue()))) {
1843 newProperty.setValueUniqueUid(existingProperty.getValueUniqueUid());
1844 reducedProperties.add(newProperty);
1846 if (!isEmptyMinInitialCountValue(propertyName, newValue)) {
1847 newPropertyValues.put(propertyName, newValue);
1851 private boolean isEmptyMinInitialCountValue(PropertyNames propertyName, String newValue) {
1852 boolean result = false;
1853 if ((propertyName == PropertyNames.MIN_INSTANCES || propertyName == PropertyNames.INITIAL_COUNT) && !NumberUtils.isNumber(newValue)) {
1859 private int convertIfUnboundMax(String value) {
1862 if (!NumberUtils.isNumber(value)) {
1863 result = Integer.MAX_VALUE;
1865 result = Integer.parseInt(value);
1870 private Either<Boolean, ResponseFormat> validateAndUpdatePropertyValue(GroupInstanceProperty newProperty, GroupInstanceProperty existingProperty) {
1872 Either<Boolean, ResponseFormat> validationRes = null;
1873 String parentValue = existingProperty.getParentValue();
1875 newProperty.setParentValue(parentValue);
1876 if (StringUtils.isEmpty(newProperty.getValue())) {
1877 newProperty.setValue(parentValue);
1879 if (StringUtils.isEmpty(existingProperty.getValue())) {
1880 existingProperty.setValue(parentValue);
1882 StorageOperationStatus status = groupOperation.validateAndUpdatePropertyValue(newProperty);
1883 if (status != StorageOperationStatus.OK) {
1884 log.debug("Failed to validate property value {} of property with name {}. Status is {}. ", newProperty.getValue(), newProperty.getName(), status);
1885 validationRes = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(status)));
1887 if (validationRes == null) {
1888 validationRes = Either.left(true);
1890 return validationRes;
1893 private void validateImmutableProperty(GroupProperty oldProperty, GroupProperty newProperty) {
1894 if (oldProperty.getValue() == null && newProperty.getValue() != null || oldProperty.getValue() != null && !oldProperty.getValue().equals(newProperty.getValue())) {
1895 log.warn("The value of property with the name {} cannot be updated on service level. Going to ignore new property value {}. ", oldProperty.getName(), newProperty.getValue());
1899 public Either<List<GroupDefinition>, ResponseFormat> createGroups(Component component, User user, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions) {
1901 Map<String, GroupDataDefinition> groups = new HashMap<>();
1902 // Map<String, MapPropertiesDataDefinition> groupsPropertiesMap = new HashMap<>();
1903 Either<List<GroupDefinition>, ResponseFormat> result = null;
1904 Either<List<GroupDefinition>, StorageOperationStatus> createGroupsResult = null;
1905 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = dataTypeCache.getAll();
1906 if (allDataTypes.isRight()) {
1907 TitanOperationStatus status = allDataTypes.right().value();
1908 BeEcompErrorManager.getInstance().logInternalFlowError("AddPropertyToGroup", "Failed to add property to group. Status is " + status, ErrorSeverity.ERROR);
1909 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
1913 // handle groups and convert to tosca data
1914 if (groupDefinitions != null && !groupDefinitions.isEmpty()) {
1915 for (GroupDefinition groupDefinition : groupDefinitions) {
1916 Either<GroupDefinition, ResponseFormat> handleGroupRes = handleGroup(component, user, componentType, groupDefinition, allDataTypes.left().value());
1917 if (handleGroupRes.isRight()) {
1918 result = Either.right(handleGroupRes.right().value());
1921 GroupDefinition handledGroup = handleGroupRes.left().value();
1922 groups.put(handledGroup.getName(), new GroupDataDefinition(handledGroup));
1926 if (result == null) {
1927 createGroupsResult = groupsOperation.createGroups(component, user, componentType, groups);
1928 if (createGroupsResult.isRight()) {
1929 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(createGroupsResult.right().value())));
1932 if (result == null) {
1933 result = Either.left(createGroupsResult.left().value());
1938 public Either<List<GroupDefinition>, ResponseFormat> addGroups(Component component, User user, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions) {
1940 Either<List<GroupDefinition>, ResponseFormat> result = null;
1941 Either<List<GroupDefinition>, StorageOperationStatus> createGroupsResult = null;
1942 List<GroupDataDefinition> groups = new ArrayList<>();
1944 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = dataTypeCache.getAll();
1945 if (allDataTypes.isRight()) {
1946 TitanOperationStatus status = allDataTypes.right().value();
1947 BeEcompErrorManager.getInstance().logInternalFlowError("AddPropertyToGroup", "Failed to add property to group. Status is " + status, ErrorSeverity.ERROR);
1948 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
1952 // handle groups and convert to tosca data
1953 if (groupDefinitions != null && !groupDefinitions.isEmpty()) {
1954 for (GroupDefinition groupDefinition : groupDefinitions) {
1955 Either<GroupDefinition, ResponseFormat> handleGroupRes = handleGroup(component, user, componentType, groupDefinition, allDataTypes.left().value());
1956 if (handleGroupRes.isRight()) {
1957 result = Either.right(handleGroupRes.right().value());
1960 GroupDefinition handledGroup = handleGroupRes.left().value();
1961 groups.add(new GroupDataDefinition(handledGroup));
1964 if (result == null) {
1965 createGroupsResult = groupsOperation.addGroups(component, user, componentType, groups);
1966 if (createGroupsResult.isRight()) {
1967 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(createGroupsResult.right().value())));
1970 if (result == null) {
1971 result = Either.left(createGroupsResult.left().value());
1976 public Either<List<GroupDefinition>, ResponseFormat> deleteGroups(Component component, User user, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions) {
1978 Either<List<GroupDefinition>, ResponseFormat> result = null;
1979 Either<List<GroupDefinition>, StorageOperationStatus> createGroupsResult = null;
1981 createGroupsResult = groupsOperation.deleteGroups(component, user, componentType, groupDefinitions.stream().map(x -> new GroupDataDefinition(x)).collect(Collectors.toList()));
1982 if (createGroupsResult.isRight()) {
1983 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(createGroupsResult.right().value())));
1986 if (result == null) {
1987 result = Either.left(createGroupsResult.left().value());
1993 * Update specific group version
1995 * @param groupDefinition
1996 * @param inTransaction
1999 public Either<List<GroupDefinition>, ResponseFormat> updateGroups(Component component, ComponentTypeEnum componentType, List<GroupDefinition> groupDefinitions) {
2001 Either<List<GroupDefinition>, ResponseFormat> result = null;
2002 Either<List<GroupDefinition>, StorageOperationStatus> createGroupsResult = null;
2004 createGroupsResult = groupsOperation.updateGroups(component, componentType, groupDefinitions.stream().map(x -> new GroupDataDefinition(x)).collect(Collectors.toList()));
2005 if (createGroupsResult.isRight()) {
2006 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(createGroupsResult.right().value())));
2009 if (result == null) {
2010 result = Either.left(createGroupsResult.left().value());
2015 public Either<GroupDefinition, ResponseFormat> handleGroup(Component component, User user, ComponentTypeEnum componentType, GroupDefinition groupDefinition, Map<String, DataTypeDefinition> allDAtaTypes) {
2017 Either<GroupDefinition, ResponseFormat> result = null;
2019 log.trace("Going to create group {}", groupDefinition);
2020 // 3. verify group not already exist
2021 List<GroupDefinition> groups = component.getGroups();
2022 boolean found = false;
2023 if (groups != null && false == groups.isEmpty()) {
2025 GroupDefinition existGroupDef = groups.stream().filter(p -> p.getName().equalsIgnoreCase(groupDefinition.getName())).findFirst().orElse(null);
2027 found = existGroupDef != null;
2029 if (true == found) {
2030 String componentTypeForResponse = getComponentTypeForResponse(component);
2031 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_ALREADY_EXIST, groupDefinition.getName(), component.getNormalizedName(), componentTypeForResponse));
2034 // 4. verify type of group exist
2035 String groupType = groupDefinition.getType();
2036 if (groupType == null || groupType.isEmpty()) {
2037 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupDefinition.getName()));
2040 Either<GroupTypeDefinition, StorageOperationStatus> getGroupType = groupTypeOperation.getLatestGroupTypeByType(groupType, true);
2041 if (getGroupType.isRight()) {
2042 StorageOperationStatus status = getGroupType.right().value();
2043 if (status == StorageOperationStatus.NOT_FOUND) {
2044 BeEcompErrorManager.getInstance().logInvalidInputError(CREATE_GROUP, "group type " + groupType + " cannot be found", ErrorSeverity.INFO);
2045 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GROUP_TYPE_IS_INVALID, groupType));
2048 result = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
2052 // 6. verify the component instances type are allowed according to
2053 // the member types in the group type
2054 GroupTypeDefinition groupTypeDefinition = getGroupType.left().value();
2056 Either<Boolean, ResponseFormat> areValidMembers = verifyComponentInstancesAreValidMembers(component, componentType, groupDefinition.getName(), groupType, groupDefinition.getMembers(), groupTypeDefinition.getMembers());
2058 if (areValidMembers.isRight()) {
2059 ResponseFormat responseFormat = areValidMembers.right().value();
2060 result = Either.right(responseFormat);
2063 // 7. verify the artifacts belongs to the component
2064 Either<Boolean, ResponseFormat> areValidArtifacts = verifyArtifactsBelongsToComponent(component, groupDefinition.getArtifacts(), CREATE_GROUP);
2065 if (areValidArtifacts.isRight()) {
2066 ResponseFormat responseFormat = areValidArtifacts.right().value();
2067 result = Either.right(responseFormat);
2070 List<PropertyDefinition> groupTypeProperties = groupTypeDefinition.getProperties();
2072 List<GroupProperty> properties = groupDefinition.convertToGroupProperties();
2073 List<GroupProperty> updatedGroupTypeProperties = new ArrayList<>();
2074 if (properties != null && false == properties.isEmpty()) {
2076 if (groupTypeProperties == null || true == groupTypeProperties.isEmpty()) {
2077 BeEcompErrorManager.getInstance().logInvalidInputError(ADDING_GROUP, "group type does not have properties", ErrorSeverity.INFO);
2078 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.MATCH_NOT_FOUND))));
2081 Map<String, PropertyDefinition> groupTypePropertiesMap = groupTypeProperties.stream().collect(Collectors.toMap(p -> p.getName(), p -> p));
2083 Either<GroupProperty, TitanOperationStatus> addPropertyResult = null;
2085 for (GroupProperty prop : properties) {
2086 addPropertyResult = handleProperty(prop, groupTypePropertiesMap.get(prop.getName()), i, allDAtaTypes);
2087 if (addPropertyResult.isRight()) {
2088 BeEcompErrorManager.getInstance().logInvalidInputError(ADDING_GROUP, "failed to validate property", ErrorSeverity.INFO);
2089 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(addPropertyResult.right().value()))));
2091 updatedGroupTypeProperties.add(addPropertyResult.left().value());
2096 if (groupDefinition.getUniqueId() == null) {
2097 String uid = UniqueIdBuilder.buildGroupingUid(component.getUniqueId(), groupDefinition.getName());
2098 groupDefinition.setUniqueId(uid);
2100 groupDefinition.convertFromGroupProperties(updatedGroupTypeProperties);
2101 groupDefinition.setInvariantUUID(UniqueIdBuilder.buildInvariantUUID());
2102 groupDefinition.setGroupUUID(UniqueIdBuilder.generateUUID());
2103 groupDefinition.setVersion(INITIAL_VERSION);
2104 groupDefinition.setTypeUid(groupTypeDefinition.getUniqueId());
2106 return Either.left(groupDefinition);
2109 public Either<GroupProperty, TitanOperationStatus> handleProperty(GroupProperty groupProperty, PropertyDefinition prop, Integer index, Map<String, DataTypeDefinition> allDataTypes) {
2112 return Either.right(TitanOperationStatus.ILLEGAL_ARGUMENT);
2115 PropertyDataDefinition propDataDef = prop;
2116 String propertyType = propDataDef.getType();
2117 String value = groupProperty.getValue();
2119 Either<String, TitanOperationStatus> checkInnerType = propertyOperation.checkInnerType(propDataDef);
2120 if (checkInnerType.isRight()) {
2121 TitanOperationStatus status = checkInnerType.right().value();
2122 return Either.right(status);
2125 String innerType = checkInnerType.left().value();
2127 log.debug("Before validateAndUpdatePropertyValue");
2128 Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, value, innerType, allDataTypes);
2129 log.debug("After validateAndUpdatePropertyValue. isValid = {}", isValid);
2131 String newValue = value;
2132 if (isValid.isRight()) {
2133 Boolean res = isValid.right().value();
2135 return Either.right(TitanOperationStatus.ILLEGAL_ARGUMENT);
2138 Object object = isValid.left().value();
2139 if (object != null) {
2140 newValue = object.toString();
2144 String uniqueId = UniqueIdBuilder.buildGroupPropertyValueUid((String) prop.getUniqueId(), index);
2146 groupProperty.setUniqueId(uniqueId);
2147 groupProperty.setValue(newValue);
2148 groupProperty.setType(prop.getType());
2149 groupProperty.setDefaultValue(prop.getDefaultValue());
2150 groupProperty.setDescription(prop.getDescription());
2151 groupProperty.setSchema(prop.getSchema());
2152 groupProperty.setPassword(prop.isPassword());
2153 groupProperty.setParentUniqueId(prop.getUniqueId());
2155 log.debug("Before adding property value to graph {}", groupProperty);
2157 return Either.left(groupProperty);