merge 1710 to 1.1.0
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsontitan / operations / ToscaElementLifecycleOperation.java
index 1397e81..f34975b 100644 (file)
@@ -1,3 +1,23 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
 package org.openecomp.sdc.be.model.jsontitan.operations;
 
 import java.util.ArrayList;
@@ -7,7 +27,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Map.Entry;
 import java.util.stream.Collectors;
 
 import org.apache.commons.collections.CollectionUtils;
@@ -17,9 +36,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
-
 import org.openecomp.sdc.be.config.ConfigurationManager;
-import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
@@ -35,20 +52,16 @@ import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition;
-import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
-import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
-import org.openecomp.sdc.be.model.ArtifactDefinition;
 import org.openecomp.sdc.be.model.ComponentParametersView;
 import org.openecomp.sdc.be.model.DistributionStatusEnum;
-import org.openecomp.sdc.be.model.GroupDefinition;
 import org.openecomp.sdc.be.model.LifecycleStateEnum;
 import org.openecomp.sdc.be.model.User;
 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
@@ -58,10 +71,8 @@ import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
-import org.openecomp.sdc.common.api.ArtifactTypeEnum;
 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
-import org.openecomp.sdc.exception.ResponseFormat;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -755,6 +766,12 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                                result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
                        }
                }
+               if (result == null) {
+                       Either<GraphVertex, StorageOperationStatus> updateRelationsRes = updateLastModifierEdge(toscaElement, owner, modifier);
+                       if (updateRelationsRes.isRight()) {
+                               result = updateRelationsRes.right().value();
+                       }
+               }
                if (result == null) {
                        result = StorageOperationStatus.OK;
                }
@@ -871,8 +888,9 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                // check if component with the next version doesn't exist.
                Iterator<Edge> nextVersionComponentIter = toscaElementVertex.getVertex().edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
                if (nextVersionComponentIter != null && nextVersionComponentIter.hasNext()) {
-                       String fetchedVersion = (String) nextVersionComponentIter.next().inVertex().property(GraphPropertyEnum.VERSION.getProperty()).value();
-                       String fetchedName = (String) nextVersionComponentIter.next().inVertex().property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
+                       Vertex  nextVersionVertex =  nextVersionComponentIter.next().inVertex();
+                       String fetchedVersion = (String) nextVersionVertex.property(GraphPropertyEnum.VERSION.getProperty()).value();
+                       String fetchedName = (String)nextVersionVertex.property(GraphPropertyEnum.NORMALIZED_NAME.getProperty()).value();
                        CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to checkout component {} with version {}. The component with name {} and version {} was fetched from graph as existing following version. ",
                                        toscaElementVertex.getMetadataProperty(GraphPropertyEnum.NORMALIZED_NAME).toString(), toscaElementVertex.getMetadataProperty(GraphPropertyEnum.VERSION).toString(), fetchedName, fetchedVersion);
                        result = Either.right(StorageOperationStatus.ENTITY_ALREADY_EXISTS);
@@ -992,6 +1010,11 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                }
                return result;
        }
+       
+       //TODO remove after jsonModelMigration
+       public boolean resolveToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
+               return fixToscaComponentName(vfInst, origCompMap);
+       }
 
        private boolean fixToscaComponentName(ComponentInstanceDataDefinition vfInst, Map<String, ToscaElement> origCompMap) {
                if (vfInst.getToscaComponentName() == null || vfInst.getToscaComponentName().isEmpty()) {
@@ -1010,7 +1033,7 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                                        CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to find orig component {} . Status is {}. ", origCompEither.right().value());
                                        return false;
                                }
-                               origComp =  origCompEither.left().value();
+                               origComp = origCompEither.left().value();
                                origCompMap.put(origCompUid, origComp);
                        } else {
                                origComp = origCompMap.get(origCompUid);
@@ -1097,7 +1120,7 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                                        Map<String, GroupInstanceDataDefinition> groupInstanceToCreate = new HashMap<>();
                                        for(GroupDataDefinition group:filteredGroups){
                                                CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "create new groupInstance  {} ", group.getName());
-                                               GroupInstanceDataDefinition groupInstance = buildGroupInstanceDataDefinition(group, vfInst);
+                                               GroupInstanceDataDefinition groupInstance = buildGroupInstanceDataDefinition(group, vfInst, instDeplArtifMap);
                                                List<String> artifactsUid = new ArrayList<>();
                                                List<String> artifactsId = new ArrayList<>();
                                                for (ArtifactDataDefinition artifact : instDeplArtifMap.values()) {
@@ -1413,6 +1436,13 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                return verticesToGetParameters;
        }
 
+
+       private String getNextCertifiedVersion(String version) {
+               String[] versionParts = version.split(VERSION_DELIMETER_REGEXP);
+               Integer nextMajorVersion = Integer.parseInt(versionParts[0]) + 1;
+               return nextMajorVersion + VERSION_DELIMETER + "0";
+       }
+       
        private String getNextVersion(String currVersion) {
                String[] versionParts = currVersion.split(VERSION_DELIMETER_REGEXP);
                Integer minorVersion = Integer.parseInt(versionParts[1]) + 1;
@@ -1435,4 +1465,72 @@ public class ToscaElementLifecycleOperation extends BaseOperation {
                }
                return false;
        }
+
+       public Either<ToscaElement,StorageOperationStatus> forceCerificationOfToscaElement(String toscaElementId, String modifierId, String ownerId, String currVersion) {
+               Either<GraphVertex, StorageOperationStatus> resultUpdate = null;
+               Either<ToscaElement, StorageOperationStatus> result = null;
+               GraphVertex toscaElement = null;
+               GraphVertex modifier = null;
+               GraphVertex owner;
+               try {
+                       Either<Map<String, GraphVertex>, TitanOperationStatus> getVerticesRes = titanDao.getVerticesByUniqueIdAndParseFlag(prepareParametersToGetVerticesForRequestCertification(toscaElementId, modifierId, ownerId));
+                       if (getVerticesRes.isRight()) {
+                               CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, FAILED_TO_GET_VERTICES, toscaElementId);
+                               result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(getVerticesRes.right().value()));
+                       }
+                       if (result == null) {
+                               toscaElement = getVerticesRes.left().value().get(toscaElementId);
+                               modifier = getVerticesRes.left().value().get(modifierId);
+                               owner = getVerticesRes.left().value().get(ownerId);
+
+                               StorageOperationStatus status = handleRelationsUponForceCertification(toscaElement, modifier, owner);
+                               if (status != StorageOperationStatus.OK) {
+                                       CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to handle relations on certification request for tosca element {}. Status is {}. ", toscaElement.getUniqueId(), status);
+                               }
+                       }
+                       if (result == null) {
+                               LifecycleStateEnum nextState = LifecycleStateEnum.CERTIFIED;
+
+                               toscaElement.addMetadataProperty(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
+                               toscaElement.addMetadataProperty(GraphPropertyEnum.VERSION, getNextCertifiedVersion(currVersion));
+
+                               resultUpdate = updateToscaElementVertexMetadataPropertiesAndJson(toscaElement);
+                               if (resultUpdate.isRight()) {
+                                       CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to set lifecycle for tosca elememt {} to state {}, error: {}", toscaElement.getUniqueId(), nextState, resultUpdate.right().value());
+                                       result = Either.right(resultUpdate.right().value());
+                               }
+                       }
+                       if (result == null) {
+                               ToscaElementOperation operation = getToscaElementOperation(toscaElement.getLabel());
+                               result = operation.getToscaElement(toscaElement.getUniqueId());
+                       }
+                       return result;
+
+               } catch (Exception e) {
+                       CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during request certification tosca element {}. {}", toscaElementId, e.getMessage());
+               }
+               return result;
+       }
+
+       private StorageOperationStatus handleRelationsUponForceCertification(GraphVertex toscaElement, GraphVertex modifier, GraphVertex owner) {
+
+                       StorageOperationStatus result = null;
+                       TitanOperationStatus status = titanDao.replaceEdgeLabel(owner.getVertex(), toscaElement.getVertex(), EdgeLabelEnum.STATE, EdgeLabelEnum.LAST_STATE);
+                       if (status != TitanOperationStatus.OK) {
+                               result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
+                       }
+                       if (result == null) {
+                               Map<EdgePropertyEnum, Object> properties = new EnumMap<>(EdgePropertyEnum.class);
+                               properties.put(EdgePropertyEnum.STATE, LifecycleStateEnum.CERTIFIED);
+                               status = titanDao.createEdge(modifier, toscaElement, EdgeLabelEnum.STATE, properties);
+                               if (status != TitanOperationStatus.OK) {
+                                       CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "failed to create edge. Status is {}", status);
+                                       result = DaoStatusConverter.convertTitanStatusToStorageStatus(status);
+                               }
+                       }
+                       if (result == null) {
+                               result = StorageOperationStatus.OK;
+                       }
+                       return result;
+       }
 }