Sync Integ to Master
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / tasks / mig1710 / UpgradeMigration1710.java
1 package org.openecomp.sdc.asdctool.migration.tasks.mig1710;
2
3 import com.google.common.collect.Lists;
4 import fj.data.Either;
5 import org.apache.commons.collections.CollectionUtils;
6 import org.apache.commons.collections.MapUtils;
7 import org.apache.commons.lang3.StringUtils;
8 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
9 import org.openecomp.sdc.asdctool.migration.core.task.PostMigration;
10 import org.openecomp.sdc.asdctool.migration.tasks.handlers.XlsOutputHandler;
11 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
12 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
13 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
14 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
15 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
16 import org.openecomp.sdc.be.config.ConfigurationManager;
17 import org.openecomp.sdc.be.dao.api.ActionStatus;
18 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
19 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
20 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
21 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
22 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
23 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
24 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
25 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
26 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
27 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.ComponentInstance;
31 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
32 import org.openecomp.sdc.be.model.ComponentParametersView;
33 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
38 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
39 import org.openecomp.sdc.be.model.operations.api.IUserAdminOperation;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
42 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.stereotype.Component;
48
49 import java.util.ArrayList;
50 import java.util.EnumMap;
51 import java.util.HashMap;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Optional;
55 import java.util.function.Predicate;
56 import java.util.stream.Collectors;
57
58 //@Component
59 public class UpgradeMigration1710 implements PostMigration {
60         
61         private static final String SERVICE_UUID_RPOPERTY = "providing_service_uuid";
62         
63         private static final String SERVICE_INVARIANT_UUID_RPOPERTY = "providing_service_invariant_uuid";
64         
65     private static final String UNKNOWN = "UNKNOWN";
66
67     private static final String CHECKOUT_MESSAGE = "checkout upon upgrade migration";
68
69     private static final String FAILED_TO_CHANGE_STATE_OF_COMPONENT = "Failed to change state of component with name {}, invariantUUID {}, version {} to {}. ";
70
71     private static final String FAILED_TO_UPGRADE_COMPONENT = "Failed to upgrade {} with name {}, invariantUUID {}, version {}. Operation {}. The reason for failure: {}. ";
72
73     private static final String UPGRADE_COMPONENT_SUCCEEDED = "Upgrade of {} with name {}, invariantUUID {}, version {} finished successfully. ";
74
75     private static final String UPGRADE_VFS_FAILED = "Upgrade VFs upon upgrade migration 1710 process failed. ";
76
77     private static final Logger LOGGER = LoggerFactory.getLogger(UpgradeMigration1710.class);
78
79     @Autowired
80     private TitanDao titanDao;
81
82     @Autowired
83     private ToscaOperationFacade toscaOperationFacade;
84
85     @Autowired
86     private LifecycleBusinessLogic lifecycleBusinessLogic;
87
88     @Autowired
89     private IUserAdminOperation userAdminOperation;
90
91     @Autowired
92     private ResourceBusinessLogic resourceBusinessLogic;
93
94     @Autowired
95     private CsarOperation csarOperation;
96
97     @Autowired
98     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
99
100     @Autowired
101     private ComponentsUtils componentsUtils;
102
103     private XlsOutputHandler outputHandler = new XlsOutputHandler("COMPONENT TYPE", "COMPONENT NAME", "COMPONENT UUID", "COMPONENT UNIQUE_ID", "UPGRADE STATUS", "DESCRIPTION");
104
105     private User user = null;
106
107     private final LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction(CHECKOUT_MESSAGE, LifecycleChanceActionEnum.UPGRADE_MIGRATION);
108
109     private final Map<String, GraphVertex> latestGenericTypes = new HashMap<>();
110
111     private final Map<String, String> latestOriginResourceVersions = new HashMap<>();
112     
113     private final List<String> proxyServiceContainers = new ArrayList<>();
114     
115     private final List<String> vfAllottedResources = new ArrayList<>();
116     
117     private final List<String> allottedVfContainers = new ArrayList<>();
118
119     private boolean isVfcUpgradeRequired = false;
120
121     private boolean skipIfUpgradeVfFailed = true;
122
123     /***********************************************/
124
125     @Override
126     public String description() {
127         return "Upgrade migration 1710 - post migration task, which is dedicated to upgrade all latest certified (and not checked out) Node types, VFs and Services. ";
128     }
129
130     private enum UpgradeStatus {
131         UPGRADED,
132         NOT_UPGRADED
133     }
134
135     @Override
136     public MigrationResult migrate() {
137         LOGGER.info("Starting upgrade migration 1710 process. ");
138         MigrationResult migrationResult = new MigrationResult();
139
140         boolean result = true;
141         try {
142             isVfcUpgradeRequired = !ConfigurationManager.getConfigurationManager().getConfiguration().getSkipUpgradeVSPsFlag();
143             skipIfUpgradeVfFailed = ConfigurationManager.getConfigurationManager().getConfiguration().getSkipUpgradeFailedVfs();
144             final String userId = ConfigurationManager.getConfigurationManager().getConfiguration().getAutoHealingOwner();
145
146             Either<User, ActionStatus> userReq = userAdminOperation.getUserData(userId, false);
147             if (userReq.isRight()) {
148                 result = false;
149                 LOGGER.error("Upgrade migration failed. User {} resolve failed: {} ", userId, userReq.right().value());
150             } else {
151                 user = userReq.left().value();
152                 LOGGER.info("User {} will perform upgrade operation", user.getUserId());
153             }
154             if (result) {
155                 result = upgradeNodeTypes();
156             }
157             if (result) {
158                 result = upgradeVFs();
159             }
160             if (result) {
161                 upgradeServices();
162             }
163             if(result){
164                 upgradeProxyServiceContainers();
165             }
166             if(result){
167                 upgradeAllottedVFs();
168             }
169             if(result){
170                 upgradeAllottedVfContainers();
171             }
172         } catch (Exception e) {
173             result = false;
174             LOGGER.error("Error occurred {}. ", e);
175         } finally {
176             if (result) {
177                 LOGGER.info("Upgrade migration 1710 has been successfully finished. ");
178                 titanDao.commit();
179                 migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED);
180             } else {
181                 LOGGER.info("Upgrade migration 1710 was failed. ");
182                 titanDao.rollback();
183                 migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED);
184             }
185             outputHandler.writeOutput();
186         }
187         return migrationResult;
188     }
189
190     private void upgradeAllottedVfContainers() {
191         LOGGER.info("Starting upgrade proxy {} service containers upon upgrade migration 1710 process. ", allottedVfContainers.size());
192         for(String currUid : allottedVfContainers){
193                 upgradeServiceAndCommitIfNeeded(currUid,  component -> true);
194         }
195         }
196
197         private StorageOperationStatus upgradeServices() {
198         LOGGER.info("Starting upgrade services upon upgrade migration 1710 process. ");
199         Either<List<String>, TitanOperationStatus> getServicesRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.SERVICE);
200         if (getServicesRes.isRight()) {
201             return StorageOperationStatus.GENERAL_ERROR;
202         }
203         for (String currUid : getServicesRes.left().value()) {
204             upgradeServiceAndCommitIfNeeded(currUid, this::shouldUpgrade);
205         }
206         return StorageOperationStatus.OK;
207     }
208
209     private void upgradeServiceAndCommitIfNeeded(String currUid, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade) {
210         boolean result = true;
211         try {
212             result = handleService(currUid, shouldUpgrade);
213         } catch (Exception e) {
214             result = false;
215             LOGGER.error("Failed to upgrade Service with uniqueId {} due to a reason {}. ", currUid, e);
216         }
217         finally {
218             if (result) {
219                 titanDao.commit();
220             }
221             else {
222                 titanDao.rollback();
223             }
224         }
225     }
226     
227     private void upgradeProxyServiceContainers() {
228         LOGGER.info("Starting upgrade proxy service containers upon upgrade migration 1710 process. ");
229         for(String currUid : proxyServiceContainers){
230                 upgradeServiceAndCommitIfNeeded(currUid,  component -> true);
231         }
232         }
233
234     private boolean handleService(String uniqueId, Predicate<org.openecomp.sdc.be.model.Component> shouldUpgrade) {
235         LOGGER.info("Starting upgrade Service with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
236         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getServiceRes = toscaOperationFacade.getToscaElement(uniqueId);
237         if(getServiceRes.isRight()){
238             LOGGER.error("Failed to upgrade service with uniqueId {} due to {}. ", uniqueId, getServiceRes.right().value());
239             outputHandler.addRecord(ComponentTypeEnum.SERVICE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getServiceRes.right().value());
240             return false;
241         }
242         String derivedFromGenericType =  getServiceRes.left().value().getDerivedFromGenericType();
243         LOGGER.debug("derivedFromGenericType: {}", derivedFromGenericType );
244         if (derivedFromGenericType == null) {
245             //malformed field value, upgrade required
246             return upgradeService(getServiceRes.left().value());
247         }
248         if(!latestGenericTypes.containsKey(derivedFromGenericType)){
249             Either<List<GraphVertex>, TitanOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType);
250             if(getDerivedRes.isRight()){
251                 LOGGER.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());
252                 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());
253                 return false;
254             }
255             latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
256         }
257         if(latestVersionExists(latestGenericTypes.get(derivedFromGenericType), getServiceRes.left().value().getDerivedFromGenericVersion())){
258             return upgradeService(getServiceRes.left().value());
259         }
260         if(!collectLatestOriginResourceVersions(getServiceRes.left().value())){
261             return false;
262         }
263         if(shouldUpgrade.test(getServiceRes.left().value())){
264             return upgradeService(getServiceRes.left().value());
265         }
266         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);
267         return true;
268     }
269
270     private boolean collectLatestOriginResourceVersions(org.openecomp.sdc.be.model.Component component) {
271         if (CollectionUtils.isNotEmpty(component.getComponentInstances())) {
272             for (ComponentInstance instance : component.getComponentInstances()) {
273                 if (instance.getOriginType() != OriginTypeEnum.ServiceProxy && !latestOriginResourceVersions.containsKey(instance.getToscaComponentName()) && !addComponent(component, instance)) {
274                     return false;
275                 }
276             }
277         }
278         return true;
279     }
280
281         private boolean addComponent(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) {
282                 VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name());
283                 Either<Resource, StorageOperationStatus> getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata);
284                 if (getOriginRes.isRight()) {
285                     LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "toscaOperationFacade.getLatestCertifiedByToscaResourceName", getOriginRes.right().value());
286                     outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getOriginRes.right().value());
287                     return false;
288                 }
289                 latestOriginResourceVersions.put(instance.getToscaComponentName(), getOriginRes.left().value().getVersion());
290                 return true;
291         }
292
293     private boolean shouldUpgrade(org.openecomp.sdc.be.model.Component component) {
294         boolean shouldUpgrade = false;
295         if(CollectionUtils.isNotEmpty(component.getComponentInstances())){
296             for(ComponentInstance instance : component.getComponentInstances()){
297                 if(instance.getOriginType() == OriginTypeEnum.ServiceProxy){
298                     LOGGER.info("The service with name {}, invariantUUID {}, version {}, contains Service proxy instance {}, than the service should be upgraded in the end of the upgrading proccess. ", component.getName(), component.getInvariantUUID(), component.getVersion(), instance.getName());
299                     proxyServiceContainers.add(component.getUniqueId());
300                     shouldUpgrade = false;
301                     break;
302                 }
303                 if(isAllottedResource(instance.getActualComponentUid())){
304                         allottedVfContainers.add(component.getUniqueId());
305                 }
306                 if(isGreater(latestOriginResourceVersions.get(instance.getToscaComponentName()), instance.getComponentVersion())){
307                     LOGGER.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());
308                     shouldUpgrade = true;
309                 }
310             }
311         }
312         return shouldUpgrade;
313     }
314
315     private boolean upgradeService(org.openecomp.sdc.be.model.Component service) {
316         String serviceName = service.getName();
317         String serviceUuid = service.getUUID();
318         LOGGER.info("Starting upgrade Service with name {}, invariantUUID {}, version {} upon upgrade migration 1710 process. ", serviceName, service.getInvariantUUID(), service.getVersion());
319         LOGGER.info("Starting to perform check out of service {}. ", serviceName);
320         Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = lifecycleBusinessLogic.changeComponentState(service.getComponentType(), service.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
321         if (checkouRes.isRight()) {
322             LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "lifecycleBusinessLogic.changeComponentState", checkouRes.right().value().getFormattedMessage());
323             outputHandler.addRecord(service.getComponentType().name(), serviceName, serviceUuid, service.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage());
324             return false;
325         }
326         Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateCompositionRes = updateComposition(checkouRes.left().value());
327         if (updateCompositionRes.isRight()) {
328             LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage());
329             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());
330             return false;
331         }
332         Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkouRes.left().value());
333         if (certifyRes.isRight()) {
334             LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, service.getComponentType().getValue(), serviceName, service.getInvariantUUID(), service.getVersion(), "performFullCertification", certifyRes.right().value().getFormattedMessage());
335             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());
336             return false;
337         }
338         outputHandler.addRecord(checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), serviceUuid, checkouRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED);
339         return true;
340     }
341
342     private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateComposition(org.openecomp.sdc.be.model.Component component) {
343         Either<ComponentInstance, ResponseFormat> upgradeInstanceRes;
344         for (ComponentInstance instance : component.getComponentInstances()) {
345             upgradeInstanceRes = upgradeInstance(component, instance);
346             if (upgradeInstanceRes.isRight()) {
347                 LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion(), "upgradeInstance", upgradeInstanceRes.right().value().getFormattedMessage());
348                 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), upgradeInstanceRes.right().value().getFormattedMessage());
349                 return Either.right(upgradeInstanceRes.right().value());
350             }
351         }
352         return Either.left(component);
353     }
354
355     private Either<ComponentInstance, ResponseFormat> upgradeInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance) {
356         LOGGER.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName());
357         ComponentInstance newComponentInstance = new ComponentInstance(instance);
358         if (instance.getOriginType() == OriginTypeEnum.ServiceProxy) {
359             return upgradeServiceProxyInstance(component, instance, newComponentInstance);
360         }
361         return upgradeResourceInstance(component, instance, newComponentInstance);
362     }
363
364     private Either<ComponentInstance, ResponseFormat> upgradeResourceInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) {
365
366         LOGGER.info("Starting upgrade {} instance {} upon upgrade migration 1710 process. ", component.getComponentType().getValue(), instance.getName());
367         Either<ComponentInstance, ResponseFormat> upgradeInstanceRes = null;
368         VertexTypeEnum vertexType = ModelConverter.getVertexType(instance.getOriginType().name());
369         Either<Resource, StorageOperationStatus> getOriginRes = toscaOperationFacade.getLatestCertifiedByToscaResourceName(instance.getToscaComponentName(), vertexType, JsonParseFlagEnum.ParseMetadata);
370         if(getOriginRes.isRight()){
371             LOGGER.info("Upgrade of {} instance {} upon upgrade migration 1710 process failed due to a reason {}. ",
372                     component.getComponentType().getValue(), instance.getName(), getOriginRes.right().value());
373             upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(getOriginRes.right().value(), instance.getOriginType().getComponentType())));
374         }
375         if(upgradeInstanceRes == null){
376                 newComponentInstance.setComponentName(getOriginRes.left().value().getName());
377                 newComponentInstance.setComponentUid(getOriginRes.left().value().getUniqueId());
378                 newComponentInstance.setComponentVersion(getOriginRes.left().value().getVersion());
379                 newComponentInstance.setToscaComponentName(((Resource)getOriginRes.left().value()).getToscaResourceName());
380                 if(isGreater(getOriginRes.left().value().getVersion(), instance.getComponentVersion())){
381                         upgradeInstanceRes = changeAssetVersion(component, instance, newComponentInstance);
382                 }
383                 if((upgradeInstanceRes == null || upgradeInstanceRes.isLeft()) && isAllottedResource(instance.getComponentUid()) && MapUtils.isNotEmpty(component.getComponentInstancesProperties())){
384                         ComponentInstance instanceToUpdate = upgradeInstanceRes == null ? instance : upgradeInstanceRes.left().value();
385                         upgradeInstanceRes = Either.left(updateServiceUuidProperty(component, instanceToUpdate, component.getComponentInstancesProperties().get(instance.getUniqueId())));
386                 }
387         }
388         //upgrade nodes contained by CVFC
389         if(upgradeInstanceRes == null && isVfcUpgradeRequired && newComponentInstance.getOriginType() == OriginTypeEnum.CVFC &&
390                                                     !upgradeVf(getOriginRes.left().value().getUniqueId(), false)) {
391                 upgradeInstanceRes = Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
392         }
393         if(upgradeInstanceRes == null){
394                 upgradeInstanceRes = Either.left(instance);
395         }
396         LOGGER.info("Upgrade of {} instance {} upon upgrade migration 1710 process finished successfully. ",
397                             component.getComponentType().getValue(), instance.getName());
398         return upgradeInstanceRes;
399     }
400
401     private ComponentInstance updateServiceUuidProperty(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, List<ComponentInstanceProperty> instanceProperties){
402         if(isAllottedResource(instance.getComponentUid()) && instanceProperties != null){
403                 Optional<ComponentInstanceProperty> propertyUuid =      instanceProperties.stream().filter(p->p.getName().equals(SERVICE_UUID_RPOPERTY)).findFirst();
404                 Optional<ComponentInstanceProperty> propertyInvariantUuid = instanceProperties.stream().filter(p->p.getName().equals(SERVICE_INVARIANT_UUID_RPOPERTY)).findFirst();
405                 if(propertyUuid.isPresent() && propertyInvariantUuid.isPresent()){
406                         String serviceInvariantUUID = propertyInvariantUuid.get().getValue();
407                         Either<List<GraphVertex>, TitanOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(serviceInvariantUUID);
408                     if (getLatestOriginServiceRes.isRight()) {
409                         return instance;
410                     }
411                     propertyUuid.get().setValue((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID));
412                         componentInstanceBusinessLogic.createOrUpdatePropertiesValues(component.getComponentType(), component.getUniqueId(), instance.getUniqueId(), Lists.newArrayList(propertyUuid.get()), user.getUserId())
413                         .right()
414                         .forEach(e -> LOGGER.debug("Failed to update property {} of the instance {} of the component {}. ", SERVICE_UUID_RPOPERTY, instance.getUniqueId(), component.getName()));
415                 }
416         }
417         return instance;
418         }
419     
420     private boolean isAllottedResource(String uniqueId){
421         ComponentParametersView filters = new ComponentParametersView(true);
422         filters.setIgnoreCategories(false);
423         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getResourceRes = toscaOperationFacade.getToscaElement(uniqueId, filters);
424         if(getResourceRes.isRight()){
425                 return false;
426         }
427         if(getResourceRes.left().value().getCategories() != null && getResourceRes.left().value().getCategories().get(0)!= null){
428                 return "Allotted Resource".equals(getResourceRes.left().value().getCategories().get(0).getName());
429         }
430         return false;
431     }
432     
433     private boolean isAllottedVf(org.openecomp.sdc.be.model.Component component){
434         if(component.getComponentType() != ComponentTypeEnum.RESOURCE){
435                 return false;
436         }
437         if(((Resource)component).getResourceType() != ResourceTypeEnum.VF){
438                 return false;
439         }
440         return isAllottedResource(component.getUniqueId());
441     }
442     
443     private Either<ComponentInstance, ResponseFormat> upgradeServiceProxyInstance(org.openecomp.sdc.be.model.Component component, ComponentInstance instance, ComponentInstance newComponentInstance) {
444         Either<List<GraphVertex>, TitanOperationStatus> getLatestOriginServiceRes = getLatestCertifiedService(instance.getSourceModelInvariant());
445         if (getLatestOriginServiceRes.isRight()) {
446             return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertTitanStatusToStorageStatus(getLatestOriginServiceRes.right().value()), instance.getOriginType().getComponentType())));
447         }
448         newComponentInstance.setComponentVersion((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.VERSION));
449         newComponentInstance.setSourceModelUid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
450         newComponentInstance.setSourceModelName((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.NAME));
451         newComponentInstance.setSourceModelUuid((String) getLatestOriginServiceRes.left().value().get(0).getJsonMetadataField(JsonPresentationFields.UUID));
452         return changeAssetVersion(component, instance, newComponentInstance);
453     }
454
455     private Either<List<GraphVertex>, TitanOperationStatus> getLatestCertifiedService(String invariantUUID) {
456
457         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
458         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
459         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
460         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
461         propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUUID);
462         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
463         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
464         return titanDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
465     }
466
467     private Either<ComponentInstance, ResponseFormat> changeAssetVersion(org.openecomp.sdc.be.model.Component containerComponent, ComponentInstance instance, ComponentInstance newComponentInstance) {
468         return componentInstanceBusinessLogic.changeComponentInstanceVersion(ComponentTypeEnum.SERVICE_PARAM_NAME, containerComponent.getUniqueId(), instance.getUniqueId(), user.getUserId(), newComponentInstance);
469     }
470
471     private boolean upgradeNodeTypes() {
472         LOGGER.info("Starting upgrade node types upon upgrade migration 1710 process. ");
473         String toscaConformanceLevel = ConfigurationManager.getConfigurationManager().getConfiguration().getToscaConformanceLevel();
474         Map<String, List<String>> resourcesForUpgrade = ConfigurationManager.getConfigurationManager().getConfiguration().getResourcesForUpgrade();
475         Map<String, org.openecomp.sdc.be.model.Component> upgradedNodeTypesMap = new HashMap<>();
476         List<String> nodeTypes;
477         if (resourcesForUpgrade.containsKey(toscaConformanceLevel)) {
478             nodeTypes = resourcesForUpgrade.get(toscaConformanceLevel);
479             if (nodeTypes != null && !nodeTypes.isEmpty()) {
480                 Either<List<String>, TitanOperationStatus> getRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.NODE_TYPE, ComponentTypeEnum.RESOURCE);
481                 if (getRes.isRight()) {
482                     return false;
483                 }
484                 List<String> allNodeTypes = getRes.left().value();
485
486                 for (String toscaResourceName : nodeTypes) {
487                     Either<List<GraphVertex>, StorageOperationStatus> status = getLatestByName(GraphPropertyEnum.TOSCA_RESOURCE_NAME, toscaResourceName);
488                     if (status.isRight()) {
489                         LOGGER.error("Failed to find node type {} ", toscaResourceName);
490                         return false;
491                     }
492                     List<GraphVertex> vList = status.left().value();
493                     for (GraphVertex vertex : vList) {
494                         StorageOperationStatus updateRes = upgradeNodeType(vertex, upgradedNodeTypesMap, allNodeTypes, nodeTypes);
495                         if (updateRes != StorageOperationStatus.OK) {
496                             return false;
497                         }
498                     }
499                 }
500             }
501         }
502         return true;
503     }
504
505     private boolean upgradeVFs() {
506         return upgradeVFs(false);
507     }
508     
509     private boolean upgradeAllottedVFs() {
510         LOGGER.info("Starting upgrade {} allotted Vfs with upon upgrade migration 1710 process. ", vfAllottedResources.size());
511         return upgradeVFs(true);
512     }
513     
514     private boolean upgradeVFs(boolean allottedVfsUpgrade) {
515         LOGGER.info("Starting upgrade VFs upon upgrade migration 1710 process. ");
516         Either<List<String>, TitanOperationStatus> getVfsRes = getAllLatestCertifiedComponentUids(VertexTypeEnum.TOPOLOGY_TEMPLATE, ComponentTypeEnum.RESOURCE);
517         if (getVfsRes.isRight()) {
518             LOGGER.info(UPGRADE_VFS_FAILED);
519             return false;
520         }
521         for (String currUid : getVfsRes.left().value()) {
522             boolean result = true;
523             try {
524                 result = upgradeVf(currUid, allottedVfsUpgrade);
525                 if (!result && !skipIfUpgradeVfFailed) {
526                     return false;
527                 }
528             } catch (Exception e) {
529                 LOGGER.error("The exception {} occured upon upgrade VFs. ", e);
530                 result = false;
531                 if (!skipIfUpgradeVfFailed) {
532                    return false;
533                 }
534             }
535             finally {
536                 if (!result) {
537                     LOGGER.error("Failed to upgrade RESOURCE with uniqueId {} ", currUid);
538                     titanDao.rollback();
539                 }
540                 else {
541                     LOGGER.info("RESOURCE upgrade finished successfully: uniqueId {} ", currUid);
542                     titanDao.commit();
543                 }
544             }
545         }
546         LOGGER.info("Upgrade VFs upon upgrade migration 1710 process finished successfully. ");
547         return true;
548     }
549
550     private boolean upgradeVf(String uniqueId, boolean allottedVfsUpgrade) {
551         LOGGER.info("Starting upgrade VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
552         Either<String, StorageOperationStatus> latestVersionRes;
553         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getRes = toscaOperationFacade.getToscaElement(uniqueId);
554         if (getRes.isRight()) {
555             LOGGER.debug("Failed to fetch VF with uniqueId {} upon upgrade migration 1710 process. ", uniqueId);
556             outputHandler.addRecord(ComponentTypeEnum.RESOURCE.name(), UNKNOWN, UNKNOWN, uniqueId, MigrationResult.MigrationStatus.FAILED.name(), getRes.right().value());
557             return false;
558         }
559         if(!allottedVfsUpgrade && isAllottedVf(getRes.left().value())){
560                 vfAllottedResources.add(uniqueId);
561                 return true;
562         }
563         if (StringUtils.isNotEmpty(getRes.left().value().getCsarUUID())) {
564             LOGGER.info("Going to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID());
565             latestVersionRes = csarOperation.getCsarLatestVersion(getRes.left().value().getCsarUUID(), user);
566             if (latestVersionRes.isRight()) {
567                 LOGGER.debug("Failed to fetch the latest version of VSP with csarUUID {} upon upgrade migration 1710 process. ", getRes.left().value().getCsarUUID());
568                 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());
569                 return false;
570             }
571             if (isGreater(latestVersionRes.left().value(), getRes.left().value().getCsarVersion())) {
572                 return upgradeVfWithLatestVsp(getRes.left().value(), latestVersionRes);
573             }
574             if (!isVfcUpgradeRequired) {
575                 LOGGER.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());
576             }
577         }
578         return upgradeComponentWithLatestGeneric(getRes.left().value());
579     }
580
581     private boolean upgradeVfWithLatestVsp(org.openecomp.sdc.be.model.Component vf, Either<String, StorageOperationStatus> latestVersionRes) {
582         LOGGER.info("Starting upgrade vf with name {}, invariantUUID {}, version {} and latest VSP version {} upon upgrade migration 1710 process. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value());
583         LOGGER.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion());
584         Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = lifecycleBusinessLogic.changeComponentState(vf.getComponentType(), vf.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
585         if (checkouRes.isRight()) {
586             outputHandler.addRecord(vf.getComponentType().name(), vf.getName(), vf.getUUID(), vf.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage());
587             return false;
588         }
589         LOGGER.info("Starting update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value());
590         Resource resourceToUpdate = new Resource(((Resource) checkouRes.left().value()).getComponentMetadataDefinition());
591         resourceToUpdate.setDerivedFromGenericType(((Resource) checkouRes.left().value()).getDerivedFromGenericType());
592         resourceToUpdate.setDerivedFromGenericVersion(((Resource) checkouRes.left().value()).getDerivedFromGenericVersion());
593         resourceToUpdate.setCsarVersion(Double.toString(Double.parseDouble(latestVersionRes.left().value())));
594         Either<Resource, ResponseFormat> updateResourceFromCsarRes = resourceBusinessLogic.validateAndUpdateResourceFromCsar(resourceToUpdate, user, null, null, resourceToUpdate.getUniqueId());
595         if (updateResourceFromCsarRes.isRight()) {
596             outputHandler.addRecord(resourceToUpdate.getComponentType().name(), resourceToUpdate.getName(), resourceToUpdate.getUUID(), resourceToUpdate.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), updateResourceFromCsarRes.right().value().getFormattedMessage());
597             LOGGER.info("Failed to update vf with name {}, invariantUUID {}, version {} and latest VSP {}. ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value());
598             return false;
599         }
600         Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkouRes.left().value());
601         if (certifyRes.isRight()) {
602             LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), LifeCycleTransitionEnum.CERTIFY);
603             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());
604             return false;
605         }
606         LOGGER.info("Full certification of vf with name {}, invariantUUID {}, version {} finished . ", vf.getName(), vf.getInvariantUUID(), vf.getVersion(), latestVersionRes.left().value());
607         outputHandler.addRecord(certifyRes.left().value().getComponentType().name(), certifyRes.left().value().getName(), certifyRes.left().value().getUUID(), certifyRes.left().value().getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), UpgradeStatus.UPGRADED);
608         return true;
609     }
610
611     private boolean upgradeComponentWithLatestGeneric(org.openecomp.sdc.be.model.Component component) {
612         String derivedFromGenericType = component.getDerivedFromGenericType();
613         String derivedFromGenericVersion = component.getDerivedFromGenericVersion();
614         org.openecomp.sdc.be.model.Component updatedComponent = component;
615         if (StringUtils.isNotEmpty(derivedFromGenericType) && !latestGenericTypes.containsKey(derivedFromGenericType)) {
616             LOGGER.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);
617             LOGGER.info("Starting to fetch latest generic node type {}. ", derivedFromGenericType);
618             Either<List<GraphVertex>, TitanOperationStatus> getDerivedRes = findDerivedResources(derivedFromGenericType);
619             if (getDerivedRes.isRight()) {
620                 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), getDerivedRes.right().value());
621                 LOGGER.info("Failed to upgrade component with name {}, invariantUUID {}, version {} and latest generic. Status is {}. ", component.getName(), component.getInvariantUUID(), component.getVersion(), derivedFromGenericType);
622                 return false;
623             }
624             latestGenericTypes.put(derivedFromGenericType, getDerivedRes.left().value().get(0));
625         }
626         if (StringUtils.isEmpty(derivedFromGenericType) ||
627                 latestVersionExists(latestGenericTypes.get(derivedFromGenericType), derivedFromGenericVersion) ||
628                 isVfcUpgradeRequired) {
629             if (StringUtils.isNotEmpty(derivedFromGenericType))
630                 LOGGER.info("Newer version {} of derived from generic type {} exists. ", latestGenericTypes.get(derivedFromGenericType).getJsonMetadataField(JsonPresentationFields.VERSION), derivedFromGenericType);
631             else
632                 LOGGER.info("The vf resource with name {}, invariantUUID {}, version {},  has an empty derivedFromGenericType field. ", component.getName(), component.getInvariantUUID(), component.getVersion());
633
634             LOGGER.info("Starting to perform check out of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion());
635             Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
636             if (checkouRes.isRight()) {
637                 LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CHECKOUT);
638                 outputHandler.addRecord(component.getComponentType().name(), component.getName(), component.getInvariantUUID(), component.getUniqueId(), MigrationResult.MigrationStatus.FAILED.name(), checkouRes.right().value().getFormattedMessage());
639                 return false;
640             }
641             //update included VFCs, if it is required as per configuration
642             if (CollectionUtils.isNotEmpty(checkouRes.left().value().getComponentInstances())) {
643                 LOGGER.info("VFC upgrade is required: updating components of vf with name {}, invariantUUID {}, version {}. ", component.getName(), component.getInvariantUUID(), component.getVersion());
644                 Either<org.openecomp.sdc.be.model.Component, ResponseFormat> updateCompositionRes =
645                         updateComposition(checkouRes.left().value());
646                 if (updateCompositionRes.isRight()) {
647                     LOGGER.error(FAILED_TO_UPGRADE_COMPONENT, checkouRes.left().value().getComponentType().name(), checkouRes.left().value().getName(), checkouRes.left().value().getInvariantUUID(), checkouRes.left().value().getVersion(), "updateComposition", updateCompositionRes.right().value().getFormattedMessage());
648                     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());
649                     return false;
650                 }
651             }
652             Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> certifyRes = performFullCertification(checkouRes.left().value());
653             if (certifyRes.isRight()) {
654                 LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY);
655                 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());
656                 return false;
657             }
658             updatedComponent = certifyRes.left().value();
659         } else {
660             LOGGER.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());
661         }
662         LOGGER.info(UPGRADE_COMPONENT_SUCCEEDED, component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion());
663         outputHandler.addRecord(updatedComponent.getComponentType().name(), updatedComponent.getName(), updatedComponent.getUUID(), updatedComponent.getUniqueId(), MigrationResult.MigrationStatus.COMPLETED.name(), updatedComponent.equals(component) ? UpgradeStatus.NOT_UPGRADED : UpgradeStatus.UPGRADED);
664         return true;
665     }
666
667     private StorageOperationStatus upgradeNodeType(GraphVertex nodeTypeV, Map<String, org.openecomp.sdc.be.model.Component> upgradedNodeTypesMap, List<String> allCertifiedUids, List<String> nodeTypes) {
668         StorageOperationStatus result = StorageOperationStatus.OK;
669         LOGGER.info("Starting upgrade node type with name {}, invariantUUID {}, version{}. ", nodeTypeV.getMetadataProperty(GraphPropertyEnum.NAME), nodeTypeV.getMetadataProperty(GraphPropertyEnum.INVARIANT_UUID), nodeTypeV.getMetadataProperty(GraphPropertyEnum.VERSION));
670         LOGGER.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));
671         Either<List<GraphVertex>, TitanOperationStatus> parentResourceRes = titanDao.getParentVertecies(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseMetadata);
672         if (parentResourceRes.isRight() && parentResourceRes.right().value() != TitanOperationStatus.NOT_FOUND) {
673             return DaoStatusConverter.convertTitanStatusToStorageStatus(parentResourceRes.right().value());
674
675         }
676         List<GraphVertex> derivedResourcesUid = new ArrayList<>();
677         if (parentResourceRes.isLeft()) {
678             for (GraphVertex chV : parentResourceRes.left().value()) {
679                 Optional<String> op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny();
680                 if (op.isPresent()) {
681                     derivedResourcesUid.add(chV);
682                 }
683             }
684         }
685         String uniqueId = (String) nodeTypeV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID);
686         Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> getRes = toscaOperationFacade.getToscaElement(uniqueId);
687         if (getRes.isRight()) {
688             LOGGER.info("failed to fetch element with uniqueId {} ", uniqueId);
689             return getRes.right().value();
690         }
691
692         org.openecomp.sdc.be.model.Resource nt = (Resource) getRes.left().value();
693         boolean isNeedToUpgrade = true;
694         if (upgradedNodeTypesMap.containsKey(nt.getToscaResourceName()) || nodeTypes.stream().anyMatch(p -> p.equals(nt.getToscaResourceName()))) {
695             isNeedToUpgrade = false;
696         }
697         if (isNeedToUpgrade) {
698             LOGGER.info("Starting to perform check out of node type with name {}, invariantUUID {}, version {}. ", nt.getName(), nt.getInvariantUUID(), nt.getVersion());
699             Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> checkouRes = lifecycleBusinessLogic.changeComponentState(nt.getComponentType(), nt.getUniqueId(), user, LifeCycleTransitionEnum.CHECKOUT, changeInfo, true, false);
700             if (checkouRes.isRight()) {
701                 return StorageOperationStatus.GENERAL_ERROR;
702             }
703             org.openecomp.sdc.be.model.Component upgradetComp = checkouRes.left().value();
704             boolean res = performFullCertification(upgradetComp).isLeft();
705             if (!res) {
706                 return StorageOperationStatus.GENERAL_ERROR;
707             }
708             upgradedNodeTypesMap.put(nt.getToscaResourceName(), upgradetComp);
709             titanDao.commit();
710         }
711         for (GraphVertex chV : derivedResourcesUid) {
712             result = upgradeNodeType(chV, upgradedNodeTypesMap, allCertifiedUids, nodeTypes);
713             LOGGER.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);
714         }
715         return result;
716     }
717
718     private Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> performFullCertification(org.openecomp.sdc.be.model.Component component) {
719         LOGGER.info("Starting to perform full certification of {} with name {}, invariantUUID {}, version {}. ",
720                 component.getComponentType().getValue(), component.getName(), component.getInvariantUUID(), component.getVersion());
721
722         Either<? extends org.openecomp.sdc.be.model.Component, ResponseFormat> changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), component.getUniqueId(), user, LifeCycleTransitionEnum.CERTIFICATION_REQUEST, changeInfo, true, false);
723         if (changeStateEither.isRight()) {
724             LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFICATION_REQUEST);
725             return changeStateEither;
726         }
727         changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.START_CERTIFICATION, changeInfo, true, false);
728         if (changeStateEither.isRight()) {
729             LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.START_CERTIFICATION);
730             return changeStateEither;
731         }
732         changeStateEither = lifecycleBusinessLogic.changeComponentState(component.getComponentType(), changeStateEither.left().value().getUniqueId(), user, LifeCycleTransitionEnum.CERTIFY, changeInfo, true, false);
733         if (changeStateEither.isRight()) {
734             LOGGER.info(FAILED_TO_CHANGE_STATE_OF_COMPONENT, component.getName(), component.getInvariantUUID(), component.getVersion(), LifeCycleTransitionEnum.CERTIFY);
735         } else {
736             LOGGER.info("Full certification of {} with name {}, invariantUUID {}, version {} finished successfully",
737                     changeStateEither.left().value().getComponentType().getValue(), changeStateEither.left().value().getName(),
738                     changeStateEither.left().value().getInvariantUUID(), changeStateEither.left().value().getVersion());
739         }
740         return changeStateEither;
741     }
742
743     private Either<List<GraphVertex>, TitanOperationStatus> findDerivedResources(String parentResource) {
744         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
745         propertiesToMatch.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
746
747         propertiesToMatch.put(GraphPropertyEnum.TOSCA_RESOURCE_NAME, parentResource);
748         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
749
750         return titanDao.getByCriteria(VertexTypeEnum.NODE_TYPE, propertiesToMatch, JsonParseFlagEnum.ParseMetadata);
751     }
752
753     private boolean latestVersionExists(GraphVertex latestDerivedFrom, String currentVersion) {
754         return isGreater((String) latestDerivedFrom.getJsonMetadataField(JsonPresentationFields.VERSION), currentVersion);
755     }
756
757     private boolean isGreater(String latestVersion, String currentVersion) {
758         if (latestVersion != null && currentVersion == null)
759             return true;
760         if (latestVersion == null)
761             return false;
762         return Double.parseDouble(latestVersion) > Double.parseDouble(currentVersion);
763     }
764
765     private Either<List<String>, TitanOperationStatus> getAllLatestCertifiedComponentUids(VertexTypeEnum vertexType, ComponentTypeEnum componentType) {
766         LOGGER.info("Starting to fetch all latest certified not checked out components with type {} upon upgrade migration 1710 process", componentType);
767         Either<List<String>, TitanOperationStatus> result = null;
768         Map<String, String> latestCertifiedMap = new HashMap<>();
769         Map<String, String> latestNotCertifiedMap = new HashMap<>();
770
771         Either<List<GraphVertex>, TitanOperationStatus> getComponentsRes = getAllLatestCertifiedComponents(vertexType, componentType);
772         if (getComponentsRes.isRight() && getComponentsRes.right().value() != TitanOperationStatus.NOT_FOUND) {
773             LOGGER.error("Failed to fetch all latest certified not checked out components with type {}. Status is {}. ", componentType, getComponentsRes.right().value());
774             result = Either.right(getComponentsRes.right().value());
775         }
776         if (getComponentsRes.isRight()) {
777             result = Either.left(new ArrayList<>());
778         }
779         if (result == null) {
780             for (GraphVertex component : getComponentsRes.left().value()) {
781                 String invariantUUID = (String) component.getJsonMetadataField(JsonPresentationFields.INVARIANT_UUID);
782                 if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
783                     latestCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
784                 } else {
785                     latestNotCertifiedMap.put(invariantUUID, (String) component.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID));
786                 }
787             }
788             result = Either.left(latestCertifiedMap.entrySet().stream().filter(e -> !latestNotCertifiedMap.containsKey(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList()));
789         }
790         return result;
791     }
792
793     private Either<List<GraphVertex>, TitanOperationStatus> getAllLatestCertifiedComponents(VertexTypeEnum vertexType, ComponentTypeEnum componentType) {
794
795         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
796         propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name());
797         propertiesToMatch.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true);
798
799         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
800         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
801         if (vertexType == VertexTypeEnum.TOPOLOGY_TEMPLATE && componentType == ComponentTypeEnum.RESOURCE)
802             propertiesNotToMatch.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.CVFC.name());
803         return titanDao.getByCriteria(vertexType, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
804     }
805
806     protected Either<List<String>, TitanOperationStatus> findResourcesPathRecursively(GraphVertex nodeTypeV, List<String> allCertifiedUids) {
807         Either<List<GraphVertex>, TitanOperationStatus> parentResourceRes = titanDao.getParentVertecies(nodeTypeV, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseMetadata);
808         if (parentResourceRes.isRight()) {
809             return Either.right(parentResourceRes.right().value());
810         }
811         List<GraphVertex> derivedResourcesUid = new ArrayList<>();
812         for (GraphVertex chV : parentResourceRes.left().value()) {
813             Optional<String> op = allCertifiedUids.stream().filter(id -> id.equals((String) chV.getJsonMetadataField(JsonPresentationFields.UNIQUE_ID))).findAny();
814             if (op.isPresent()) {
815                 derivedResourcesUid.add(chV);
816             }
817         }
818         return null;
819     }
820
821     private Either<List<GraphVertex>, StorageOperationStatus> getLatestByName(GraphPropertyEnum property, String nodeName) {
822
823         Map<GraphPropertyEnum, Object> propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class);
824         Map<GraphPropertyEnum, Object> propertiesNotToMatch = new EnumMap<>(GraphPropertyEnum.class);
825
826         propertiesToMatch.put(property, nodeName);
827         propertiesNotToMatch.put(GraphPropertyEnum.IS_DELETED, true);
828
829         Either<List<GraphVertex>, TitanOperationStatus> highestResources = titanDao.getByCriteria(null, propertiesToMatch, propertiesNotToMatch, JsonParseFlagEnum.ParseMetadata);
830         if (highestResources.isRight()) {
831             TitanOperationStatus status = highestResources.right().value();
832             LOGGER.debug("Failed to fetch resource with name {}. Status is {} ", nodeName, status);
833             return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(status));
834         }
835         List<GraphVertex> resources = highestResources.left().value();
836         List<GraphVertex> result = new ArrayList<>();
837         for (GraphVertex component : resources) {
838             if (((String) component.getJsonMetadataField(JsonPresentationFields.LIFECYCLE_STATE)).equals(LifecycleStateEnum.CERTIFIED.name())) {
839                 result.add(component);
840             }
841         }
842         return Either.left(result);
843     }
844
845 }