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.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.Objects;
30 import java.util.Optional;
31 import java.util.UUID;
32 import java.util.stream.Collectors;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.apache.commons.collections.MapUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.commons.lang3.tuple.ImmutablePair;
38 import org.openecomp.sdc.be.config.BeEcompErrorManager;
39 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
42 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
43 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
44 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
45 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
46 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
47 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
49 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
50 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
51 import org.openecomp.sdc.be.model.ArtifactDefinition;
52 import org.openecomp.sdc.be.model.Component;
53 import org.openecomp.sdc.be.model.ComponentInstance;
54 import org.openecomp.sdc.be.model.ComponentInstanceInput;
55 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
56 import org.openecomp.sdc.be.model.ComponentParametersView;
57 import org.openecomp.sdc.be.model.DataTypeDefinition;
58 import org.openecomp.sdc.be.model.GroupDefinition;
59 import org.openecomp.sdc.be.model.GroupInstance;
60 import org.openecomp.sdc.be.model.LifecycleStateEnum;
61 import org.openecomp.sdc.be.model.PropertyDefinition.PropertyNames;
62 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
63 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
66 import org.openecomp.sdc.be.model.operations.api.IComponentInstanceOperation;
67 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
68 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
69 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
70 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
71 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
72 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
73 import org.openecomp.sdc.be.resources.data.PropertyValueData;
74 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
75 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
76 import org.openecomp.sdc.common.api.Constants;
77 import org.openecomp.sdc.common.datastructure.Wrapper;
78 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
79 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
80 import org.openecomp.sdc.common.util.ValidationUtils;
81 import org.openecomp.sdc.exception.ResponseFormat;
82 import org.slf4j.Logger;
83 import org.slf4j.LoggerFactory;
84 import org.springframework.beans.factory.annotation.Autowired;
86 import fj.data.Either;
88 public abstract class ComponentInstanceBusinessLogic extends BaseBusinessLogic {
90 private static Logger log = LoggerFactory.getLogger(ComponentInstanceBusinessLogic.class.getName());
93 private IComponentInstanceOperation componentInstanceOperation;
96 private ArtifactsBusinessLogic artifactBusinessLogic;
98 private ApplicationDataTypeCache dataTypeCache;
100 public static final String VF_MODULE = "org.openecomp.groups.VfModule";
102 public ComponentInstanceBusinessLogic() {
105 public Either<ComponentInstance, ResponseFormat> createComponentInstance(String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance) {
106 return createComponentInstance(containerComponentParam, containerComponentId, userId, resourceInstance, false, true);
109 public Either<ComponentInstance, ResponseFormat> createComponentInstance(String containerComponentParam, String containerComponentId, String userId, ComponentInstance resourceInstance, boolean inTransaction, boolean needLock) {
111 Component origComponent = null;
112 Either<ComponentInstance, ResponseFormat> resultOp = null;
114 org.openecomp.sdc.be.model.Component containerComponent = null;
115 ComponentTypeEnum containerComponentType;
118 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Component Instance", inTransaction);
119 if (resp.isRight()) {
120 return Either.right(resp.right().value());
122 user = resp.left().value();
125 Either<Boolean, ResponseFormat> validateValidJson = validateJsonBody(resourceInstance, ComponentInstance.class);
126 if (validateValidJson.isRight()) {
127 return Either.right(validateValidJson.right().value());
130 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
131 if (validateComponentType.isRight()) {
132 return Either.right(validateComponentType.right().value());
134 containerComponentType = validateComponentType.left().value();
137 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
138 if (validateComponentExists.isRight()) {
139 return Either.right(validateComponentExists.right().value());
141 containerComponent = validateComponentExists.left().value();
144 Either<Boolean, ResponseFormat> validateAllowedToContainCompInstances = validateAllowedToContainCompInstances(containerComponent);
145 if (validateAllowedToContainCompInstances.isRight()) {
146 return Either.right(validateAllowedToContainCompInstances.right().value());
149 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
150 if (validateCanWorkOnComponent.isRight()) {
151 return Either.right(validateCanWorkOnComponent.right().value());
154 if (resourceInstance != null && containerComponentType != null) {
155 Either<Component, ResponseFormat> getOriginComponentRes = getAndValidateOriginComponentOfComponentInstance(containerComponentType, resourceInstance);
156 if (getOriginComponentRes.isRight()) {
157 return Either.right(getOriginComponentRes.right().value());
159 origComponent = getOriginComponentRes.left().value();
163 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "createComponentInstance");
164 if (lockComponent.isRight()) {
165 return Either.right(lockComponent.right().value());
168 log.debug("Try to create entry on graph");
169 resultOp = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
174 unlockComponent(resultOp, containerComponent);
178 public Either<CreateAndAssotiateInfo, ResponseFormat> createAndAssociateRIToRI(String containerComponentParam, String containerComponentId, String userId, CreateAndAssotiateInfo createAndAssotiateInfo) {
180 Either<CreateAndAssotiateInfo, ResponseFormat> resultOp = null;
181 ComponentInstance resourceInstance = createAndAssotiateInfo.getNode();
182 RequirementCapabilityRelDef associationInfo = createAndAssotiateInfo.getAssociate();
184 Either<User, ResponseFormat> resp = validateUserExists(userId, "create And Associate RI To RI", false);
185 if (resp.isRight()) {
186 return Either.right(resp.right().value());
189 User user = resp.left().value();
190 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
191 if (validateComponentType.isRight()) {
192 return Either.right(validateComponentType.right().value());
195 final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
197 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
198 if (validateComponentExists.isRight()) {
199 return Either.right(validateComponentExists.right().value());
201 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
203 Either<Boolean, ResponseFormat> validateAllowedToContainCompInstances = validateAllowedToContainCompInstances(containerComponent);
204 if (validateAllowedToContainCompInstances.isRight()) {
205 return Either.right(validateAllowedToContainCompInstances.right().value());
208 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
209 if (validateCanWorkOnComponent.isRight()) {
210 return Either.right(validateCanWorkOnComponent.right().value());
213 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "createAndAssociateRIToRI");
214 if (lockComponent.isRight()) {
215 return Either.right(lockComponent.right().value());
219 log.debug("Try to create entry on graph");
220 Either<Component, ResponseFormat> eitherResourceName = getOriginComponentNameFromComponentInstance(resourceInstance, true);
222 if (eitherResourceName.isRight()) {
223 resultOp = Either.right(eitherResourceName.right().value());
226 Component origComponent = eitherResourceName.left().value();
228 Either<ComponentInstance, ResponseFormat> result = createComponentInstanceOnGraph(containerComponent, origComponent, resourceInstance, user);
229 if (result.isRight()) {
230 log.debug("Failed to create resource instance {}", containerComponentId);
231 resultOp = Either.right(result.right().value());
236 log.debug("Entity on graph is created.");
237 ComponentInstance resResourceInfo = result.left().value();
238 if (associationInfo.getFromNode() == null || associationInfo.getFromNode().isEmpty()) {
239 associationInfo.setFromNode(resResourceInfo.getUniqueId());
241 associationInfo.setToNode(resResourceInfo.getUniqueId());
244 RequirementCapabilityRelDef requirementCapabilityRelDef = associationInfo;// createRequirementCapabilityrelDef(associationInfo);
245 Either<RequirementCapabilityRelDef, StorageOperationStatus> resultReqCapDef = toscaOperationFacade.associateResourceInstances(containerComponentId, requirementCapabilityRelDef);
246 if (resultReqCapDef.isLeft()) {
247 log.debug("Enty on graph is created.");
248 RequirementCapabilityRelDef resReqCapabilityRelDef = resultReqCapDef.left().value();
249 CreateAndAssotiateInfo resInfo = new CreateAndAssotiateInfo(resResourceInfo, resReqCapabilityRelDef);
250 resultOp = Either.left(resInfo);
254 log.info("Failed to associate node {} with node {}", associationInfo.getFromNode(), associationInfo.getToNode());
255 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(resultReqCapDef.right().value(), true), "", null));
260 unlockComponent(resultOp, containerComponent);
264 private Either<Component, ResponseFormat> getOriginComponentNameFromComponentInstance(ComponentInstance componentInstance, boolean inTransaction) {
265 Either<Component, ResponseFormat> eitherResponse;
266 Either<Component, StorageOperationStatus> eitherComponent = toscaOperationFacade.getToscaFullElement(componentInstance.getComponentUid());
267 if (eitherComponent.isRight()) {
268 log.debug("Failed to get origin component with id {} for component instance {} ", componentInstance.getComponentUid(), componentInstance.getName());
269 eitherResponse = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponse(eitherComponent.right().value(), ComponentTypeEnum.RESOURCE), "", null));
271 eitherResponse = Either.left(eitherComponent.left().value());
273 return eitherResponse;
276 private Either<ComponentInstance, ResponseFormat> createComponentInstanceOnGraph(org.openecomp.sdc.be.model.Component containerComponent, Component originComponent, ComponentInstance componentInstance, User user) {
277 Either<ComponentInstance, ResponseFormat> resultOp;
279 Either<ImmutablePair<Component, String>, StorageOperationStatus> result = toscaOperationFacade.addComponentInstanceToTopologyTemplate(containerComponent, originComponent, componentInstance, false, user);
281 if (result.isRight()) {
282 log.debug("Failed to create entry on graph for component instance {}", componentInstance.getName());
283 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), "", null));
287 log.debug("Entity on graph is created.");
288 Component updatedComponent = result.left().value().getLeft();
289 Map<String, String> existingEnvVersions = new HashMap<>();
290 //TODO existingEnvVersions ??
291 Either<ActionStatus, ResponseFormat> addComponentInstanceArtifacts = addComponentInstanceArtifacts(updatedComponent, componentInstance, originComponent, user, existingEnvVersions);
292 if (addComponentInstanceArtifacts.isRight()) {
293 log.debug("Failed to create component instance {}", componentInstance.getName());
294 resultOp = Either.right(addComponentInstanceArtifacts.right().value());
298 Optional<ComponentInstance> updatedInstanceOptional = updatedComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(result.left().value().getRight())).findFirst();
299 if (!updatedInstanceOptional.isPresent()) {
300 log.debug("Failed to fetch new added component instance {} from component {}", componentInstance.getName(), containerComponent.getName());
301 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
304 resultOp = Either.left(updatedInstanceOptional.get());
309 * addResourceInstanceArtifacts - add artifacts (HEAT_ENV) to resource instance The instance artifacts are generated from the resource's artifacts
311 * @param componentInstance
313 * @param existingEnvVersions
315 * @param containerComponentId
319 protected Either<ActionStatus, ResponseFormat> addComponentInstanceArtifacts(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance componentInstance, org.openecomp.sdc.be.model.Component originComponent, User user,
320 Map<String, String> existingEnvVersions) {
321 log.debug("add artifacts to resource instance");
322 List<GroupDefinition> filteredGroups = null;
323 ActionStatus status = setResourceArtifactsOnResourceInstance(componentInstance);
324 if (!ActionStatus.OK.equals(status)) {
325 ResponseFormat resultOp = componentsUtils.getResponseFormatForResourceInstance(status, "", null);
326 return Either.right(resultOp);
329 // generate heat_env if necessary
330 Map<String, ArtifactDefinition> componentDeploymentArtifacts = componentInstance.getDeploymentArtifacts();
331 if (componentDeploymentArtifacts == null) {
332 return Either.left(ActionStatus.OK);
334 Map<String, ArtifactDefinition> finalDeploymentArtifacts = new HashMap<String, ArtifactDefinition>();
336 Map<String, List<ArtifactDefinition>> groupInstancesArtifacts = new HashMap<>();
338 for (ArtifactDefinition artifact : componentDeploymentArtifacts.values()) {
339 String type = artifact.getArtifactType();
341 if ( !type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_ENV.getType()) ){
342 finalDeploymentArtifacts.put(artifact.getArtifactLabel(), artifact);
345 if (!(type.equalsIgnoreCase(ArtifactTypeEnum.HEAT.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_NET.getType()) || type.equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType()))) {
349 if (artifact.checkEsIdExist()) {
350 Either<ArtifactDefinition, ResponseFormat> createHeatEnvPlaceHolder = artifactBusinessLogic.createHeatEnvPlaceHolder(artifact, ArtifactsBusinessLogic.HEAT_ENV_NAME, componentInstance.getUniqueId(), NodeTypeEnum.ResourceInstance,
351 componentInstance.getName(), user, containerComponent, existingEnvVersions);
352 if (createHeatEnvPlaceHolder.isRight()) {
353 return Either.right(createHeatEnvPlaceHolder.right().value());
355 ArtifactDefinition artifactDefinition = createHeatEnvPlaceHolder.left().value();
358 finalDeploymentArtifacts.put(artifactDefinition.getArtifactLabel(), artifactDefinition);
360 if(CollectionUtils.isNotEmpty(originComponent.getGroups())){
361 filteredGroups = originComponent.getGroups().stream().filter(g -> g.getType().equals(VF_MODULE)).collect(Collectors.toList());
363 if (CollectionUtils.isNotEmpty(filteredGroups)) {
364 for (GroupDefinition groupInstance : filteredGroups) {
365 Optional<String> op = groupInstance.getArtifacts().stream().filter(p -> p.equals(artifactDefinition.getGeneratedFromId())).findAny();
366 if (op.isPresent()) {
367 List<ArtifactDefinition> artifactsUid;
368 if (groupInstancesArtifacts.containsKey(groupInstance.getUniqueId())) {
369 artifactsUid = groupInstancesArtifacts.get(groupInstance.getUniqueId());
371 artifactsUid = new ArrayList<>();
373 artifactsUid.add(artifactDefinition);
374 groupInstancesArtifacts.put(groupInstance.getUniqueId(), artifactsUid);
382 StorageOperationStatus artStatus = toscaOperationFacade.addDeploymentArtifactsToInstance(containerComponent.getUniqueId(), componentInstance, finalDeploymentArtifacts);
383 if ( artStatus != StorageOperationStatus.OK){
384 log.debug("Failed to add instance deployment artifacts for instance {} in conatiner {} error {}", componentInstance.getUniqueId(), containerComponent.getUniqueId(), artStatus);
385 return Either.left(componentsUtils.convertFromStorageResponseForResourceInstance(artStatus, false));
388 StorageOperationStatus result = toscaOperationFacade
389 .addGroupInstancesToComponentInstance(containerComponent, componentInstance, filteredGroups, groupInstancesArtifacts);
390 if (result != StorageOperationStatus.OK) {
391 log.debug("failed to update group instance for component instance {}", componentInstance.getUniqueId());
392 return Either.left(componentsUtils.convertFromStorageResponse(result));
394 componentInstance.setDeploymentArtifacts(finalDeploymentArtifacts);
395 return Either.left(ActionStatus.OK);
398 private ActionStatus setResourceArtifactsOnResourceInstance(ComponentInstance resourceInstance) {
399 Either<Map<String, ArtifactDefinition>, StorageOperationStatus> getResourceDeploymentArtifacts = artifactBusinessLogic.getArtifacts(resourceInstance.getComponentUid(), NodeTypeEnum.Resource, true, ArtifactGroupTypeEnum.DEPLOYMENT, null);
401 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<String, ArtifactDefinition>();
402 if (getResourceDeploymentArtifacts.isRight()) {
403 StorageOperationStatus status = getResourceDeploymentArtifacts.right().value();
404 if (!status.equals(StorageOperationStatus.NOT_FOUND)) {
405 log.debug("Failed to fetch resource: {} artifacts. status is {}", resourceInstance.getComponentUid(), status);
406 return componentsUtils.convertFromStorageResponseForResourceInstance(status, true);
409 deploymentArtifacts = getResourceDeploymentArtifacts.left().value();
412 if (!deploymentArtifacts.isEmpty()) {
413 Map<String, ArtifactDefinition> tempDeploymentArtifacts = new HashMap<String, ArtifactDefinition>(deploymentArtifacts);
414 for (Entry<String, ArtifactDefinition> artifact : deploymentArtifacts.entrySet()) {
415 if (!artifact.getValue().checkEsIdExist()) {
416 tempDeploymentArtifacts.remove(artifact.getKey());
420 resourceInstance.setDeploymentArtifacts(tempDeploymentArtifacts);
423 return ActionStatus.OK;
426 public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance componentInstance) {
427 return updateComponentInstanceMetadata(containerComponentParam, containerComponentId, componentInstanceId, userId, componentInstance, false, true, true);
430 public Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance componentInstance, boolean inTransaction,
431 boolean needLock, boolean createNewTransaction) {
433 Either<User, ResponseFormat> resp = validateUserExists(userId, "update Component Instance", inTransaction);
434 if (resp.isRight()) {
435 return Either.right(resp.right().value());
438 Either<ComponentInstance, ResponseFormat> resultOp = null;
440 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
441 if (validateComponentType.isRight()) {
442 return Either.right(validateComponentType.right().value());
445 final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
447 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
448 if (validateComponentExists.isRight()) {
449 return Either.right(validateComponentExists.right().value());
451 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
453 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
454 if (validateCanWorkOnComponent.isRight()) {
455 return Either.right(validateCanWorkOnComponent.right().value());
457 ComponentTypeEnum instanceType = getComponentType(containerComponentType);
458 Either<Boolean, StorageOperationStatus> validateParentStatus = toscaOperationFacade.validateComponentExists(componentInstance.getComponentUid());
459 if (validateParentStatus.isRight()) {
460 log.debug("Failed to get component instance {} on service {}", componentInstanceId, containerComponentId);
461 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, componentInstance.getName(), instanceType.getValue().toLowerCase()));
464 if (!validateParentStatus.left().value()) {
465 resultOp = Either.right(
466 componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(), instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(), containerComponentId));
471 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "updateComponentInstance");
472 if (lockComponent.isRight()) {
473 return Either.right(lockComponent.right().value());
478 Either<Component, ResponseFormat> eitherResourceName = getOriginComponentNameFromComponentInstance(componentInstance, inTransaction);
480 if (eitherResourceName.isRight()) {
481 resultOp = Either.right(eitherResourceName.right().value());
484 Component origComponent = eitherResourceName.left().value();
486 resultOp = updateComponentInstanceMetadata(containerComponent, containerComponentType, origComponent, componentInstanceId, componentInstance, inTransaction);
491 unlockComponent(resultOp, containerComponent);
495 // New Multiple Instance Update API
496 public Either<List<ComponentInstance>, ResponseFormat> updateComponentInstance(String containerComponentParam, String containerComponentId, String userId, List<ComponentInstance> componentInstanceList, boolean needLock,
497 boolean createNewTransaction) {
499 Either<List<ComponentInstance>, ResponseFormat> resultOp = null;
500 org.openecomp.sdc.be.model.Component containerComponent = null;
502 Either<User, ResponseFormat> resp = validateUserExists(userId, "update Component Instance", true);
503 if (resp.isRight()) {
504 return Either.right(resp.right().value());
507 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
508 if (validateComponentType.isRight()) {
509 return Either.right(validateComponentType.right().value());
512 final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
514 ComponentParametersView componentFilter = new ComponentParametersView();
515 componentFilter.disableAll();
516 componentFilter.setIgnoreUsers(false);
517 componentFilter.setIgnoreComponentInstances(false);
518 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExistsByFilter(containerComponentId, containerComponentType, componentFilter, true);
519 if (validateComponentExists.isRight()) {
520 return Either.right(validateComponentExists.right().value());
523 containerComponent = validateComponentExists.left().value();
525 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
526 if (validateCanWorkOnComponent.isRight()) {
527 return Either.right(validateCanWorkOnComponent.right().value());
530 ComponentTypeEnum instanceType = getComponentType(containerComponentType);
532 for (ComponentInstance componentInstance : componentInstanceList) {
533 boolean validateParent = validateParent(containerComponent, componentInstance.getUniqueId());
534 if (!validateParent) {
535 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName(), instanceType.getValue().toLowerCase(), containerComponentType.getValue().toLowerCase(),
536 containerComponentId));
543 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "updateComponentInstance");
544 if (lockComponent.isRight()) {
545 return Either.right(lockComponent.right().value());
549 List<ComponentInstance> updatedList = new ArrayList<>();
550 List<ComponentInstance> instancesFromContainerComponent = containerComponent.getComponentInstances();
551 List<ComponentInstance> listForUpdate = new ArrayList<>();
552 if(instancesFromContainerComponent == null || instancesFromContainerComponent.isEmpty())
553 containerComponent.setComponentInstances(componentInstanceList);
555 Iterator<ComponentInstance> iterator = instancesFromContainerComponent.iterator();
556 while(iterator.hasNext()){
557 ComponentInstance origInst = iterator.next();
558 Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getUniqueId().equals(origInst.getUniqueId())).findAny();
560 ComponentInstance updatedCi = op.get();
561 Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, origInst, updatedCi.getName());
563 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ",
564 origInst.getName(), updatedCi.getName());
565 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), origInst.getName()));
569 listForUpdate.add(updatedCi);
572 listForUpdate.add(origInst);
574 containerComponent.setComponentInstances(listForUpdate);
576 if(resultOp == null){
577 Either<Component, StorageOperationStatus> updateStatus = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
578 if(updateStatus.isRight()){
579 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata belonging to container component {}. Status is {}. ", containerComponent.getName(), updateStatus.right().value());
580 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateStatus.right().value(), true), "", null));
583 for(ComponentInstance updatedInstance : updateStatus.left().value().getComponentInstances()){
584 Optional<ComponentInstance> op = componentInstanceList.stream().filter(ci -> ci.getName().equals(updatedInstance.getName())).findAny();
586 updatedList.add(updatedInstance);
592 resultOp = Either.left(updatedList);
597 unlockComponent(resultOp, containerComponent);
602 private boolean validateParent(org.openecomp.sdc.be.model.Component containerComponent, String nodeTemplateId) {
603 return containerComponent.getComponentInstances().stream().anyMatch(p -> p.getUniqueId().equals(nodeTemplateId));
606 private ComponentTypeEnum getComponentType(ComponentTypeEnum containerComponentType) {
607 if (ComponentTypeEnum.PRODUCT.equals(containerComponentType)) {
608 return ComponentTypeEnum.SERVICE_INSTANCE;
610 return ComponentTypeEnum.RESOURCE_INSTANCE;
614 private Either<ComponentInstance, ResponseFormat> updateComponentInstanceMetadata(Component containerComponent, ComponentTypeEnum containerComponentType, org.openecomp.sdc.be.model.Component origComponent, String componentInstanceId,
615 ComponentInstance componentInstance, boolean inTransaction) {
617 Either<ComponentInstance, ResponseFormat> resultOp = null;
618 Optional<ComponentInstance> componentInstanceOptional = null;
619 Either<ImmutablePair<Component, String>, StorageOperationStatus> updateRes = null;
621 if (resultOp == null) {
622 componentInstanceOptional = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(componentInstance.getUniqueId())).findFirst();
623 if (!componentInstanceOptional.isPresent()) {
624 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to find the component instance {} in container component {}. ", componentInstance.getName(), containerComponent.getName());
625 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
628 if (resultOp == null) {
629 ComponentInstance oldComponentInstance = componentInstanceOptional.get();
630 String newInstanceName = componentInstance.getName();
631 Boolean isUniqueName = validateInstanceNameUniquenessUponUpdate(containerComponent, oldComponentInstance, newInstanceName);
633 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update the name of the component instance {} to {}. A component instance with the same name already exists. ", oldComponentInstance.getName(), newInstanceName);
634 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, containerComponentType.getValue(), componentInstance.getName()));
637 if (resultOp == null) {
638 updateRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent, origComponent, componentInstance);
639 if (updateRes.isRight()) {
640 CommonUtility.addRecordToLog(log, LogLevelEnum.DEBUG, "Failed to update metadata of component instance {} belonging to container component {}. Status is {}. ", componentInstance.getName(), containerComponent.getName(),
641 updateRes.right().value());
642 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstance(componentsUtils.convertFromStorageResponseForResourceInstance(updateRes.right().value(), true), "", null));
645 if (resultOp == null) {
646 String newInstanceId = updateRes.left().value().getRight();
647 Optional<ComponentInstance> updatedInstanceOptional = updateRes.left().value().getLeft().getComponentInstances().stream().filter(ci -> ci.getUniqueId().equals(newInstanceId)).findFirst();
649 if (!updatedInstanceOptional.isPresent()) {
650 log.debug("Failed to update metadata of component instance {} of container component {}", componentInstance.getName(), containerComponent.getName());
651 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND_ON_CONTAINER, componentInstance.getName()));
653 resultOp = Either.left(updatedInstanceOptional.get());
657 if (resultOp == null) {
658 resultOp = Either.left(componentInstanceOptional.get());
663 public Either<ComponentInstance, ResponseFormat> deleteComponentInstance(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId) {
665 Either<User, ResponseFormat> resp = validateUserExists(userId, "delete Component Instance", false);
666 if (resp.isRight()) {
667 return Either.right(resp.right().value());
670 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
671 if (validateComponentType.isRight()) {
672 return Either.right(validateComponentType.right().value());
675 final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
676 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
677 if (validateComponentExists.isRight()) {
678 return Either.right(validateComponentExists.right().value());
680 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
681 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
682 if (validateCanWorkOnComponent.isRight()) {
683 return Either.right(validateCanWorkOnComponent.right().value());
686 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "deleteComponentInstance");
687 if (lockComponent.isRight()) {
688 return Either.right(lockComponent.right().value());
692 * if (!ComponentValidationUtils.canWorkOnComponent(containerComponentId, serviceOperation, userId)) { log.info( "Restricted operation for user " + userId + " on service " + containerComponentId); return Either.right(componentsUtils
693 * .getResponseFormat(ActionStatus.RESTRICTED_OPERATION)); } // lock resource StorageOperationStatus lockStatus = graphLockOperation.lockComponent( containerComponentId, NodeTypeEnum.Service); if (lockStatus != StorageOperationStatus.OK) {
694 * log.debug("Failed to lock service {}", containerComponentId); resultOp = Either.right(componentsUtils .getResponseFormat(componentsUtils .convertFromStorageResponse(lockStatus))); return resultOp; }
696 Either<ComponentInstance, ResponseFormat> resultOp = null;
698 resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
703 * if (resultOp == null || resultOp.isRight()) { titanGenericDao.rollback(); } else { titanGenericDao.commit(); } graphLockOperation.unlockComponent(containerComponentId, NodeTypeEnum.Service);
705 unlockComponent(resultOp, containerComponent);
709 private Either<ComponentInstance, ResponseFormat> deleteComponentInstance(Component containerComponent, String componentInstanceId, ComponentTypeEnum containerComponentType) {
711 Either<ComponentInstance, ResponseFormat> resultOp = null;
712 ComponentInstance deletedInstance = null;
713 Either<ImmutablePair<Component, String>, StorageOperationStatus> deleteRes = toscaOperationFacade.deleteComponentInstanceFromTopologyTemplate(containerComponent, componentInstanceId);
715 if (deleteRes.isRight()) {
716 log.debug("Failed to delete entry on graph for resourceInstance {}", componentInstanceId);
717 ActionStatus status = componentsUtils.convertFromStorageResponse(deleteRes.right().value(), containerComponentType);
718 resultOp = Either.right(componentsUtils.getResponseFormat(status, componentInstanceId));
720 if (resultOp == null) {
721 log.debug("The component instance {} has been removed from container component {}. ", componentInstanceId, containerComponent);
722 deletedInstance = findAndRemoveComponentInstanceFromContainerComponent(componentInstanceId, containerComponent);
723 resultOp = Either.left(deletedInstance);
725 if (resultOp.isLeft() && CollectionUtils.isNotEmpty(containerComponent.getGroups())) {
726 List<GroupDataDefinition> groupsToUpdate = new ArrayList<>();
727 for(GroupDataDefinition currGroup : containerComponent.getGroups()){
728 if(currGroup.getMembers().containsKey(deletedInstance.getName())){
729 currGroup.getMembers().remove(deletedInstance.getName());
730 groupsToUpdate.add(currGroup);
733 Either<List<GroupDefinition>, StorageOperationStatus> updateGroupsRes =
734 toscaOperationFacade.updateGroupsOnComponent(containerComponent, containerComponent.getComponentType(), groupsToUpdate);
735 if (updateGroupsRes.isRight()) {
736 log.debug("Failed to delete component instance {} from group members. ", componentInstanceId);
737 ActionStatus status = componentsUtils.convertFromStorageResponse(updateGroupsRes.right().value(), containerComponentType);
738 resultOp = Either.right(componentsUtils.getResponseFormat(status, componentInstanceId));
744 private ComponentInstance findAndRemoveComponentInstanceFromContainerComponent(String componentInstanceId, Component containerComponent) {
745 ComponentInstance foundInstance = null;
746 for(ComponentInstance instance : containerComponent.getComponentInstances()){
747 if(instance.getUniqueId().equals(componentInstanceId)){
748 foundInstance = instance;
749 containerComponent.getComponentInstances().remove(instance);
753 return foundInstance;
756 public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
757 return associateRIToRI(componentId, userId, requirementDef, componentTypeEnum, false, true, true);
760 public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum, boolean inTransaction, boolean needLock,
761 boolean createNewTransaction) {
763 Either<User, ResponseFormat> resp = validateUserExists(userId, "associate Ri To RI", inTransaction);
764 if (resp.isRight()) {
765 return Either.right(resp.right().value());
768 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
770 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null);
771 if (validateComponentExists.isRight()) {
772 return Either.right(validateComponentExists.right().value());
774 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
776 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
777 if (validateCanWorkOnComponent.isRight()) {
778 return Either.right(validateCanWorkOnComponent.right().value());
781 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "associateRIToRI");
783 if (lockComponent.isRight()) {
784 return Either.right(lockComponent.right().value());
790 resultOp = associateRIToRIOnGraph(validateComponentExists.left().value(), requirementDef, componentTypeEnum, inTransaction);
796 unlockComponent(resultOp, containerComponent);
800 public Either<RequirementCapabilityRelDef, ResponseFormat> associateRIToRIOnGraph(Component containerComponent, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum, boolean inTransaction) {
802 log.debug("Try to create entry on graph");
803 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
805 Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.associateResourceInstances(containerComponent.getUniqueId(), requirementDef);
807 if (result.isLeft()) {
808 log.debug("Enty on graph is created.");
809 RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value();
810 resultOp = Either.left(requirementCapabilityRelDef);
814 log.debug("Failed to associate node: {} with node {}", requirementDef.getFromNode(), requirementDef.getToNode());
815 String fromNameOrId = "";
816 String toNameOrId = "";
817 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
818 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
820 toNameOrId = requirementDef.getFromNode();
821 fromNameOrId = requirementDef.getFromNode();
822 if (fromResult.isLeft()) {
823 fromNameOrId = fromResult.left().value().getName();
825 if (toResult.isLeft()) {
826 toNameOrId = toResult.left().value().getName();
829 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRequirement()));
836 public Either<RequirementCapabilityRelDef, ResponseFormat> dissociateRIFromRI(String componentId, String userId, RequirementCapabilityRelDef requirementDef, ComponentTypeEnum componentTypeEnum) {
837 Either<User, ResponseFormat> resp = validateUserExists(userId, "dissociate RI From RI", false);
838 if (resp.isRight()) {
839 return Either.right(resp.right().value());
842 Either<RequirementCapabilityRelDef, ResponseFormat> resultOp = null;
843 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(componentId, componentTypeEnum, null);
844 if (validateComponentExists.isRight()) {
845 return Either.right(validateComponentExists.right().value());
847 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
849 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
850 if (validateCanWorkOnComponent.isRight()) {
851 return Either.right(validateCanWorkOnComponent.right().value());
853 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "associateRIToRI");
855 if (lockComponent.isRight()) {
856 return Either.right(lockComponent.right().value());
859 log.debug("Try to create entry on graph");
860 Either<RequirementCapabilityRelDef, StorageOperationStatus> result = toscaOperationFacade.dissociateResourceInstances(componentId, requirementDef);
861 if (result.isLeft()) {
862 log.debug("Enty on graph is created.");
863 RequirementCapabilityRelDef requirementCapabilityRelDef = result.left().value();
864 resultOp = Either.left(requirementCapabilityRelDef);
869 log.debug("Failed to dissocaite node {} from node {}", requirementDef.getFromNode(), requirementDef.getToNode());
870 String fromNameOrId = "";
871 String toNameOrId = "";
872 Either<ComponentInstance, StorageOperationStatus> fromResult = getResourceInstanceById(containerComponent, requirementDef.getFromNode());
873 Either<ComponentInstance, StorageOperationStatus> toResult = getResourceInstanceById(containerComponent, requirementDef.getToNode());
875 toNameOrId = requirementDef.getFromNode();
876 fromNameOrId = requirementDef.getFromNode();
877 if (fromResult.isLeft()) {
878 fromNameOrId = fromResult.left().value().getName();
880 if (toResult.isLeft()) {
881 toNameOrId = toResult.left().value().getName();
885 .right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponseForResourceInstance(result.right().value(), true), fromNameOrId, toNameOrId, requirementDef.getRelationships().get(0).getRequirement()));
889 unlockComponent(resultOp, containerComponent);
893 private Either<ComponentInstanceProperty, ResponseFormat> updateAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
894 Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.updateAttributeValueInResourceInstance(attribute, resourceInstanceId, true);
895 Either<ComponentInstanceProperty, ResponseFormat> result;
896 if (eitherAttribute.isLeft()) {
897 log.debug("Attribute value {} was updated on graph.", attribute.getValueUniqueUid());
898 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
900 result = Either.left(instanceAttribute);
903 log.debug("Failed to update attribute value {} in resource instance {}", attribute, resourceInstanceId);
905 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
907 result = Either.right(componentsUtils.getResponseFormat(actionStatus, ""));
913 private Either<ComponentInstanceProperty, ResponseFormat> createAttributeValue(ComponentInstanceProperty attribute, String resourceInstanceId) {
915 Either<ComponentInstanceProperty, ResponseFormat> result;
917 Wrapper<Integer> indexCounterWrapper = new Wrapper<>();
918 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
919 validateIncrementCounter(resourceInstanceId, GraphPropertiesDictionary.ATTRIBUTE_COUNTER, indexCounterWrapper, errorWrapper);
921 if (!errorWrapper.isEmpty()) {
922 result = Either.right(errorWrapper.getInnerElement());
924 Either<ComponentInstanceProperty, StorageOperationStatus> eitherAttribute = componentInstanceOperation.addAttributeValueToResourceInstance(attribute, resourceInstanceId, indexCounterWrapper.getInnerElement(), true);
925 if (eitherAttribute.isLeft()) {
926 log.debug("Attribute value was added to resource instance {}", resourceInstanceId);
927 ComponentInstanceProperty instanceAttribute = eitherAttribute.left().value();
928 result = Either.left(instanceAttribute);
931 log.debug("Failed to add attribute value {} to resource instance {}", attribute, resourceInstanceId);
933 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(eitherAttribute.right().value());
934 result = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
942 * Create Or Updates Attribute Instance
944 * @param componentTypeEnum
946 * @param resourceInstanceId
951 public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateAttributeValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceProperty attribute, String userId) {
952 Either<ComponentInstanceProperty, ResponseFormat> result = null;
953 Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
955 validateUserExist(userId, "create Or Update Attribute Value", errorWrapper);
956 if (errorWrapper.isEmpty()) {
957 validateComponentTypeEnum(componentTypeEnum, "CreateOrUpdateAttributeValue", errorWrapper);
959 if (errorWrapper.isEmpty()) {
960 validateCanWorkOnComponent(componentId, componentTypeEnum, userId, errorWrapper);
962 if (errorWrapper.isEmpty()) {
963 validateComponentLock(componentId, componentTypeEnum, errorWrapper);
967 if (errorWrapper.isEmpty()) {
968 final boolean isCreate = Objects.isNull(attribute.getValueUniqueUid());
970 result = createAttributeValue(attribute, resourceInstanceId);
972 result = updateAttributeValue(attribute, resourceInstanceId);
975 result = Either.right(errorWrapper.getInnerElement());
981 if (result == null || result.isRight()) {
987 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
991 // US833308 VLI in service - specific network_role property value logic
992 private StorageOperationStatus concatServiceNameToVLINetworkRolePropertyValue(ToscaOperationFacade toscaOperationFacade, ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceProperty property) {
993 if (StringUtils.isNotEmpty(property.getValue()) && PropertyNames.NETWORK_ROLE.getPropertyName().equalsIgnoreCase(property.getName()) && ComponentTypeEnum.SERVICE == componentTypeEnum) {
994 ComponentParametersView componentParametersView = new ComponentParametersView();
995 componentParametersView.disableAll();
996 componentParametersView.setIgnoreComponentInstances(false);
997 Either<Component, StorageOperationStatus> getServiceResult = toscaOperationFacade.getToscaElement(componentId, componentParametersView);
998 if (getServiceResult.isRight()) {
999 return getServiceResult.right().value();
1001 Component service = getServiceResult.left().value();
1002 Optional<ComponentInstance> getInstance = service.getComponentInstances().stream().filter(p -> p.getUniqueId().equals(resourceInstanceId)).findAny();
1003 if (!getInstance.isPresent()) {
1004 return StorageOperationStatus.NOT_FOUND;
1006 String prefix = service.getSystemName() + ".";
1007 String value = property.getValue();
1008 if (OriginTypeEnum.VL == getInstance.get().getOriginType() && (!value.startsWith(prefix) || value.equalsIgnoreCase(prefix))) {
1009 property.setValue(prefix + value);
1012 return StorageOperationStatus.OK;
1015 public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdatePropertyValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceProperty property, String userId) {
1017 Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
1019 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Or Update Property Value", false);
1020 if (resp.isRight()) {
1021 return Either.right(resp.right().value());
1024 if (componentTypeEnum == null) {
1025 BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1026 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1029 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
1031 if (getResourceResult.isRight()) {
1032 log.debug("Failed to retrieve component, component id {}", componentId);
1033 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1036 Component containerComponent = getResourceResult.left().value();
1038 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
1039 log.info("Restricted operation for user: {} on service {}", userId, componentId);
1040 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1043 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
1044 if (resourceInstanceStatus.isRight()) {
1045 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
1048 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
1049 // specific property value logic US833308
1050 StorageOperationStatus fetchByIdsStatus = concatServiceNameToVLINetworkRolePropertyValue(toscaOperationFacade, componentTypeEnum, componentId, resourceInstanceId, property);
1051 if (StorageOperationStatus.OK != fetchByIdsStatus) {
1052 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(fetchByIdsStatus)));
1056 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1057 if (lockStatus != StorageOperationStatus.OK) {
1058 log.debug("Failed to lock service {}", componentId);
1059 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1062 String innerType = null;
1063 String propertyType = property.getType();
1064 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
1065 log.debug("The type of the property {} is {}", property.getUniqueId(), propertyType);
1067 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
1068 SchemaDefinition def = property.getSchema();
1070 log.debug("Schema doesn't exists for property of type {}", type);
1071 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1073 PropertyDataDefinition propDef = def.getProperty();
1074 if (propDef == null) {
1075 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
1076 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1078 innerType = propDef.getType();
1080 // Specific Update Logic
1081 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = dataTypeCache.getAll();
1082 if (allDataTypes.isRight()) {
1083 TitanOperationStatus status = allDataTypes.right().value();
1084 BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
1085 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
1087 Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes.left().value());
1089 String newValue = property.getValue();
1090 if (isValid.isRight()) {
1091 Boolean res = isValid.right().value();
1093 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
1096 Object object = isValid.left().value();
1097 if (object != null) {
1098 newValue = object.toString();
1102 ImmutablePair<String, Boolean> pair = propertyOperation.validateAndUpdateRules(propertyType, property.getRules(), innerType, allDataTypes.left().value(), true);
1103 if (pair.getRight() != null && pair.getRight() == false) {
1104 BeEcompErrorManager.getInstance().logBeInvalidValueError("Add property value", pair.getLeft(), property.getName(), propertyType);
1105 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
1109 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(resourceInstanceId);
1110 Optional<ComponentInstanceProperty> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1111 StorageOperationStatus status;
1112 instanceProperty.get().setValue(newValue);
1113 if(instanceProperty.isPresent()){
1114 status = toscaOperationFacade.updateComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), property);
1116 status = toscaOperationFacade.addComponentInstanceProperty(containerComponent, foundResourceInstance.getUniqueId(), property);
1118 if(status != StorageOperationStatus.OK){
1119 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1120 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1123 List<String> path = new ArrayList<>();
1124 path.add(foundResourceInstance.getUniqueId());
1125 property.setPath(path);
1127 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
1128 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
1130 if (updateContainerRes.isRight()) {
1131 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
1132 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1135 resultOp = Either.left(property);
1139 if (resultOp == null || resultOp.isRight()) {
1140 titanDao.rollback();
1145 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1150 public Either<ComponentInstanceInput, ResponseFormat> createOrUpdateInstanceInputValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceInput property, String userId) {
1152 Either<ComponentInstanceInput, ResponseFormat> resultOp = null;
1154 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Or Update Property Value", false);
1155 if (resp.isRight()) {
1156 return Either.right(resp.right().value());
1159 if (componentTypeEnum == null) {
1160 BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1161 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1164 Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseAll);
1166 if (getResourceResult.isRight()) {
1167 log.debug("Failed to retrieve component, component id {}", componentId);
1168 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1171 Component containerComponent = getResourceResult.left().value();
1173 if (!ComponentValidationUtils.canWorkOnComponent(containerComponent, userId)) {
1174 log.info("Restricted operation for user: {} on service {}", userId, componentId);
1175 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1178 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, resourceInstanceId);
1179 if (resourceInstanceStatus.isRight()) {
1180 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, resourceInstanceId, componentId));
1184 ComponentInstance foundResourceInstance = resourceInstanceStatus.left().value();
1187 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1188 if (lockStatus != StorageOperationStatus.OK) {
1189 log.debug("Failed to lock service {}", componentId);
1190 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1193 String innerType = null;
1194 String propertyType = property.getType();
1195 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
1196 log.debug("The type of the property {} is {}", property.getUniqueId(), propertyType);
1198 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
1199 SchemaDefinition def = property.getSchema();
1201 log.debug("Schema doesn't exists for property of type {}", type);
1202 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1204 PropertyDataDefinition propDef = def.getProperty();
1205 if (propDef == null) {
1206 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
1207 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE)));
1209 innerType = propDef.getType();
1211 // Specific Update Logic
1212 Either<Map<String, DataTypeDefinition>, TitanOperationStatus> allDataTypes = dataTypeCache.getAll();
1213 if (allDataTypes.isRight()) {
1214 TitanOperationStatus status = allDataTypes.right().value();
1215 BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
1216 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(status))));
1218 Either<Object, Boolean> isValid = propertyOperation.validateAndUpdatePropertyValue(propertyType, property.getValue(), true, innerType, allDataTypes.left().value());
1220 String newValue = property.getValue();
1221 if (isValid.isRight()) {
1222 Boolean res = isValid.right().value();
1224 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(TitanOperationStatus.ILLEGAL_ARGUMENT))));
1227 Object object = isValid.left().value();
1228 if (object != null) {
1229 newValue = object.toString();
1234 List<ComponentInstanceInput> instanceProperties = containerComponent.getComponentInstancesInputs().get(resourceInstanceId);
1235 Optional<ComponentInstanceInput> instanceProperty = instanceProperties.stream().filter(p -> p.getUniqueId().equals(property.getUniqueId())).findAny();
1236 StorageOperationStatus status;
1237 if(instanceProperty.isPresent()){
1238 instanceProperty.get().setValue(property.getValue());
1239 status = toscaOperationFacade.updateComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), property);
1241 status = toscaOperationFacade.addComponentInstanceInput(containerComponent, foundResourceInstance.getUniqueId(), property);
1243 if(status != StorageOperationStatus.OK){
1244 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1245 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1248 foundResourceInstance.setCustomizationUUID(UUID.randomUUID().toString());
1249 Either<Component, StorageOperationStatus> updateContainerRes = toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(containerComponent);
1251 if (updateContainerRes.isRight()) {
1252 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateContainerRes.right().value());
1253 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1256 resultOp = Either.left(property);
1260 if (resultOp == null || resultOp.isRight()) {
1261 titanDao.rollback();
1266 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1271 public Either<ComponentInstanceProperty, ResponseFormat> createOrUpdateGroupInstancePropertyValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, String groupInstanceId, ComponentInstanceProperty property,
1274 Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
1276 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Or Update Property Value", false);
1277 if (resp.isRight()) {
1278 return Either.right(resp.right().value());
1281 if (componentTypeEnum == null) {
1282 BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1283 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1287 if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
1288 log.info("Restricted operation for user: {} on service: {}", userId, componentId);
1289 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1293 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1294 if (lockStatus != StorageOperationStatus.OK) {
1295 log.debug("Failed to lock service {}", componentId);
1296 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1300 String propertyValueUid = property.getValueUniqueUid();
1302 if (propertyValueUid == null) {
1304 Either<Integer, StorageOperationStatus> counterRes = groupInstanceOperation.increaseAndGetGroupInstancePropertyCounter(groupInstanceId);
1306 if (counterRes.isRight()) {
1307 log.debug("increaseAndGetResourcePropertyCounter failed resource instance: {} property: {}", resourceInstanceId, property);
1308 StorageOperationStatus status = counterRes.right().value();
1309 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1310 resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
1312 Integer index = counterRes.left().value();
1313 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.addPropertyValueToGroupInstance(property, resourceInstanceId, index, true);
1315 if (result.isLeft()) {
1316 log.trace("Property value was added to resource instance {}", resourceInstanceId);
1317 ComponentInstanceProperty instanceProperty = result.left().value();
1319 resultOp = Either.left(instanceProperty);
1322 log.debug("Failed to add property value: {} to resource instance {}", property, resourceInstanceId);
1324 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1326 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1330 Either<ComponentInstanceProperty, StorageOperationStatus> result = groupInstanceOperation.updatePropertyValueInGroupInstance(property, resourceInstanceId, true);
1332 if (result.isLeft()) {
1333 log.debug("Property value {} was updated on graph.", property.getValueUniqueUid());
1334 ComponentInstanceProperty instanceProperty = result.left().value();
1336 resultOp = Either.left(instanceProperty);
1339 log.debug("Failed to update property value: {}, in resource instance {}", property, resourceInstanceId);
1341 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1343 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1346 if (resultOp.isLeft()) {
1347 StorageOperationStatus updateCustomizationUUID = componentInstanceOperation.updateCustomizationUUID(resourceInstanceId);
1348 if (updateCustomizationUUID != StorageOperationStatus.OK) {
1349 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(updateCustomizationUUID);
1351 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1358 if (resultOp == null || resultOp.isRight()) {
1359 titanDao.rollback();
1364 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1369 public Either<ComponentInstanceInput, ResponseFormat> createOrUpdateInputValue(ComponentTypeEnum componentTypeEnum, String componentId, String resourceInstanceId, ComponentInstanceInput inputProperty, String userId) {
1371 Either<ComponentInstanceInput, ResponseFormat> resultOp = null;
1373 Either<User, ResponseFormat> resp = validateUserExists(userId, "create Or Update Input Value", false);
1374 if (resp.isRight()) {
1375 return Either.right(resp.right().value());
1378 if (componentTypeEnum == null) {
1379 BeEcompErrorManager.getInstance().logInvalidInputError("createOrUpdateInputValue", "invalid component type", ErrorSeverity.INFO);
1380 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1384 if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
1385 log.info("Restricted operation for user: {} on service: {}", userId, componentId);
1386 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1390 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
1391 if (lockStatus != StorageOperationStatus.OK) {
1392 log.debug("Failed to lock service {}", componentId);
1393 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1397 String propertyValueUid = inputProperty.getValueUniqueUid();
1398 if (propertyValueUid == null) {
1400 Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation.increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, GraphPropertiesDictionary.INPUT_COUNTER, true);
1402 if (counterRes.isRight()) {
1403 log.debug("increaseAndGetResourceInputCounter failed resource instance {} inputProperty {}", resourceInstanceId, inputProperty);
1404 StorageOperationStatus status = counterRes.right().value();
1405 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1406 resultOp = Either.right(componentsUtils.getResponseFormat(actionStatus));
1408 Integer index = counterRes.left().value();
1409 Either<ComponentInstanceInput, StorageOperationStatus> result = componentInstanceOperation.addInputValueToResourceInstance(inputProperty, resourceInstanceId, index, true);
1411 if (result.isLeft()) {
1412 log.debug("Property value was added to resource instance {}", resourceInstanceId);
1413 ComponentInstanceInput instanceProperty = result.left().value();
1415 resultOp = Either.left(instanceProperty);
1419 log.debug("Failed to add input value {} to resource instance {}", inputProperty, resourceInstanceId);
1421 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1423 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1429 Either<ComponentInstanceInput, StorageOperationStatus> result = componentInstanceOperation.updateInputValueInResourceInstance(inputProperty, resourceInstanceId, true);
1431 if (result.isLeft()) {
1432 log.debug("Input value {} was updated on graph.", inputProperty.getValueUniqueUid());
1433 ComponentInstanceInput instanceProperty = result.left().value();
1435 resultOp = Either.left(instanceProperty);
1439 log.debug("Failed to update property value {} in resource instance {}", inputProperty, resourceInstanceId);
1441 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1443 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1450 if (resultOp == null || resultOp.isRight()) {
1451 titanDao.rollback();
1456 graphLockOperation.unlockComponent(componentId, componentTypeEnum.getNodeType());
1461 public Either<ComponentInstanceProperty, ResponseFormat> deletePropertyValue(ComponentTypeEnum componentTypeEnum, String serviceId, String resourceInstanceId, String propertyValueId, String userId) {
1463 Either<User, ResponseFormat> resp = validateUserExists(userId, "delete Property Value", false);
1464 if (resp.isRight()) {
1465 return Either.right(resp.right().value());
1468 Either<ComponentInstanceProperty, ResponseFormat> resultOp = null;
1470 if (componentTypeEnum == null) {
1471 BeEcompErrorManager.getInstance().logInvalidInputError("CreateOrUpdatePropertyValue", "invalid component type", ErrorSeverity.INFO);
1472 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
1476 if (!ComponentValidationUtils.canWorkOnComponent(serviceId, toscaOperationFacade, userId)) {
1477 log.info("Restricted operation for user {} on service {}", userId, serviceId);
1478 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
1482 StorageOperationStatus lockStatus = graphLockOperation.lockComponent(serviceId, componentTypeEnum.getNodeType());
1483 if (lockStatus != StorageOperationStatus.OK) {
1484 log.debug("Failed to lock service {}", serviceId);
1485 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
1489 Either<ComponentInstanceProperty, StorageOperationStatus> result = propertyOperation.removePropertyValueFromResourceInstance(propertyValueId, resourceInstanceId, true);
1491 if (result.isLeft()) {
1492 log.debug("Property value {} was removed from graph.", propertyValueId);
1493 ComponentInstanceProperty instanceProperty = result.left().value();
1495 resultOp = Either.left(instanceProperty);
1499 log.debug("Failed to remove property value {} in resource instance {}", propertyValueId, resourceInstanceId);
1501 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(result.right().value());
1503 resultOp = Either.right(componentsUtils.getResponseFormatForResourceInstanceProperty(actionStatus, ""));
1509 if (resultOp == null || resultOp.isRight()) {
1510 titanDao.rollback();
1515 graphLockOperation.unlockComponent(serviceId, componentTypeEnum.getNodeType());
1520 private Either<Component, ResponseFormat> getAndValidateOriginComponentOfComponentInstance(ComponentTypeEnum containerComponentType, ComponentInstance componentInstance) {
1522 Either<Component, ResponseFormat> eitherResponse = null;
1523 ComponentTypeEnum componentType = getComponentTypeByParentComponentType(containerComponentType);
1524 Component component;
1525 ResponseFormat errorResponse;
1526 Either<Component, StorageOperationStatus> getComponentRes = toscaOperationFacade.getToscaFullElement(componentInstance.getComponentUid());
1527 if (getComponentRes.isRight()) {
1528 log.debug("Failed to get the component with id {} for component instance {} creation. ", componentInstance.getComponentUid(), componentInstance.getName());
1529 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentRes.right().value(), componentType);
1530 errorResponse = componentsUtils.getResponseFormat(actionStatus, Constants.EMPTY_STRING);
1531 eitherResponse = Either.right(errorResponse);
1533 if(eitherResponse == null) {
1534 component = getComponentRes.left().value();
1535 LifecycleStateEnum resourceCurrState = component.getLifecycleState();
1536 if (resourceCurrState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
1537 ActionStatus actionStatus = ActionStatus.ILLEGAL_COMPONENT_STATE;
1538 errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().toString(), component.getName(), resourceCurrState.toString());
1539 eitherResponse = Either.right(errorResponse);
1542 if(eitherResponse == null) {
1543 eitherResponse = Either.left(getComponentRes.left().value());
1545 return eitherResponse;
1548 public Either<ComponentInstance, ResponseFormat> changeComponentInstanceVersion(String containerComponentParam, String containerComponentId, String componentInstanceId, String userId, ComponentInstance newComponentInstance) {
1550 Either<User, ResponseFormat> resp = validateUserExists(userId, "change Component Instance Version", false);
1551 if (resp.isRight()) {
1552 return Either.right(resp.right().value());
1555 User user = resp.left().value();
1556 Either<ComponentInstance, ResponseFormat> resultOp = null;
1558 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentParam);
1559 if (validateComponentType.isRight()) {
1560 return Either.right(validateComponentType.right().value());
1563 final ComponentTypeEnum containerComponentType = validateComponentType.left().value();
1565 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> validateComponentExists = validateComponentExists(containerComponentId, containerComponentType, null);
1566 if (validateComponentExists.isRight()) {
1567 return Either.right(validateComponentExists.right().value());
1569 org.openecomp.sdc.be.model.Component containerComponent = validateComponentExists.left().value();
1571 Either<Boolean, ResponseFormat> validateCanWorkOnComponent = validateCanWorkOnComponent(containerComponent, userId);
1572 if (validateCanWorkOnComponent.isRight()) {
1573 return Either.right(validateCanWorkOnComponent.right().value());
1576 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent,componentInstanceId);
1577 if (resourceInstanceStatus.isRight()) {
1578 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceId, containerComponentId));
1582 ComponentInstance currentResourceInstance = resourceInstanceStatus.left().value();
1584 Either<Boolean, ResponseFormat> lockComponent = lockComponent(containerComponent, "changeComponentInstanceVersion");
1585 if (lockComponent.isRight()) {
1586 return Either.right(lockComponent.right().value());
1590 if (currentResourceInstance.getComponentUid().equals(newComponentInstance.getComponentUid())) {
1591 resultOp = Either.left(currentResourceInstance);
1595 String resourceId = newComponentInstance.getComponentUid();
1596 Either<Boolean, StorageOperationStatus> componentExistsRes = toscaOperationFacade.validateComponentExists(resourceId);
1597 if(componentExistsRes.isRight()){
1598 log.debug("Failed to validate existing of the component {}. Status is {} ", resourceId);
1599 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(componentExistsRes.right().value()), resourceId));
1602 else if (!componentExistsRes.left().value()) {
1603 log.debug("The resource {} not found ", resourceId);
1604 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND, resourceId));
1608 // List<GroupInstance> groupInstances = currentResourceInstance.getGroupInstances();
1609 Map<String, ArtifactDefinition> deploymentArtifacts = currentResourceInstance.getDeploymentArtifacts();
1610 resultOp = deleteComponentInstance(containerComponent, componentInstanceId, containerComponentType);
1611 if (resultOp.isRight()) {
1612 log.debug("failed to delete resource instance {}", resourceId);
1616 Either<Component, ResponseFormat> eitherResourceName = getOriginComponentNameFromComponentInstance(newComponentInstance, true);
1618 if (eitherResourceName.isRight()) {
1619 resultOp = Either.right(eitherResourceName.right().value());
1623 Component origComponent = eitherResourceName.left().value();
1625 ComponentInstance resResourceInfo = resultOp.left().value();
1626 newComponentInstance.setName(resResourceInfo.getName());
1627 newComponentInstance.setPosX(resResourceInfo.getPosX());
1628 newComponentInstance.setPosY(resResourceInfo.getPosY());
1629 newComponentInstance.setDescription(resResourceInfo.getDescription());
1631 resultOp = createComponentInstanceOnGraph(containerComponent, origComponent, newComponentInstance, user);
1633 if (resultOp.isRight()) {
1634 log.debug("failed to create resource instance {}", resourceId);
1638 ComponentInstance updatedComponentInstance = resultOp.left().value();
1639 if (resultOp.isRight()) {
1640 log.debug("failed to create resource instance {}", resourceId);
1644 /* if (CollectionUtils.isNotEmpty(groupInstances)) {
1645 StorageOperationStatus addGroupsToComponentInstance = toscaOperationFacade.addGroupInstancesToComponentInstance(containerComponent, updatedComponentInstance, groupInstances);
1646 if (addGroupsToComponentInstance != StorageOperationStatus.OK) {
1647 BeEcompErrorManager.getInstance().logInternalFlowError("ChangeComponentInstanceVersion", "Failed to associate groups to new component instance", ErrorSeverity.ERROR);
1648 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1654 if (MapUtils.isNotEmpty(deploymentArtifacts)) {
1655 StorageOperationStatus addDeploymentArtifactsToComponentInstance = toscaOperationFacade.addDeploymentArtifactsToComponentInstance(containerComponent, updatedComponentInstance, deploymentArtifacts);
1656 if (addDeploymentArtifactsToComponentInstance != StorageOperationStatus.OK) {
1657 BeEcompErrorManager.getInstance().logInternalFlowError("ChangeComponentInstanceVersion", "Failed to associate groups to new component instance", ErrorSeverity.ERROR);
1658 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
1664 ComponentParametersView filter = new ComponentParametersView(true);
1665 filter.setIgnoreComponentInstances(false);
1666 Either<Component, StorageOperationStatus> updatedComponentRes = toscaOperationFacade.getToscaElement(containerComponentId, filter);
1667 if (updatedComponentRes.isRight()) {
1668 StorageOperationStatus storageOperationStatus = updatedComponentRes.right().value();
1669 ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(storageOperationStatus, containerComponent.getComponentType());
1670 ResponseFormat responseFormat = componentsUtils.getResponseFormat(actionStatus, Constants.EMPTY_STRING);
1671 log.debug("Component with id {} was not found", containerComponentId);
1672 return Either.right(responseFormat);
1674 resourceInstanceStatus = getResourceInstanceById(updatedComponentRes.left().value(),updatedComponentInstance.getUniqueId());
1675 if (resourceInstanceStatus.isRight()) {
1676 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(resourceInstanceStatus.right().value()), updatedComponentInstance.getUniqueId()));
1679 resultOp = Either.left(resourceInstanceStatus.left().value());
1683 unlockComponent(resultOp, containerComponent);
1687 protected abstract Either<Boolean, ResponseFormat> validateAllowedToContainCompInstances(org.openecomp.sdc.be.model.Component containerComponent);
1689 protected abstract NodeTypeEnum getNodeTypeOfComponentInstanceOrigin();
1691 protected abstract ComponentTypeEnum getComponentTypeOfComponentInstance();
1694 public Either<List<ComponentInstanceProperty>, ResponseFormat> getComponentInstancePropertiesById(String containerComponentTypeParam, String containerComponentId, String componentInstanceUniqueId, String userId) {
1695 final String ECOMP_ERROR_CONTEXT = "Get Component Instance Properties By Id";
1696 Component containerComponent = null;
1698 Either<List<ComponentInstanceProperty>, ResponseFormat> resultOp = null;
1700 Either<User, ResponseFormat> validateUserExists = validateUserExists(userId, ECOMP_ERROR_CONTEXT, false);
1701 if (validateUserExists.isRight()) {
1702 resultOp = Either.right(validateUserExists.right().value());
1706 Either<ComponentTypeEnum, ResponseFormat> validateComponentType = validateComponentType(containerComponentTypeParam);
1707 if (validateComponentType.isRight()) {
1708 resultOp = Either.right(validateComponentType.right().value());
1712 Either<Component, StorageOperationStatus> validateContainerComponentExists = toscaOperationFacade.getToscaElement(containerComponentId);
1713 if (validateContainerComponentExists.isRight()) {
1714 resultOp = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(validateContainerComponentExists.right().value())));
1717 containerComponent = validateContainerComponentExists.left().value();
1719 Either<ComponentInstance, StorageOperationStatus> resourceInstanceStatus = getResourceInstanceById(containerComponent, componentInstanceUniqueId);
1720 if (resourceInstanceStatus.isRight()) {
1721 resultOp = Either.right(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId));
1725 List<ComponentInstanceProperty> instanceProperties = containerComponent.getComponentInstancesProperties().get(componentInstanceUniqueId);
1726 if(CollectionUtils.isEmpty(instanceProperties)){
1727 instanceProperties = new ArrayList<>();
1729 resultOp = Either.left(instanceProperties);
1732 unlockComponent(resultOp, containerComponent);
1736 protected void validateIncrementCounter(String resourceInstanceId, GraphPropertiesDictionary counterType, Wrapper<Integer> instaceCounterWrapper, Wrapper<ResponseFormat> errorWrapper) {
1737 Either<Integer, StorageOperationStatus> counterRes = componentInstanceOperation.increaseAndGetResourceInstanceSpecificCounter(resourceInstanceId, counterType, true);
1739 if (counterRes.isRight()) {
1740 log.debug("increase And Get {} failed resource instance {}", counterType.name(), resourceInstanceId);
1741 StorageOperationStatus status = counterRes.right().value();
1742 ActionStatus actionStatus = componentsUtils.convertFromStorageResponseForResourceInstanceProperty(status);
1743 errorWrapper.setInnerElement(componentsUtils.getResponseFormat(actionStatus));
1745 instaceCounterWrapper.setInnerElement(counterRes.left().value());
1751 * updates componentInstance modificationTime
1753 * @param componentInstance
1754 * @param componentInstanceType
1755 * @param modificationTime
1756 * @param inTransaction
1759 public Either<ComponentInstanceData, ResponseFormat> updateComponentInstanceModificationTimeAndCustomizationUuid(ComponentInstance componentInstance, NodeTypeEnum componentInstanceType, Long modificationTime, boolean inTransaction) {
1760 Either<ComponentInstanceData, ResponseFormat> result;
1761 Either<ComponentInstanceData, StorageOperationStatus> updateComponentInstanceRes = componentInstanceOperation.updateComponentInstanceModificationTimeAndCustomizationUuidOnGraph(componentInstance, componentInstanceType, modificationTime,
1763 if (updateComponentInstanceRes.isRight()) {
1764 log.debug("Failed to update component instance {} with new last update date and mofifier. Status is {}. ", componentInstance.getName(), updateComponentInstanceRes.right().value());
1765 result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(updateComponentInstanceRes.right().value())));
1767 result = Either.left(updateComponentInstanceRes.left().value());
1772 private Boolean validateInstanceNameUniqueness(Component containerComponent, ComponentInstance oldComponentInstance, String newInstanceName) {
1773 Boolean isUnique = true;
1774 String newInstanceNormalizedName = ValidationUtils.normalizeComponentInstanceName(newInstanceName);
1775 if (!oldComponentInstance.getNormalizedName().equals(newInstanceNormalizedName)) {
1776 Optional<ComponentInstance> foundComponentInstance = containerComponent.getComponentInstances().stream().filter(ci -> ci.getNormalizedName().equals(newInstanceNormalizedName)).findFirst();
1777 if (foundComponentInstance.isPresent()) {
1781 foundComponentInstance = containerComponent.getComponentInstances().stream().filter(ci -> ci.getUniqueId().endsWith(newInstanceNormalizedName)).findFirst();
1782 if (foundComponentInstance.isPresent()) {
1790 private Boolean validateInstanceNameUniquenessUponUpdate(Component containerComponent, ComponentInstance oldComponentInstance, String newInstanceName) {
1791 Boolean isUnique = true;
1792 String newInstanceNormalizedName = ValidationUtils.normalizeComponentInstanceName(newInstanceName);
1793 if (!oldComponentInstance.getNormalizedName().equals(newInstanceNormalizedName)) {
1794 Optional<ComponentInstance> foundComponentInstance = containerComponent.getComponentInstances().stream().filter(ci -> ci.getNormalizedName().equals(newInstanceNormalizedName)).findFirst();
1795 if (foundComponentInstance.isPresent()) {
1802 private Either<ComponentInstance, StorageOperationStatus> getResourceInstanceById(Component containerComponent, String instanceId) {
1804 Either<ComponentInstance, StorageOperationStatus> result = null;
1805 List<ComponentInstance> instances = containerComponent.getComponentInstances();
1806 Optional<ComponentInstance> foundInstance = null;
1807 if(CollectionUtils.isEmpty(instances)){
1808 result = Either.right(StorageOperationStatus.NOT_FOUND);
1811 foundInstance = instances.stream().filter(i -> i.getUniqueId().equals(instanceId)).findFirst();
1812 if(!foundInstance.isPresent()){
1813 result = Either.right(StorageOperationStatus.NOT_FOUND);
1817 result = Either.left(foundInstance.get());