Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig1710 / UpgradeMigration1710.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.migration.tasks.mig1710;
22
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;
63
64 import javax.annotation.PostConstruct;
65 import java.util.*;
66 import java.util.function.Predicate;
67 import java.util.stream.Collectors;
68
69 @Component
70 public class UpgradeMigration1710 implements PostMigration {
71
72     private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid";
73
74     private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid";
75
76     private static final String UNKNOWN = "UNKNOWN";
77
78     private static final String CHECKOUT_MESSAGE = "checkout upon upgrade migration";
79
80     private static final String FAILED_TO_CHANGE_STATE_OF_COMPONENT = "Failed to change state of component with name {}, invariantUUID {}, version {} to {}. ";
81
82     private static final String FAILED_TO_UPGRADE_COMPONENT = "Failed to upgrade {} with name {}, invariantUUID {}, version {}. Operation {}. The reason for failure: {}. ";
83
84     private static final String UPGRADE_COMPONENT_SUCCEEDED = "Upgrade of {} with name {}, invariantUUID {}, version {} finished successfully. ";
85
86     private static final String UPGRADE_VFS_FAILED = "Upgrade VFs upon upgrade migration 1710 process failed. ";
87
88     private static final Logger log = Logger.getLogger(UpgradeMigration1710.class);
89
90     private static final String ALLOTTED_RESOURCE_NAME = "Allotted Resource";
91
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;
95
96     @Autowired
97     private JanusGraphDao janusGraphDao;
98
99     @Autowired
100     private ToscaOperationFacade toscaOperationFacade;
101
102     @Autowired
103     private LifecycleBusinessLogic lifecycleBusinessLogic;
104
105     @Autowired
106     private IUserAdminOperation userAdminOperation;
107
108     @Autowired
109     private ResourceBusinessLogic resourceBusinessLogic;
110
111     @Autowired
112     private ServiceBusinessLogic serviceBusinessLogic;
113
114     @Autowired
115     private CsarOperation csarOperation;
116
117     @Autowired
118     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
119
120     @Autowired
121     private ComponentsUtils componentsUtils;
122
123     @Autowired
124     private ComponentsCleanBusinessLogic componentsCleanBusinessLogic;
125
126     private XlsOutputHandler outputHandler = new XlsOutputHandler(null, "UpgradeMigration1710report","COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION");
127
128     private User user = null;
129
130     private final LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction(CHECKOUT_MESSAGE, LifecycleChanceActionEnum.UPGRADE_MIGRATION);
131
132     private final Map<String, GraphVertex> latestGenericTypes = new HashMap<>();
133
134     private final Map<String, String> latestOriginResourceVersions = new HashMap<>();
135
136     private final Map<String, org.openecomp.sdc.be.model.Component> upgradedNodeTypesMap = new HashMap<>();
137
138     private List<String> nodeTypes;
139
140     private List<String> proxyServiceContainers = new ArrayList<>();
141
142     private List<String> vfAllottedResources = new ArrayList<>();
143
144     private List<String> allottedVfContainers = new ArrayList<>();
145
146     private boolean isVfcUpgradeRequired = false;
147
148     private boolean skipIfUpgradeVfFailed = true;
149
150     private boolean isAllottedAndProxySupported = true;
151
152     private String userId;
153
154     private boolean isCleanupLocked = false;
155
156     private int markedAsDeletedResourcesCnt = 0;
157
158     private int markedAsDeletedServicesCnt = 0;
159
160     //how many components can be deleted once
161     private int maxDeleteComponents = 10;
162
163     private boolean enableAutoHealing = true;
164
165     //map for tracing checked out resources that keep in place after upgrade failure
166     private HashMap<String, String> certifiedToNextCheckedOutUniqueId = new HashMap<>();
167
168     private int deleteLockTimeoutInSeconds = 60;
169
170     private boolean isLockSucceeded = false;
171
172     /***********************************************/
173
174     @VisibleForTesting
175     void setNodeTypesSupportOnly(boolean nodeTypesSupportOnly) {
176         isNodeTypesSupportOnly = nodeTypesSupportOnly;
177     }
178
179     @VisibleForTesting
180     void setUser(User user) {
181         this.user = user;
182     }
183
184     @VisibleForTesting
185     void setMarkedAsDeletedResourcesCnt(int markedAsDeletedResourcesCnt) {
186         this.markedAsDeletedResourcesCnt = markedAsDeletedResourcesCnt;
187     }
188
189     @VisibleForTesting
190     void setMarkedAsDeletedServicesCnt(int markedAsDeletedServicesCnt) {
191         this.markedAsDeletedServicesCnt = markedAsDeletedServicesCnt;
192     }
193
194     @PostConstruct
195     void init() {
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();
202
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;
209     }
210
211     @Override
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. ";
214     }
215
216     enum UpgradeStatus {
217         UPGRADED,
218         UPGRADED_AS_INSTANCE,
219         NOT_UPGRADED
220     }
221
222     @Override
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 ;
230         }
231         log.info("Starting upgrade migration 1710 process. ");
232         boolean result = true;
233
234         try {
235             //lock cleanup node to avoid BE to delete marked components
236             //while the auto-healing process is running
237             isLockSucceeded = isNodeTypesSupportOnly ? true : isLockDeleteOperationSucceeded();
238
239             if (!isLockSucceeded) {
240                 result = false;
241                 log.error("Cleanup node can't be locked. Upgrade migration failed");
242             }
243             else {
244                 Either<User, ActionStatus> userReq = userAdminOperation.getUserData(userId, false);
245                 if (userReq.isRight()) {
246                     result = false;
247                     log.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value());
248                 } else {
249                     user = userReq.left().value();
250                     log.info("User {} will perform upgrade operation", user.getUserId());
251                 }
252             }
253             if (result) {
254                 result = upgradeNodeTypes();
255             }
256             if (!isNodeTypesSupportOnly && result) {
257                 result = upgradeTopologyTemplates();
258             }
259         }
260         catch (Exception e) {
261             result = false;
262             log.error("Error occurred during the migration: ", e);
263         } finally {
264             MigrationResult.MigrationStatus status = result ?
265                     MigrationResult.MigrationStatus.COMPLETED : MigrationResult.MigrationStatus.FAILED;
266             cleanup(status);
267             migrationResult.setMigrationStatus(status);
268         }
269         return migrationResult;
270     }
271
272     private boolean upgradeTopologyTemplates() {
273         if (upgradeVFs()) {
274             upgradeServices();
275             upgradeProxyServiceContainers();
276             upgradeAllottedVFs();
277             upgradeAllottedVfContainers();
278             return true;
279         }
280         return false;
281     }
282
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();
287         } else {
288             log.info("Upgrade migration 1710 was failed. ");
289             janusGraphDao.rollback();
290         }
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();
299         }
300     }
301
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);
306         }
307         log.info("Upgrade {} upon upgrade migration 1710 process is finished. ", containerName);
308     }
309
310     private void upgradeServiceAndCommitIfNeeded(String currUid, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade) {
311         boolean result = true;
312         try {
313             result = handleService(currUid, shouldUpgrade);
314         } catch (Exception e) {
315             result = false;
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);
318         }
319         finally {
320             if (result) {
321                 log.info("Service upgrade finished successfully: uniqueId {} ", currUid);
322                 janusGraphDao.commit();
323             }
324             else {
325                 log.error("Failed to upgrade service with uniqueId {} ", currUid);
326                 janusGraphDao.rollback();
327             }
328             markCheckedOutServiceAsDeletedIfUpgradeFailed(currUid, result);
329         }
330     }
331
332     private void upgradeAllottedVfContainers() {
333         upgradeServices(allottedVfContainers, component -> true, "proxy " + allottedVfContainers.size() + " service containers");
334     }
335
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");
340             return;
341         }
342         upgradeServices(getServicesRes.left().value(), this::shouldUpgrade, "services");
343     }
344
345     private void upgradeProxyServiceContainers() {
346         upgradeServices(proxyServiceContainers, component -> true, "proxy service containers");
347     }
348
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());
355             return false;
356         }
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());
362         }
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());
368                 return false;
369             }
370             latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
371         }
372         if(latestVersionExists(latestGenericTypes.get(derivedFromGenericType), getServiceRes.left().value().getDerivedFromGenericVersion())){
373             return upgradeService(getServiceRes.left().value());
374         }
375         if(!collectLatestOriginResourceVersions(getServiceRes.left().value())){
376             return false;
377         }
378         if(shouldUpgrade.test(getServiceRes.left().value())){
379             return upgradeService(getServiceRes.left().value());
380         }
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);
382         return true;
383     }
384
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)) {
389                     return false;
390                 }
391             }
392         }
393         return true;
394     }
395
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());
402             return false;
403         }
404         latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion());
405         return true;
406     }
407
408     private boolean shouldUpgrade(org.openecomp.sdc.be.model.Component component) {
409         if(CollectionUtils.isNotEmpty(component.getComponentInstances())) {
410             if (containsProxyOrAllottedVF(component)) {
411                 return false;
412             }
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());
416                     return true;
417                 }
418             }
419         }
420         return false;
421     }
422
423     private boolean containsProxyOrAllottedVF(org.openecomp.sdc.be.model.Component component) {
424         return !component.getComponentInstances()
425                 .stream()
426                 .filter(i->isProxyOrAllottedVF(i, component.getUniqueId()))
427                 .collect(Collectors.toList()).isEmpty();
428     }
429
430     private boolean isProxyOrAllottedVF(ComponentInstance instance, String uniqueId) {
431         if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) {
432             keepProxyServiceContainerIfSupported(uniqueId);
433             return true;
434         }
435         if (isAllottedResource(instance.getActualComponentUid())) {
436             keepAllottedVfContainerIfSupported(uniqueId);
437             return true;
438         }
439         return false;
440     }
441
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);
446         }
447     }
448
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);
453         }
454     }
455
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());
464             return false;
465         }
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());
470             return false;
471         }
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());
476             return false;
477         }
478         outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), serviceUuid, checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED);
479         return true;
480     }
481
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());
491                 }
492             }
493         }
494         return Either.left(component);
495     }
496
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);
502         }
503         return upgradeResourceInstance(component, instance, newComponentInstance);
504     }
505
506     private Either<ComponentInstance, ResponseFormat> upgradeResourceInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) {
507
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())));
516         }
517         if(upgradeInstanceRes == null) {
518             copyComponentNameAndVersionToNewInstance(newComponentInstance, getOriginRes.left().value());
519
520             if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){
521                 upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance);
522             }
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())));
526             }
527         }
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));
532         }
533         if(upgradeInstanceRes == null){
534             upgradeInstanceRes = Either.left(instance);
535         }
536         log.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ",
537                 component.getComponentType().getValue(), instance.getName());
538         return upgradeInstanceRes;
539     }
540
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());
546     }
547
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()) {
556                     return instance;
557                 }
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())
560                         .right()
561                         .forEach(e -> log.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName()));
562             }
563         }
564         return instance;
565     }
566
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()){
572             return false;
573         }
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());
576         }
577         return false;
578     }
579
580     private boolean isAllottedVf(org.openecomp.sdc.be.model.Component component){
581         if(component.getComponentType() != ComponentTypeEnum.RESOURCE || ((Resource)component).getResourceType() != ResourceTypeEnum.VF){
582             return false;
583         }
584         return isAllottedResource(component.getUniqueId());
585     }
586
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())));
591         }
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())));
598         }
599         newComponentInstance.setComponentUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
600         return changeAssetVersion(component, instance, newComponentInstance);
601     }
602
603     private Either<List<GraphVertex>, JanusGraphOperationStatus> getLatestCertifiedService(String invariantUUID) {
604
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);
612         return janusGraphDao
613             .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
614     }
615
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);
618     }
619
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()) {
625                 return false;
626             }
627             for (String toscaResourceName : nodeTypes) {
628                 if (!upgradeNodeType(toscaResourceName, getRes.left().value())) {
629                     return false;
630                 }
631             }
632         }
633         else {
634             log.info("No node types for upgrade are configured");
635         }
636         return true;
637     }
638
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);
643             return false;
644         }
645         List<GraphVertex> vList = status.left().value();
646         for (GraphVertex vertex : vList) {
647             StorageOperationStatus updateRes = upgradeNodeType(vertex, allNodeTypes);
648             if (updateRes != StorageOperationStatus.OK) {
649                 return false;
650             }
651         }
652         return true;
653     }
654
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);
660             return false;
661         }
662         return upgradeVFs(getVfsRes.left().value(), false);
663     }
664
665     private boolean upgradeAllottedVFs() {
666         log.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size());
667         return upgradeVFs(vfAllottedResources, true);
668     }
669
670     boolean upgradeVFs(List<String> resourceList, boolean isAllottedVfsUpgrade) {
671         for (String currUid : resourceList) {
672             boolean result = true;
673             try {
674                 result = upgradeVf(currUid, isAllottedVfsUpgrade, false);
675                 if (!result && !skipIfUpgradeVfFailed) {
676                     return false;
677                 }
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);
681                 result = false;
682                 if (!skipIfUpgradeVfFailed) {
683                     return false;
684                 }
685             }
686             finally {
687                 if (result) {
688                     log.info("Resource upgrade finished successfully: uniqueId {} ", currUid);
689                     janusGraphDao.commit();
690                 }
691                 else {
692                     log.error("Failed to upgrade resource with uniqueId {} ", currUid);
693                     janusGraphDao.rollback();
694                 }
695                 markCheckedOutResourceAsDeletedIfUpgradeFailed(currUid, result);
696             }
697         }
698         log.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. ");
699         return true;
700     }
701
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());
708             return false;
709         }
710         if(!allottedVfsUpgrade && isAllottedVf(getRes.left().value())){
711             keepAllottedResourceIfSupported(uniqueId);
712             return true;
713         }
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());
720                 return false;
721             }
722             if (isGreater(latestVersionRes.left().value(), getRes.left().value().getCsarVersion())) {
723                 return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes.left().value(), isInstance);
724             }
725             if (isVfcUpgradeRequired) {
726                 return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance);
727             }
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());
729             return true;
730         }
731         else {
732             return upgradeComponentWithLatestGeneric(getRes.left().value(), isInstance);
733         }
734     }
735
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);
740         }
741     }
742
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());
748             return false;
749         }
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());
755         try {
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);
760             return false;
761         }
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());
766             return false;
767         }
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));
770         return true;
771     }
772
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)) {
778             return false;
779         }
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);
786             }
787             else {
788                 log.info("The vf resource with name {}, invariantUUID {}, version {},  has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion());
789             }
790             updatedComponent = checkOutAndCertifyComponent(component);
791         } else {
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());
793         }
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));
798         }
799         return true;
800     }
801
802     private org.openecomp.sdc.be.model.Component checkOutAndCertifyComponent(org.openecomp.sdc.be.model.Component component) {
803
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());
809             return null;
810         }
811
812         if (!updateCompositionFailed(component, checkoutRes.left().value())) {
813             return null;
814         }
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());
819             return null;
820         }
821         return certifyRes.left().value();
822     }
823
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);
832                 return true;
833             }
834             latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
835         }
836         return false;
837     }
838
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());
847                 }
848                 outputHandler.addRecord(checkoutResource.getComponentType().name(), checkoutResource.getName(), checkoutResource.getUUID(), checkoutResource.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateCompositionRes.right().value().getFormattedMessage());
849                 return true;
850             }
851         }
852         return false;
853     }
854
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());
863
864         }
865         List<GraphVertex> derivedResourcesUid = getAllDerivedGraphVertices(allCertifiedUids, parentResourceRes);
866         String uniqueId = (String) nodeTypeV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID);
867
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();
872         }
873
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;
878         }
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);
882         }
883         return result;
884     }
885
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());
892             return false;
893         }
894         if (performFullCertification(checkouRes.left().value()).isLeft()) {
895             upgradedNodeTypesMap.put(nodeType.getToscaResourceName(), checkouRes.left().value());
896             janusGraphDao.commit();
897             return true;
898         }
899         return false;
900     }
901
902     private List<GraphVertex> getAllDerivedGraphVertices(List<String> allCertifiedUids, Either<List<GraphVertex>, JanusGraphOperationStatus> parentResources) {
903         List<GraphVertex> derivedResourcesUid = new ArrayList<>();
904
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);
910                 }
911             }
912         }
913         return derivedResourcesUid;
914     }
915
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());
919
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;
924         }
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;
929         }
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);
933         } else {
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());
937         }
938         return changeStateEither;
939     }
940
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());
944
945         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, parentResource);
946         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
947
948         return janusGraphDao
949             .getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
950     }
951
952     private boolean latestVersionExists(GraphVertex latestDerivedFrom, String currentVersion) {
953         return isGreater((String) latestDerivedFrom.getJsonMetadataField(JsonPresentationFields.VERSION), currentVersion);
954     }
955
956     private boolean isGreater(String latestVersion, String currentVersion) {
957         if (latestVersion != null && currentVersion == null) {
958             return true;
959         }
960         if (latestVersion == null) {
961             return false;
962         }
963         return Double.parseDouble(latestVersion) > Double.parseDouble(currentVersion);
964     }
965
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<>();
971
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());
976         }
977         if (getComponentsRes.isRight()) {
978             result = Either.left(new ArrayList<>());
979         }
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));
985                 } else {
986                     latestNotCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
987                 }
988             }
989             result = Either.left(latestCertifiedMap.entrySet().stream().filter(e -> !latestNotCertifiedMap.containsKey(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList()));
990         }
991         return result;
992     }
993
994     private Either<List<GraphVertex>, JanusGraphOperationStatus> getAllLatestComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) {
995
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);
999
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());
1004         }
1005         return janusGraphDao
1006             .getByCriteria(vertexType, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
1007     }
1008
1009     private Either<List<GraphVertex>, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
1010
1011         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
1012         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
1013
1014         propertiesToMatch.put(property, nodeName);
1015         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
1016
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));
1023         }
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);
1029             }
1030         }
1031         return Either.left(result);
1032     }
1033
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);
1038         try {
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));
1042         }
1043         catch (Exception e) {
1044             log.error("Exception occurred {}", e.getMessage());
1045             log.debug("Exception occurred", e);
1046         }
1047     }
1048
1049     private void logDeleteResult(NodeTypeEnum type, Either<List<String>, ResponseFormat> deleteResult) {
1050         if (deleteResult == null) {
1051             return;
1052         }
1053         if (deleteResult.isLeft()) {
1054             log.info("Checked out {} versions are deleted successfully", type.getName());
1055         }
1056         else {
1057             log.info("Cleanup of checked out {} versions failed due to the error: {}", type.getName(), deleteResult.right().value().getFormattedMessage());
1058         }
1059     }
1060
1061     private void markCheckedOutResourceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) {
1062         String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid);
1063         if (!isNotFailed && checkedOutUniqueId != null) {
1064             try {
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();
1069             }
1070             catch (Exception e) {
1071                 log.error("Error occurred:", e);
1072             }
1073         }
1074     }
1075
1076     private void markCheckedOutServiceAsDeletedIfUpgradeFailed(String certUid, boolean isNotFailed) {
1077         String checkedOutUniqueId = certifiedToNextCheckedOutUniqueId.remove(certUid);
1078         if (!isNotFailed && checkedOutUniqueId != null) {
1079             try {
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);
1086             }
1087         }
1088
1089     }
1090
1091     void deleteResourcesIfLimitIsReached() {
1092         markedAsDeletedResourcesCnt++;
1093         if (markedAsDeletedResourcesCnt >= maxDeleteComponents) {
1094             deleteMarkedComponents(NodeTypeEnum.Resource, markedAsDeletedResourcesCnt);
1095             markedAsDeletedResourcesCnt = 0;
1096         }
1097     }
1098
1099     void deleteServicesIfLimitIsReached() {
1100         markedAsDeletedServicesCnt++;
1101         if (markedAsDeletedServicesCnt >= maxDeleteComponents) {
1102             deleteMarkedComponents(NodeTypeEnum.Service, markedAsDeletedServicesCnt);
1103             markedAsDeletedServicesCnt = 0;
1104         }
1105     }
1106
1107     boolean isLockDeleteOperationSucceeded() {
1108         StorageOperationStatus status = componentsCleanBusinessLogic.lockDeleteOperation();
1109
1110         switch(status) {
1111             case OK:
1112                 log.info("Lock delete operation succeeded");
1113                 isCleanupLocked = true;
1114                 break;
1115             case FAILED_TO_LOCK_ELEMENT:
1116                 log.info("Delete operation node is already locked");
1117                 isCleanupLocked = isLockRetrySucceeded();
1118                 break;
1119             default:
1120                 log.error("Lock delete operation failed due to the error: {}", status);
1121                 isCleanupLocked = false;
1122                 break;
1123         }
1124         return isCleanupLocked;
1125     }
1126
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) {
1131             try {
1132                 //sleep one second and try lock again
1133                 Thread.sleep(1000);
1134                 if (componentsCleanBusinessLogic.lockDeleteOperation() == StorageOperationStatus.OK) {
1135                     return true;
1136                 }
1137             } catch (InterruptedException e) {
1138                 log.error("Error occurred: {}", e.getMessage());
1139                 Thread.currentThread().interrupt();
1140             }
1141         }
1142         return false;
1143     }
1144
1145     void unlockDeleteOperation() {
1146         if (isCleanupLocked) {
1147             try {
1148                 componentsCleanBusinessLogic.unlockDeleteOperation();
1149                 log.info("Lock delete operation is canceled");
1150                 isCleanupLocked = false;
1151             }
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());
1155             }
1156         }
1157     }
1158
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());
1167         }
1168         return checkoutRes;
1169     }
1170
1171     UpgradeStatus getVfUpgradeStatus(boolean isUpgraded, boolean isInstance) {
1172         if (isUpgraded) {
1173             return isInstance ? UpgradeStatus.UPGRADED_AS_INSTANCE : UpgradeStatus.UPGRADED;
1174         }
1175         return UpgradeStatus.NOT_UPGRADED;
1176     }
1177
1178 }