2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.asdctool.migration.tasks.mig1710;
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.collect.Lists;
25 import fj.data.Either;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.MapUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
30 import org.openecomp.sdc.asdctool.migration.core.task.PostMigration;
31 import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler;
32 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
36 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
37 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
38 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
39 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
40 import org.openecomp.sdc.be.components.scheduledtasks.ComponentsCleanBusinessLogic;
41 import org.openecomp.sdc.be.config.Configuration;
42 import org.openecomp.sdc.be.config.ConfigurationManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
45 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
46 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
47 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
48 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
49 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
50 import org.openecomp.sdc.be.datatypes.enums.*;
51 import org.openecomp.sdc.be.impl.ComponentsUtils;
52 import org.openecomp.sdc.be.model.*;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
55 import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
56 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
57 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
58 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
59 import org.openecomp.sdc.common.log.wrappers.Logger;
60 import org.openecomp.sdc.exception.ResponseFormat;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.stereotype.Component;
64 import javax.annotation.PostConstruct;
66 import java.util.function.Predicate;
67 import java.util.stream.Collectors;
70 public class UpgradeMigration1710 implements PostMigration {
72 private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid";
74 private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid";
76 private static final String UNKNOWN = "UNKNOWN";
78 private static final String CHECKOUT_MESSAGE = "checkout upon upgrade migration";
80 private static final String FAILED_TO_CHANGE_STATE_OF_COMPONENT = "Failed to change state of component with name {}, invariantUUID {}, version {} to {}. ";
82 private static final String FAILED_TO_UPGRADE_COMPONENT = "Failed to upgrade {} with name {}, invariantUUID {}, version {}. Operation {}. The reason for failure: {}. ";
84 private static final String UPGRADE_COMPONENT_SUCCEEDED = "Upgrade of {} with name {}, invariantUUID {}, version {} finished successfully. ";
86 private static final String UPGRADE_VFS_FAILED = "Upgrade VFs upon upgrade migration 1710 process failed. ";
88 private static final Logger log = Logger.getLogger(UpgradeMigration1710.class);
90 private static final String ALLOTTED_RESOURCE_NAME = "Allotted Resource";
92 //as per US 397775, only node type upgrade should be enabled,
93 // to support resource and service upgrade, this flag should be reverted
94 private boolean isNodeTypesSupportOnly = true;
97 private JanusGraphDao janusGraphDao;
100 private ToscaOperationFacade toscaOperationFacade;
103 private LifecycleBusinessLogic lifecycleBusinessLogic;
106 private IUserAdminOperation userAdminOperation;
109 private ResourceBusinessLogic resourceBusinessLogic;
112 private ServiceBusinessLogic serviceBusinessLogic;
115 private CsarOperation csarOperation;
118 private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
121 private ComponentsUtils componentsUtils;
124 private ComponentsCleanBusinessLogic componentsCleanBusinessLogic;
126 private XlsOutputHandler outputHandler = new XlsOutputHandler(null, "UpgradeMigration1710report","COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION");
128 private User user = null;
130 private final LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction(CHECKOUT_MESSAGE, LifecycleChanceActionEnum.UPGRADE_MIGRATION);
132 private final Map<String, GraphVertex> latestGenericTypes = new HashMap<>();
134 private final Map<String, String> latestOriginResourceVersions = new HashMap<>();
136 private final Map<String, org.openecomp.sdc.be.model.Component> upgradedNodeTypesMap = new HashMap<>();
138 private List<String> nodeTypes;
140 private List<String> proxyServiceContainers = new ArrayList<>();
142 private List<String> vfAllottedResources = new ArrayList<>();
144 private List<String> allottedVfContainers = new ArrayList<>();
146 private boolean isVfcUpgradeRequired = false;
148 private boolean skipIfUpgradeVfFailed = true;
150 private boolean isAllottedAndProxySupported = true;
152 private String userId;
154 private boolean isCleanupLocked = false;
156 private int markedAsDeletedResourcesCnt = 0;
158 private int markedAsDeletedServicesCnt = 0;
160 //how many components can be deleted once
161 private int maxDeleteComponents = 10;
163 private boolean enableAutoHealing = true;
165 //map for tracing checked out resources that keep in place after upgrade failure
166 private HashMap<String, String> certifiedToNextCheckedOutUniqueId = new HashMap<>();
168 private int deleteLockTimeoutInSeconds = 60;
170 private boolean isLockSucceeded = false;
172 /***********************************************/
175 void setNodeTypesSupportOnly(boolean nodeTypesSupportOnly) {
176 isNodeTypesSupportOnly = nodeTypesSupportOnly;
180 void setUser(User user) {
185 void setMarkedAsDeletedResourcesCnt(int markedAsDeletedResourcesCnt) {
186 this.markedAsDeletedResourcesCnt = markedAsDeletedResourcesCnt;
190 void setMarkedAsDeletedServicesCnt(int markedAsDeletedServicesCnt) {
191 this.markedAsDeletedServicesCnt = markedAsDeletedServicesCnt;
196 Configuration config = ConfigurationManager.getConfigurationManager().getConfiguration();
197 isVfcUpgradeRequired = !config.getSkipUpgradeVSPsFlag();
198 skipIfUpgradeVfFailed = config.getSkipUpgradeFailedVfs();
199 isAllottedAndProxySupported = config.getSupportAllottedResourcesAndProxyFlag();
200 deleteLockTimeoutInSeconds = config.getDeleteLockTimeoutInSeconds();
201 maxDeleteComponents = config.getMaxDeleteComponents();
203 String toscaConformanceLevel = config.getToscaConformanceLevel();
204 Map<String, List<String>> resourcesForUpgrade = config.getResourcesForUpgrade();
205 nodeTypes = resourcesForUpgrade.get(toscaConformanceLevel);
206 enableAutoHealing = config.isEnableAutoHealing();
207 userId = config.getAutoHealingOwner();
208 isNodeTypesSupportOnly = true;
212 public String description() {
213 return "Upgrade migration 1710 - post migration task, which is dedicated to upgrade all latest certified (and not checked out) Node types, VFs and Services. ";
218 UPGRADED_AS_INSTANCE,
223 public MigrationResult migrate() {
224 MigrationResult migrationResult = new MigrationResult();
225 //stop the upgrade if this ask is disabled
226 if (!enableAutoHealing) {
227 log.warn("Upgrade migration 1710 task is disabled");
228 migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED);
229 return migrationResult ;
231 log.info("Starting upgrade migration 1710 process. ");
232 boolean result = true;
235 //lock cleanup node to avoid BE to delete marked components
236 //while the auto-healing process is running
237 isLockSucceeded = isNodeTypesSupportOnly ? true : isLockDeleteOperationSucceeded();
239 if (!isLockSucceeded) {
241 log.error("Cleanup node can't be locked. Upgrade migration failed");
244 Either<User, ActionStatus> userReq = userAdminOperation.getUserData(userId, false);
245 if (userReq.isRight()) {
247 log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value());
249 user = userReq.left().value();
250 log.info("User {} will perform upgrade operation", user.getUserId());
254 result = upgradeNodeTypes();
256 if (!isNodeTypesSupportOnly && result) {
257 result = upgradeTopologyTemplates();
260 catch (Exception e) {
262 log.error("Error occurred during the migration: ", e);
264 MigrationResult.MigrationStatus status = result ?
265 MigrationResult.MigrationStatus.COMPLETED : MigrationResult.MigrationStatus.FAILED;
267 migrationResult.setMigrationStatus(status);
269 return migrationResult;
272 private boolean upgradeTopologyTemplates() {
275 upgradeProxyServiceContainers();
276 upgradeAllottedVFs();
277 upgradeAllottedVfContainers();
283 private void cleanup(MigrationResult.MigrationStatus status) {
284 if (status == MigrationResult.MigrationStatus.COMPLETED ) {
285 log.info("Upgrade migration 1710 has been successfully finished. ");
286 janusGraphDao.commit();
288 log.info("Upgrade migration 1710 was failed. ");
289 janusGraphDao.rollback();
291 outputHandler.writeOutputAndCloseFile();
292 if (!isNodeTypesSupportOnly && isLockSucceeded) {
293 //delete rest of components if their upgrade failed
294 markedAsDeletedResourcesCnt = maxDeleteComponents;
295 deleteResourcesIfLimitIsReached();
296 markedAsDeletedServicesCnt = maxDeleteComponents;
297 deleteServicesIfLimitIsReached();
298 unlockDeleteOperation();
302 void upgradeServices(List<String> uniqueIDs, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade, final String containerName) {
303 log.info("Starting upgrade {} upon upgrade migration 1710 process. ", containerName);
304 for (String currUid : uniqueIDs) {
305 upgradeServiceAndCommitIfNeeded(currUid, shouldUpgrade);
307 log.info("Upgrade {} upon upgrade migration 1710 process is finished. ", containerName);
310 private void upgradeServiceAndCommitIfNeeded(String currUid, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade) {
311 boolean result = true;
313 result = handleService(currUid, shouldUpgrade);
314 } catch (Exception e) {
316 log.error("Failed to upgrade service with uniqueId {} due to a reason {}. ", currUid, e.getMessage());
317 log.debug("Failed to upgrade service with uniqueId {}", currUid, e);
321 log.info("Service upgrade finished successfully: uniqueId {} ", currUid);
322 janusGraphDao.commit();
325 log.error("Failed to upgrade service with uniqueId {} ", currUid);
326 janusGraphDao.rollback();
328 markCheckedOutServiceAsDeletedIfUpgradeFailed(currUid, result);
332 private void upgradeAllottedVfContainers() {
333 upgradeServices(allottedVfContainers, component -> true, "proxy " + allottedVfContainers.size() + " service containers");
336 private void upgradeServices() {
337 Either<List<String>, JanusGraphOperationStatus> getServicesRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.SERVICE);
338 if (getServicesRes.isRight()) {
339 log.error("Failed to retrieve the latest certified service versions");
342 upgradeServices(getServicesRes.left().value(), this::shouldUpgrade, "services");
345 private void upgradeProxyServiceContainers() {
346 upgradeServices(proxyServiceContainers, component -> true, "proxy service containers");
349 private boolean handleService(String uniqueId, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade) {
350 log.info("Starting upgrade Service with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
351 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getServiceRes = toscaOperationFacade.getToscaElement(uniqueId);
352 if(getServiceRes.isRight()){
353 log.error("Failed to upgrade service with uniqueId {} due to {}. ", uniqueId, getServiceRes.right().value());
354 outputHandler.addRecord(ComponentTypeEnum.SERVICE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getServiceRes.right().value());
357 String derivedFromGenericType = getServiceRes.left().value().getDerivedFromGenericType();
358 log.debug("derivedFromGenericType: {}", derivedFromGenericType );
359 if (derivedFromGenericType == null) {
360 //malformed field value, upgrade required
361 return upgradeService(getServiceRes.left().value());
363 if(!latestGenericTypes.containsKey(derivedFromGenericType)){
364 Either<List<GraphVertex>, JanusGraphOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType);
365 if(getDerivedRes.isRight()){
366 log.error(FAILED_TO_UPGRADE_COMPONENT, getServiceRes.left().value().getComponentType().getValue(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getVersion(), "findDerivedResources", getDerivedRes.right().value());
367 outputHandler.addRecord( getServiceRes.left().value().getComponentType().name(),getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value());
370 latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
372 if(latestVersionExists(latestGenericTypes.get(derivedFromGenericType), getServiceRes.left().value().getDerivedFromGenericVersion())){
373 return upgradeService(getServiceRes.left().value());
375 if(!collectLatestOriginResourceVersions(getServiceRes.left().value())){
378 if(shouldUpgrade.test(getServiceRes.left().value())){
379 return upgradeService(getServiceRes.left().value());
381 outputHandler.addRecord(getServiceRes.left().value().getComponentType().name(), getServiceRes.left().value().getName(), getServiceRes.left().value().getInvariantUUID(), getServiceRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.NOT_UPGRADED);
385 private boolean collectLatestOriginResourceVersions(org.openecomp.sdc.be.model.Component component) {
386 if (CollectionUtils.isNotEmpty(component.getComponentInstances())) {
387 for (ComponentInstance instance : component.getComponentInstances()) {
388 if (instance.getOriginType() != OriginTypeEnum.ServiceProxy && !latestOriginResourceVersions.containsKey(instance.getToscaComponentName()) && !addComponent(component, instance)) {
396 private boolean addComponent(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) {
397 VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name());
398 Either<Resource, StorageOperationStatus> getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata);
399 if (getOriginRes.isRight()) {
400 log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "toscaOperationFacade.getLatestCertifiedByToscaResourceName", getOriginRes.right().value());
401 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getOriginRes.right().value());
404 latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion());
408 private boolean shouldUpgrade(org.openecomp.sdc.be.model.Component component) {
409 if(CollectionUtils.isNotEmpty(component.getComponentInstances())) {
410 if (containsProxyOrAllottedVF(component)) {
413 for(ComponentInstance instance : component.getComponentInstances()){
414 if(isGreater(latestOriginResourceVersions.get(instance.getToscaComponentName()), instance.getComponentVersion())){
415 log.info("The service with name {}, invariantUUID {}, version {}, contains instance {} from outdated version of origin {} {} , than the service should be upgraded. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName(), instance.getComponentName(), instance.getComponentVersion());
423 private boolean containsProxyOrAllottedVF(org.openecomp.sdc.be.model.Component component) {
424 return !component.getComponentInstances()
426 .filter(i->isProxyOrAllottedVF(i, component.getUniqueId()))
427 .collect(Collectors.toList()).isEmpty();
430 private boolean isProxyOrAllottedVF(ComponentInstance instance, String uniqueId) {
431 if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) {
432 keepProxyServiceContainerIfSupported(uniqueId);
435 if (isAllottedResource(instance.getActualComponentUid())) {
436 keepAllottedVfContainerIfSupported(uniqueId);
442 private void keepAllottedVfContainerIfSupported(final String uniqueId) {
443 if (isAllottedAndProxySupported && !allottedVfContainers.contains(uniqueId)) {
444 log.info("Add a service with uniqueId {} to allotted VF containers container list", uniqueId);
445 allottedVfContainers.add(uniqueId);
449 private void keepProxyServiceContainerIfSupported(final String uniqueId) {
450 if (isAllottedAndProxySupported && !proxyServiceContainers.contains(uniqueId)) {
451 log.info("Add a service with uniqueId {} to proxy service container list", uniqueId);
452 proxyServiceContainers.add(uniqueId);
456 private boolean upgradeService(org.openecomp.sdc.be.model.Component service) {
457 String serviceName = service.getName();
458 String serviceUuid = service.getUUID();
459 log.info("Starting upgrade Service with name {}, invariantUUID {}, version {} upon upgrade migration 1710 process. ", serviceName, service.getInvariantUUID(), service.getVersion());
460 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = checkOutComponent(service);
461 if (checkouRes.isRight()) {
462 log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "lifecycleBusinessLogic.changeComponentState", checkouRes.right().value().getFormattedMessage());
463 outputHandler.addRecord(service.getComponentType().name(), serviceName, serviceUuid, service.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage());
466 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateCompositionRes = updateComposition(checkouRes.left().value());
467 if (updateCompositionRes.isRight()) {
468 log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage());
469 outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage());
472 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkouRes.left().value());
473 if (certifyRes.isRight()) {
474 log.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "performFullCertification", certifyRes.right().value().getFormattedMessage());
475 outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage());
478 outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), serviceUuid, checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED);
482 private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateComposition(org.openecomp.sdc.be.model.Component component) {
483 if (component != null && component.getComponentInstances() != null) {
484 Either<ComponentInstance, ResponseFormat> upgradeInstanceRes;
485 for (ComponentInstance instance : component.getComponentInstances()) {
486 upgradeInstanceRes = upgradeInstance(component, instance);
487 if (upgradeInstanceRes.isRight()) {
488 log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "upgradeInstance", upgradeInstanceRes.right().value().getFormattedMessage());
489 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), upgradeInstanceRes.right().value().getFormattedMessage());
490 return Either.right(upgradeInstanceRes.right().value());
494 return Either.left(component);
497 private Either<ComponentInstance, ResponseFormat> upgradeInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) {
498 log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName());
499 ComponentInstance newComponentInstance = new ComponentInstance(instance);
500 if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) {
501 return upgradeServiceProxyInstance(component, instance, newComponentInstance);
503 return upgradeResourceInstance(component, instance, newComponentInstance);
506 private Either<ComponentInstance, ResponseFormat> upgradeResourceInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) {
508 log.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName());
509 Either<ComponentInstance, ResponseFormat> upgradeInstanceRes = null;
510 VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name());
511 Either<Resource, StorageOperationStatus> getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata);
512 if(getOriginRes.isRight()){
513 log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ",
514 component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value());
515 upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType())));
517 if(upgradeInstanceRes == null) {
518 copyComponentNameAndVersionToNewInstance(newComponentInstance, getOriginRes.left().value());
520 if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){
521 upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance);
523 if((upgradeInstanceRes == null || upgradeInstanceRes.isLeft()) && isAllottedResource(instance.getComponentUid()) && MapUtils.isNotEmpty(component.getComponentInstancesProperties())){
524 ComponentInstance instanceToUpdate = upgradeInstanceRes == null ? instance : upgradeInstanceRes.left().value();
525 upgradeInstanceRes = Either.left(updateServiceUuidProperty(component, instanceToUpdate, component.getComponentInstancesProperties().get(instance.getUniqueId())));
528 //upgrade nodes contained by CVFC
529 if(upgradeInstanceRes == null && isVfcUpgradeRequired && newComponentInstance.getOriginType() == OriginTypeEnum.CVFC &&
530 !upgradeVf(getOriginRes.left().value().getUniqueId(), false, true)) {
531 upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
533 if(upgradeInstanceRes == null){
534 upgradeInstanceRes = Either.left(instance);
536 log.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ",
537 component.getComponentType().getValue(), instance.getName());
538 return upgradeInstanceRes;
541 private void copyComponentNameAndVersionToNewInstance(ComponentInstance newComponentInstance, Resource originResource) {
542 newComponentInstance.setComponentName(originResource.getName());
543 newComponentInstance.setComponentUid(originResource.getUniqueId());
544 newComponentInstance.setComponentVersion(originResource.getVersion());
545 newComponentInstance.setToscaComponentName(originResource.getToscaResourceName());
548 private ComponentInstance updateServiceUuidProperty(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, List<ComponentInstanceProperty> instanceProperties){
549 if(isAllottedResource(instance.getComponentUid()) && instanceProperties != null){
550 Optional<ComponentInstanceProperty> propertyUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_UUID_RPOPERTY)).findFirst();
551 Optional<ComponentInstanceProperty> propertyInvariantUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_INVARIANT_UUID_RPOPERTY)).findFirst();
552 if(propertyUuid.isPresent() && propertyInvariantUuid.isPresent()){
553 String serviceInvariantUUID = propertyInvariantUuid.get().getValue();
554 Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(serviceInvariantUUID);
555 if (getLatestOriginServiceRes.isRight()) {
558 propertyUuid.get().setValue((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID));
559 componentInstanceBusinessLogic.createOrUpdatePropertiesValues(component.getComponentType(), component.getUniqueId(), instance.getUniqueId(), Lists.newArrayList(propertyUuid.get()), user.getUserId())
561 .forEach(e -> log.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName()));
567 private boolean isAllottedResource(String uniqueId){
568 ComponentParametersView filters = new ComponentParametersView(true);
569 filters.setIgnoreCategories(false);
570 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaElement(uniqueId, filters);
571 if(getResourceRes.isRight()){
574 if(getResourceRes.left().value().getCategories() != null && getResourceRes.left().value().getCategories().get(0)!= null){
575 return ALLOTTED_RESOURCE_NAME.equals(getResourceRes.left().value().getCategories().get(0).getName());
580 private boolean isAllottedVf(org.openecomp.sdc.be.model.Component component){
581 if(component.getComponentType() != ComponentTypeEnum.RESOURCE || ((Resource)component).getResourceType() != ResourceTypeEnum.VF){
584 return isAllottedResource(component.getUniqueId());
587 private Either<ComponentInstance, ResponseFormat> upgradeServiceProxyInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) {
588 Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(instance.getSourceModelInvariant());
589 if (getLatestOriginServiceRes.isRight()) {
590 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(getLatestOriginServiceRes.right().value()), instance.getOriginType().getComponentType())));
592 ModelConverter.getVertexType(instance.getOriginType().name());
593 Either<Resource, StorageOperationStatus> getOriginRes = toscaOperationFacade.getLatestByName(instance.getComponentName());
594 if(getOriginRes.isRight()){
595 log.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ",
596 component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value());
597 return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType())));
599 newComponentInstance.setComponentUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
600 return changeAssetVersion(component, instance, newComponentInstance);
603 private Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestCertifiedService(String invariantUUID) {
605 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
606 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
607 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
608 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
609 propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
610 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
611 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
613 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
616 private Either<ComponentInstance, ResponseFormat> changeAssetVersion(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance instance, ComponentInstance newComponentInstance) {
617 return componentInstanceBusinessLogic.changeComponentInstanceVersion(ComponentTypeEnum.SERVICE_PARAM_NAME, containerComponent.getUniqueId(), instance.getUniqueId(), user.getUserId(), newComponentInstance);
620 private boolean upgradeNodeTypes() {
621 log.info("Starting upgrade node types upon upgrade migration 1710 process. ");
622 if (nodeTypes != null && !nodeTypes.isEmpty()) {
623 Either<List<String>, JanusGraphOperationStatus> getRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.NODE_TYPE, ComponentTypeEnum.RESOURCE);
624 if (getRes.isRight()) {
627 for (String toscaResourceName : nodeTypes) {
628 if (!upgradeNodeType(toscaResourceName, getRes.left().value())) {
634 log.info("No node types for upgrade are configured");
639 private boolean upgradeNodeType(String toscaResourceName, List<String> allNodeTypes) {
640 Either<List<GraphVertex>, StorageOperationStatus> status = getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
641 if (status.isRight()) {
642 log.error("Failed to find node type {} ", toscaResourceName);
645 List<GraphVertex> vList = status.left().value();
646 for (GraphVertex vertex : vList) {
647 StorageOperationStatus updateRes = upgradeNodeType(vertex, allNodeTypes);
648 if (updateRes != StorageOperationStatus.OK) {
655 private boolean upgradeVFs() {
656 log.info("Starting upgrade VFs upon upgrade migration 1710 process. ");
657 Either<List<String>, JanusGraphOperationStatus> getVfsRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.RESOURCE);
658 if (getVfsRes.isRight()) {
659 log.info(UPGRADE_VFS_FAILED);
662 return upgradeVFs(getVfsRes.left().value(), false);
665 private boolean upgradeAllottedVFs() {
666 log.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size());
667 return upgradeVFs(vfAllottedResources, true);
670 boolean upgradeVFs(List<String> resourceList, boolean isAllottedVfsUpgrade) {
671 for (String currUid : resourceList) {
672 boolean result = true;
674 result = upgradeVf(currUid, isAllottedVfsUpgrade, false);
675 if (!result && !skipIfUpgradeVfFailed) {
678 } catch (Exception e) {
679 log.error("The exception {} occurred upon upgrade VFs. ", e.getMessage());
680 log.debug("The exception occurred upon upgrade VFs:", e);
682 if (!skipIfUpgradeVfFailed) {
688 log.info("Resource upgrade finished successfully: uniqueId {} ", currUid);
689 janusGraphDao.commit();
692 log.error("Failed to upgrade resource with uniqueId {} ", currUid);
693 janusGraphDao.rollback();
695 markCheckedOutResourceAsDeletedIfUpgradeFailed(currUid, result);
698 log.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. ");
702 private boolean upgradeVf(String uniqueId, boolean allottedVfsUpgrade, boolean isInstance) {
703 log.info("Starting upgrade VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
704 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getRes = toscaOperationFacade.getToscaElement(uniqueId);
705 if (getRes.isRight()) {
706 log.debug("Failed to fetch VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
707 outputHandler.addRecord(ComponentTypeEnum.RESOURCE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getRes.right().value());
710 if(!allottedVfsUpgrade && isAllottedVf(getRes.left().value())){
711 keepAllottedResourceIfSupported(uniqueId);
714 if (StringUtils.isNotEmpty(getRes.left().value().getCsarUUID())) {
715 log.info("Going to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID());
716 Either<String, StorageOperationStatus> latestVersionRes = csarOperation.getCsarLatestVersion(getRes.left().value().getCsarUUID(), user);
717 if (latestVersionRes.isRight()) {
718 log.debug("Failed to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID());
719 outputHandler.addRecord(getRes.left().value().getComponentType().name(), getRes.left().value().getName(), getRes.left().value().getUUID(), getRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), latestVersionRes.right().value());
722 if (isGreater(latestVersionRes.left().value(), getRes.left().value().getCsarVersion())) {
723 return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes.left().value(), isInstance);
725 if (isVfcUpgradeRequired) {
726 return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance);
728 log.warn("Warning: No need to upgrade VF with name {}, invariantUUID {}, version {} and VSP version {}. No new version of VSP. ", getRes.left().value().getName(), getRes.left().value().getInvariantUUID(), getRes.left().value().getVersion(), getRes.left().value().getCsarVersion());
732 return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance);
736 private void keepAllottedResourceIfSupported(final String uniqueId) {
737 if (isAllottedAndProxySupported && !vfAllottedResources.contains(uniqueId)) {
738 log.info("Add a resource with uniqueId {} to allotted resource list", uniqueId);
739 vfAllottedResources.add(uniqueId);
743 private boolean upgradeVfWithLatestVsp(org.openecomp.sdc.be.model.Component vf, String latestVersion, boolean isInstance) {
744 log.info("Starting upgrade vf with name {}, invariantUUID {}, version {} and latest VSP version {} upon upgrade migration 1710 process. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion);
745 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = checkOutComponent(vf);
746 if (checkouRes.isRight()) {
747 outputHandler.addRecord(vf.getComponentType().name(), vf.getName(), vf.getUUID(), vf.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage());
750 Resource resourceToUpdate = new Resource(((Resource) checkouRes.left().value()).getComponentMetadataDefinition());
751 resourceToUpdate.setDerivedFromGenericType(((Resource) checkouRes.left().value()).getDerivedFromGenericType());
752 resourceToUpdate.setDerivedFromGenericVersion(((Resource) checkouRes.left().value()).getDerivedFromGenericVersion());
753 resourceToUpdate.setCsarVersion(Double.toString(Double.parseDouble(latestVersion)));
754 resourceToUpdate.setCategories(((Resource)checkouRes.left().value()).getCategories());
756 Resource updateResourceFromCsarRes = resourceBusinessLogic.validateAndUpdateResourceFromCsar(resourceToUpdate, user, null, null, resourceToUpdate.getUniqueId());
757 } catch(ByResponseFormatComponentException e){
758 outputHandler.addRecord(resourceToUpdate.getComponentType().name(), resourceToUpdate.getName(), resourceToUpdate.getUUID(), resourceToUpdate.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), e.getResponseFormat().getFormattedMessage());
759 log.info("Failed to update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion);
762 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkouRes.left().value());
763 if (certifyRes.isRight()) {
764 log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), LifeCycleTransitionEnum.CERTIFY);
765 outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage());
768 log.info("Full certification of vf with name {}, invariantUUID {}, version {} finished . ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersion);
769 outputHandler.addRecord(certifyRes.left().value().getComponentType().name(), certifyRes.left().value().getName(), certifyRes.left().value().getUUID(), certifyRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), getVfUpgradeStatus(true, isInstance));
773 private boolean upgradeComponentWithLatestGeneric(org.openecomp.sdc.be.model.Component component, boolean isInstance) {
774 String derivedFromGenericType = component.getDerivedFromGenericType();
775 String derivedFromGenericVersion = component.getDerivedFromGenericVersion();
776 org.openecomp.sdc.be.model.Component updatedComponent = component;
777 if (failedToFindDerivedResourcesOfNodeType(component, derivedFromGenericType, derivedFromGenericVersion)) {
780 if (StringUtils.isEmpty(derivedFromGenericType) ||
781 latestVersionExists(latestGenericTypes.get(derivedFromGenericType), derivedFromGenericVersion) ||
782 isVfcUpgradeRequired ||
783 isAllottedAndProxySupported) {
784 if (StringUtils.isNotEmpty(derivedFromGenericType)) {
785 log.info("Newer version {} of derived from generic type {} exists. ", latestGenericTypes.get(derivedFromGenericType).getJsonMetadataField(JsonPresentationFields.VERSION), derivedFromGenericType);
788 log.info("The vf resource with name {}, invariantUUID {}, version {}, has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion());
790 updatedComponent = checkOutAndCertifyComponent(component);
792 log.info("The version {} of derived from generic type {} is up to date. No need to upgrade component with name {}, invariantUUID {} and version {}. ", latestGenericTypes.get(derivedFromGenericType), derivedFromGenericType, component.getName(), component.getInvariantUUID(), component.getVersion());
794 if (updatedComponent != null) {
795 log.info(UPGRADE_COMPONENT_SUCCEEDED, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion());
796 outputHandler.addRecord(updatedComponent.getComponentType().name(), updatedComponent.getName(), updatedComponent.getUUID(), updatedComponent.getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(),
797 getVfUpgradeStatus(!updatedComponent.equals(component), isInstance));
802 private org.openecomp.sdc.be.model.Component checkOutAndCertifyComponent(org.openecomp.sdc.be.model.Component component) {
804 log.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion());
805 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkoutRes = checkOutComponent(component);
806 if (checkoutRes.isRight()) {
807 log.error(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CHECKOUT);
808 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkoutRes.right().value().getFormattedMessage());
812 if (!updateCompositionFailed(component, checkoutRes.left().value())) {
815 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkoutRes.left().value());
816 if (certifyRes.isRight()) {
817 log.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "performFullCertification", certifyRes.right().value());
818 outputHandler.addRecord(checkoutRes.left().value().getComponentType().name(), checkoutRes.left().value().getName(), checkoutRes.left().value().getInvariantUUID(), checkoutRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), certifyRes.right().value().getFormattedMessage());
821 return certifyRes.left().value();
824 private boolean failedToFindDerivedResourcesOfNodeType(org.openecomp.sdc.be.model.Component component, String derivedFromGenericType, String derivedFromGenericVersion) {
825 if (StringUtils.isNotEmpty(derivedFromGenericType) && !latestGenericTypes.containsKey(derivedFromGenericType)) {
826 log.info("Starting upgrade vf with name {}, invariantUUID {}, version {}, latest derived from generic type {}, latest derived from generic version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType, derivedFromGenericVersion);
827 log.info("Starting to fetch latest generic node type {}. ", derivedFromGenericType);
828 Either<List<GraphVertex>, JanusGraphOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType);
829 if (getDerivedRes.isRight()) {
830 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value());
831 log.info("Failed to upgrade component with name {}, invariantUUID {}, version {} and latest generic. Status is {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType);
834 latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
839 private boolean updateCompositionFailed(org.openecomp.sdc.be.model.Component component, org.openecomp.sdc.be.model.Component checkoutResource) {
840 //try to update included VFCs, if it is either required as per configuration or an allotted resource
841 if ((isVfcUpgradeRequired && CollectionUtils.isNotEmpty(checkoutResource.getComponentInstances())) || isAllottedAndProxySupported) {
842 log.info("VFC upgrade is required: updating components of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion());
843 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateCompositionRes = updateComposition(checkoutResource);
844 if (updateCompositionRes.isRight()) {
845 if (log.isErrorEnabled()) {
846 log.error(FAILED_TO_UPGRADE_COMPONENT, checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getInvariantUUID(), checkoutResource.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage());
848 outputHandler.addRecord(checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getUUID(), checkoutResource.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage());
855 private StorageOperationStatus upgradeNodeType(GraphVertex nodeTypeV, List<String> allCertifiedUids) {
856 StorageOperationStatus result = StorageOperationStatus.OK;
857 log.info("Starting upgrade node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION));
858 log.info("Starting to find derived to for node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION));
859 Either<List<GraphVertex>, JanusGraphOperationStatus> parentResourceRes = janusGraphDao
860 .getParentVertecies(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseMetadata);
861 if (parentResourceRes.isRight() && parentResourceRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
862 return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentResourceRes.right().value());
865 List<GraphVertex> derivedResourcesUid = getAllDerivedGraphVertices(allCertifiedUids, parentResourceRes);
866 String uniqueId = (String) nodeTypeV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID);
868 Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getRes = toscaOperationFacade.getToscaElement(uniqueId);
869 if (getRes.isRight()) {
870 log.info("failed to fetch element with uniqueId {} ", uniqueId);
871 return getRes.right().value();
874 Resource nodeType = (Resource)getRes.left().value();
875 if (!upgradedNodeTypesMap.containsKey(nodeType.getToscaResourceName()) && !nodeTypes.stream().anyMatch(p -> p.equals(nodeType.getToscaResourceName()))
876 && !isNodeTypeUpgradeSucceeded((Resource) getRes.left().value())) {
877 return StorageOperationStatus.GENERAL_ERROR;
879 for (GraphVertex chV : derivedResourcesUid) {
880 result = upgradeNodeType(chV, allCertifiedUids);
881 log.info("Upgrade node type with name {}, invariantUUID {}, version {} has been finished with the status {}", chV.getMetadataProperty(GraphPropertyEnum.NAME), chV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), chV.getMetadataProperty(GraphPropertyEnum.VERSION), result);
886 private boolean isNodeTypeUpgradeSucceeded(Resource nodeType) {
887 log.info("Starting to perform check out of node type with name {}, invariantUUID {}, version {}. ", nodeType.getName(), nodeType.getInvariantUUID(), nodeType.getVersion());
888 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes =
889 lifecycleBusinessLogic.changeComponentState(nodeType.getComponentType(), nodeType.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
890 if (checkouRes.isRight()) {
891 log.info("Failed to check out node type with name {}, invariantUUID {} due to {}", nodeType.getName(), nodeType.getInvariantUUID(), checkouRes.right().value());
894 if (performFullCertification(checkouRes.left().value()).isLeft()) {
895 upgradedNodeTypesMap.put(nodeType.getToscaResourceName(), checkouRes.left().value());
896 janusGraphDao.commit();
902 private List<GraphVertex> getAllDerivedGraphVertices(List<String> allCertifiedUids, Either<List<GraphVertex>, JanusGraphOperationStatus> parentResources) {
903 List<GraphVertex> derivedResourcesUid = new ArrayList<>();
905 if (parentResources.isLeft()) {
906 for (GraphVertex chV : parentResources.left().value()) {
907 Optional<String> op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny();
908 if (op.isPresent()) {
909 derivedResourcesUid.add(chV);
913 return derivedResourcesUid;
916 private Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> performFullCertification(org.openecomp.sdc.be.model.Component component) {
917 log.info("Starting to perform full certification of {} with name {}, invariantUUID {}, version {}. ",
918 component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion());
920 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFICATION_REQUEST, changeInfo, true, false);
921 if (changeStateEither.isRight()) {
922 log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFICATION_REQUEST);
923 return changeStateEither;
925 changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.START_CERTIFICATION, changeInfo, true, false);
926 if (changeStateEither.isRight()) {
927 log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.START_CERTIFICATION);
928 return changeStateEither;
930 changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, true, false);
931 if (changeStateEither.isRight()) {
932 log.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY);
934 log.info("Full certification of {} with name {}, invariantUUID {}, version {} finished successfully",
935 changeStateEither.left().value().getComponentType().getValue(), changeStateEither.left().value().getName(),
936 changeStateEither.left().value().getInvariantUUID(), changeStateEither.left().value().getVersion());
938 return changeStateEither;
941 private Either<List<GraphVertex>, JanusGraphOperationStatus> findDerivedResources(String parentResource) {
942 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
943 propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
945 propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, parentResource);
946 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
949 .getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
952 private boolean latestVersionExists(GraphVertex latestDerivedFrom, String currentVersion) {
953 return isGreater((String) latestDerivedFrom.getJsonMetadataField(JsonPresentationFields.VERSION), currentVersion);
956 private boolean isGreater(String latestVersion, String currentVersion) {
957 if (latestVersion != null && currentVersion == null) {
960 if (latestVersion == null) {
963 return Double.parseDouble(latestVersion) > Double.parseDouble(currentVersion);
966 private Either<List<String>, JanusGraphOperationStatus> getAllLatestCertifiedComponentUids(VertexTypeEnum vertexType, ComponentTypeEnum componentType) {
967 log.info("Starting to fetch all latest certified not checked out components with type {} upon upgrade migration 1710 process", componentType);
968 Either<List<String>, JanusGraphOperationStatus> result = null;
969 Map<String, String> latestCertifiedMap = new HashMap<>();
970 Map<String, String> latestNotCertifiedMap = new HashMap<>();
972 Either<List<GraphVertex>, JanusGraphOperationStatus> getComponentsRes = getAllLatestComponents(vertexType, componentType);
973 if (getComponentsRes.isRight() && getComponentsRes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
974 log.error("Failed to fetch all latest certified not checked out components with type {}. Status is {}. ", componentType, getComponentsRes.right().value());
975 result = Either.right(getComponentsRes.right().value());
977 if (getComponentsRes.isRight()) {
978 result = Either.left(new ArrayList<>());
980 if (result == null) {
981 for (GraphVertex component : getComponentsRes.left().value()) {
982 String invariantUUID = (String) component.getJsonMetadataField(JsonPresentationFields.INVARIANT_UUID);
983 if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
984 latestCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
986 latestNotCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
989 result = Either.left(latestCertifiedMap.entrySet().stream().filter(e -> !latestNotCertifiedMap.containsKey(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList()));
994 private Either<List<GraphVertex>, JanusGraphOperationStatus> getAllLatestComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) {
996 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
997 propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
998 propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
1000 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1001 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1002 if (vertexType == VertexTypeEnum.TOPOLOGY_TEMPLATE && componentType == ComponentTypeEnum.RESOURCE) {
1003 propertiesNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
1005 return janusGraphDao
1006 .getByCriteria(vertexType, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
1009 private Either<List<GraphVertex>, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
1011 Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1012 Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1014 propertiesToMatch.put(property, nodeName);
1015 propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1017 Either<List<GraphVertex>, JanusGraphOperationStatus> highestResources = janusGraphDao
1018 .getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
1019 if (highestResources.isRight()) {
1020 JanusGraphOperationStatus status = highestResources.right().value();
1021 log.debug("Failed to fetch resource with name {}. Status is {} ", nodeName, status);
1022 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
1024 List<GraphVertex> resources = highestResources.left().value();
1025 List<GraphVertex> result = new ArrayList<>();
1026 for (GraphVertex component : resources) {
1027 if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
1028 result.add(component);
1031 return Either.left(result);
1034 private void deleteMarkedComponents(NodeTypeEnum componentType, int toBeDeleted) {
1035 Map<NodeTypeEnum, Either<List<String>, ResponseFormat>> cleanComponentsResult;
1036 List<NodeTypeEnum> cleanComponents = new ArrayList<>();
1037 cleanComponents.add(componentType);
1039 log.info("Trying to delete {} components of type {} marked as deleted", toBeDeleted, componentType);
1040 cleanComponentsResult = componentsCleanBusinessLogic.cleanComponents(cleanComponents, true);
1041 logDeleteResult(componentType, cleanComponentsResult.get(componentType));
1043 catch (Exception e) {
1044 log.error("Exception occurred {}", e.getMessage());
1045 log.debug("Exception occurred", e);
1049 private void logDeleteResult(NodeTypeEnum type, Either<List<String>, ResponseFormat> deleteResult) {
1050 if (deleteResult == null) {
1053 if (deleteResult.isLeft()) {
1054 log.info("Checked out {} versions are deleted successfully", type.getName());
1057 log.info("Cleanup of checked out {} versions failed due to the error: {}", type.getName(), deleteResult.right().value().getFormattedMessage());
1061 private void markCheckedOutResourceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) {
1062 String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid);
1063 if (!isNotFailed && checkedOutUniqueId != null) {
1065 //mark as deleted the checked out resource as this upgrade failed
1066 ResponseFormat respFormat = resourceBusinessLogic.deleteResource(checkedOutUniqueId.toLowerCase(), user);
1067 log.info("Checked out resource uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage());
1068 deleteResourcesIfLimitIsReached();
1070 catch (Exception e) {
1071 log.error("Error occurred:", e);
1076 private void markCheckedOutServiceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) {
1077 String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid);
1078 if (!isNotFailed && checkedOutUniqueId != null) {
1080 //delete the checked out resource as this upgrade failed
1081 ResponseFormat respFormat = serviceBusinessLogic.deleteService(checkedOutUniqueId.toLowerCase(), user);
1082 log.info("Checked out service uniqueId = {} is marked as deleted, status: {}", checkedOutUniqueId, respFormat.getFormattedMessage());
1083 deleteServicesIfLimitIsReached();
1084 } catch (Exception e) {
1085 log.error("Error occurred:", e);
1091 void deleteResourcesIfLimitIsReached() {
1092 markedAsDeletedResourcesCnt++;
1093 if (markedAsDeletedResourcesCnt >= maxDeleteComponents) {
1094 deleteMarkedComponents(NodeTypeEnum.Resource, markedAsDeletedResourcesCnt);
1095 markedAsDeletedResourcesCnt = 0;
1099 void deleteServicesIfLimitIsReached() {
1100 markedAsDeletedServicesCnt++;
1101 if (markedAsDeletedServicesCnt >= maxDeleteComponents) {
1102 deleteMarkedComponents(NodeTypeEnum.Service, markedAsDeletedServicesCnt);
1103 markedAsDeletedServicesCnt = 0;
1107 boolean isLockDeleteOperationSucceeded() {
1108 StorageOperationStatus status = componentsCleanBusinessLogic.lockDeleteOperation();
1112 log.info("Lock delete operation succeeded");
1113 isCleanupLocked = true;
1115 case FAILED_TO_LOCK_ELEMENT:
1116 log.info("Delete operation node is already locked");
1117 isCleanupLocked = isLockRetrySucceeded();
1120 log.error("Lock delete operation failed due to the error: {}", status);
1121 isCleanupLocked = false;
1124 return isCleanupLocked;
1127 private boolean isLockRetrySucceeded() {
1128 long startTime = System.currentTimeMillis();
1129 //try to lock the cleanup resource until configurable time interval is finished
1130 while (System.currentTimeMillis() - startTime <= deleteLockTimeoutInSeconds * 1000) {
1132 //sleep one second and try lock again
1134 if (componentsCleanBusinessLogic.lockDeleteOperation() == StorageOperationStatus.OK) {
1137 } catch (InterruptedException e) {
1138 log.error("Error occurred: {}", e.getMessage());
1139 Thread.currentThread().interrupt();
1145 void unlockDeleteOperation() {
1146 if (isCleanupLocked) {
1148 componentsCleanBusinessLogic.unlockDeleteOperation();
1149 log.info("Lock delete operation is canceled");
1150 isCleanupLocked = false;
1152 catch (Exception e) {
1153 log.debug("Failed to unlock delete operation", e);
1154 log.error("Failed to unlock delete operation due to the error {}", e.getMessage());
1159 private Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkOutComponent(org.openecomp.sdc.be.model.Component component) {
1160 log.info("Starting to perform check out of {} {}, uniqueId = {}", component.getComponentType(), component.getName(), component.getUniqueId());
1161 Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkoutRes =
1162 lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
1163 if (checkoutRes.isLeft()) {
1164 //add the uniqueId from "upgradeVf(String uniqueId)" and checkouRes's uniqueUID to the new map
1165 certifiedToNextCheckedOutUniqueId.put(component.getUniqueId(), checkoutRes.left().value().getUniqueId());
1166 log.debug("Add checked out component uniqueId = {} produced from certified component uniqueId = {} to the checked out map", checkoutRes.left().value().getUniqueId(), component.getUniqueId());
1171 UpgradeStatus getVfUpgradeStatus(boolean isUpgraded, boolean isInstance) {
1173 return isInstance ? UpgradeStatus.UPGRADED_AS_INSTANCE : UpgradeStatus.UPGRADED;
1175 return UpgradeStatus.NOT_UPGRADED;