[SDC] rebase 1710 code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / migration / v1604 / ServiceMigration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
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.impl.migration.v1604;
22
23 import static java.nio.file.Files.readAllBytes;
24 import static java.nio.file.Paths.get;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34 import java.util.UUID;
35
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
42 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
43 import org.openecomp.sdc.be.dao.graph.datatype.RelationEndPoint;
44 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
45 import org.openecomp.sdc.be.dao.neo4j.GraphEdgePropertiesDictionary;
46 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
47 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
48 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
49 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
50 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
51 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
52 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
53 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
54 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
55 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
56 import org.openecomp.sdc.be.model.CapabilityDefinition;
57 import org.openecomp.sdc.be.model.Component;
58 import org.openecomp.sdc.be.model.ComponentInstance;
59 import org.openecomp.sdc.be.model.LifecycleStateEnum;
60 import org.openecomp.sdc.be.model.PropertyDefinition;
61 import org.openecomp.sdc.be.model.RequirementDefinition;
62 import org.openecomp.sdc.be.model.Resource;
63 import org.openecomp.sdc.be.model.User;
64 import org.openecomp.sdc.be.model.category.CategoryDefinition;
65 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
66 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
67 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
68 import org.openecomp.sdc.be.model.operations.impl.AdditionalInformationOperation;
69 import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation;
70 import org.openecomp.sdc.be.model.operations.impl.ComponentOperation;
71 import org.openecomp.sdc.be.model.operations.impl.LifecycleOperation;
72 import org.openecomp.sdc.be.model.operations.impl.ProductOperation;
73 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
74 import org.openecomp.sdc.be.model.operations.impl.ResourceOperation;
75 import org.openecomp.sdc.be.model.operations.impl.ServiceOperation;
76 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
77 import org.openecomp.sdc.be.resources.data.CapabilityData;
78 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
79 import org.openecomp.sdc.be.resources.data.ComponentMetadataData;
80 import org.openecomp.sdc.be.resources.data.ProductMetadataData;
81 import org.openecomp.sdc.be.resources.data.RelationshipInstData;
82 import org.openecomp.sdc.be.resources.data.RequirementData;
83 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
84 import org.openecomp.sdc.be.resources.data.ServiceMetadataData;
85 import org.openecomp.sdc.be.resources.data.TagData;
86 import org.openecomp.sdc.be.resources.data.category.CategoryData;
87 import org.openecomp.sdc.be.resources.data.category.SubCategoryData;
88 import org.openecomp.sdc.be.utils.CommonBeUtils;
89 import org.openecomp.sdc.common.util.ValidationUtils;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92 import org.springframework.beans.factory.annotation.Autowired;
93 import org.yaml.snakeyaml.Yaml;
94
95 import com.thinkaurelius.titan.core.TitanEdge;
96 import com.thinkaurelius.titan.core.TitanGraph;
97 import com.thinkaurelius.titan.core.TitanVertex;
98
99 import fj.data.Either;
100
101 public class ServiceMigration {
102
103         private static final String[] NORMATIVE_OLD_NAMES = { "tosca.nodes.network.Network", "tosca.nodes.network.Port", "tosca.nodes.BlockStorage", "tosca.nodes.Compute", "tosca.nodes.Container.Application", "tosca.nodes.Container.Runtime",
104                         "tosca.nodes.Database", "tosca.nodes.DBMS", "tosca.nodes.LoadBalancer", "tosca.nodes.ObjectStorage", "tosca.nodes.Root", "tosca.nodes.SoftwareComponent", "tosca.nodes.WebApplication", "tosca.nodes.WebServer", };
105
106         private static Logger log = LoggerFactory.getLogger(ServiceMigration.class.getName());
107
108         @Autowired
109         protected TitanGenericDao titanGenericDao;
110         @Autowired
111         protected ResourceOperation resourceOperation;
112         @Autowired
113         protected ServiceOperation serviceOperation;
114         @Autowired
115         protected ProductOperation productOperation;
116         @Autowired
117         protected LifecycleOperation lifecycleOperaion;
118         @Autowired
119         protected PropertyOperation propertyOperation;
120         @Autowired
121         protected AdditionalInformationOperation additionalInformationOperation;
122         @Autowired
123         protected ComponentInstanceOperation componentInstanceOperaion;
124         @Autowired
125         protected IElementOperation elementOperation;
126
127         public boolean migrate1602to1604(String appConfigDir) {
128
129                 boolean result = false;
130
131                 try {
132
133                         if (!addResourceCounterToResources()) {
134                                 log.debug("Failed to update resource instance counter on resources");
135                                 result = false;
136                                 return result;
137                         }
138                         if (!updateComponentInstanceType()) {
139                                 log.debug("Failed to update component instance type");
140                                 result = false;
141                                 return result;
142                         }
143                         // fix VF
144                         if (!fixDerivedVf()) {
145                                 log.debug("Failed to fix VFs");
146                                 result = false;
147                                 return result;
148                         }
149                         // update instances and relation
150                         if (!updateCalculatedEdges()) {
151                                 log.debug("Failed to update calculated edges for VF instances");
152                                 result = false;
153                                 return result;
154                         }
155                         // update instances and relation
156                         if (!updateRelations()) {
157                                 log.debug("Failed to update Instance And Relations in services");
158                                 result = false;
159                                 return result;
160                         }
161                         if (!updateCategories(appConfigDir)) {
162                                 log.debug("Failed to update categories");
163                                 result = false;
164                                 return result;
165                         }
166
167                         if (!AllowMultipleHeats.removeAndUpdateHeatPlaceHolders(titanGenericDao, log, true)) {
168                                 log.error("Failed to update heat place holders");
169                                 result = false;
170                                 return result;
171                         }
172
173                         if (!AddGroupUuid.addGroupUuids(titanGenericDao, log, true)) {
174                                 log.error("Failed to update group UUIDs");
175                                 result = false;
176                                 return result;
177                         }
178
179                         result = true;
180                 } finally {
181                         if (!result) {
182                                 titanGenericDao.rollback();
183                         } else {
184                                 titanGenericDao.commit();
185                         }
186                 }
187                 return result;
188         }
189
190         private boolean updateCategories(String appConfigDir) {
191                 // String categoryMigrationFile = appConfigDir + File.separator +
192                 // "categoryMigration.yaml";
193                 String categoryMigrationFile = appConfigDir + "categoryMigration.yaml";
194
195                 Map<String, List<MigrationCategory>> categoriesFromYml;
196                 try {
197                         categoriesFromYml = createCategoriesFromYml(categoryMigrationFile);
198                         if (categoriesFromYml == null || categoriesFromYml.isEmpty()) {
199                                 log.debug("updateCategories failed to load categories form migration file {}", categoryMigrationFile);
200                                 return false;
201                         }
202                 } catch (Exception e) {
203                         log.debug("Failed to load category migration file : {}", categoryMigrationFile, e);
204                         return false;
205                 }
206                 for (Map.Entry<String, List<MigrationCategory>> entry : categoriesFromYml.entrySet()) {
207                         ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(entry.getKey());
208                         if (componentType != null) {
209                                 switch (componentType) {
210                                 case RESOURCE:
211                                         if (updateResourceCategories(entry.getValue()) == false) {
212                                                 log.debug("updateCategories failed to update resource categories");
213                                                 return false;
214                                         }
215                                         break;
216                                 case SERVICE:
217                                         if (updateServiceCategories(entry.getValue()) == false) {
218                                                 log.debug("updateCategories failed to update service categories");
219                                                 return false;
220                                         }
221                                         break;
222                                 default:
223                                         log.debug("updateCategories no changes for categories from type {}", componentType);
224                                 }
225                         } else {
226                                 log.debug("updateCategories failed not supported component file in migration categories file" + entry.getKey());
227                                 return false;
228                         }
229                 }
230                 return true;
231         }
232
233         private boolean updateServiceCategories(List<MigrationCategory> categories) {
234                 log.debug("updateServiceCategories STARTED");
235                 Either<List<CategoryDefinition>, ActionStatus> serviceCategories = elementOperation.getAllCategories(NodeTypeEnum.ServiceNewCategory, true);
236                 if (serviceCategories.isRight()) {
237                         log.debug("updateServiceCategories failed fetch all service categories ,error " + serviceCategories.right().value());
238                         return false;
239                 }
240                 for (MigrationCategory newCat : categories) {
241
242                         if (newCat.getOldName() == null) {
243                                 // add new
244                                 boolean exist = false;
245                                 for (CategoryDefinition catInDB : serviceCategories.left().value()) {
246                                         if (newCat.getName().equals(catInDB.getName())) {
247                                                 exist = true;
248                                                 break;
249                                         }
250                                 }
251                                 if (!exist) {
252                                         CategoryDefinition categoryDefinition = new CategoryDefinition(newCat);
253                                         Either<CategoryDefinition, ActionStatus> result = elementOperation.createCategory(categoryDefinition, NodeTypeEnum.ServiceNewCategory, true);
254                                         if (result.isRight()) {
255                                                 log.debug("Failed to create service category {} error {}", categoryDefinition, result.right().value());
256                                                 return false;
257                                         }
258                                         log.debug("service category {} created", categoryDefinition);
259                                 }
260                         } else {
261                                 // update exist
262                                 for (CategoryDefinition catInDB : serviceCategories.left().value()) {
263                                         if (newCat.getOldName().equals(catInDB.getName())) {
264                                                 Either<CategoryData, TitanOperationStatus> updateSingleResult = updateSingleResourceCategory(newCat, NodeTypeEnum.ServiceNewCategory);
265                                                 if (updateSingleResult.isRight()) {
266                                                         return false;
267                                                 }
268                                                 break;
269                                         }
270                                 }
271                         }
272                 }
273                 log.debug("updateServiceCategories ENDED");
274                 return true;
275         }
276
277         private Either<CategoryData, TitanOperationStatus> updateSingleResourceCategory(MigrationCategory newCat, NodeTypeEnum nodetype) {
278                 Map<String, Object> properties = new HashMap<>();
279                 properties.put(GraphPropertiesDictionary.NAME.getProperty(), newCat.getOldName());
280                 Either<List<CategoryData>, TitanOperationStatus> categoryEither = titanGenericDao.getByCriteria(nodetype, properties, CategoryData.class);
281                 if (categoryEither.isRight() && categoryEither.right().value() != TitanOperationStatus.NOT_FOUND) {
282                         log.debug("Failed to get {} categories, for name {} error {}", nodetype, newCat.getOldName(), categoryEither.right().value());
283                         return Either.right(categoryEither.right().value());
284                 }
285                 List<CategoryData> categoryList = (categoryEither.isLeft() ? categoryEither.left().value() : null);
286                 if (categoryList == null) {
287                         log.debug("No {} categories, for name {} error {}", nodetype, newCat.getOldName());
288                         return Either.right(TitanOperationStatus.NOT_FOUND);
289                 }
290                 CategoryData categoryData = categoryList.get(0);
291                 categoryData.getCategoryDataDefinition().setName(newCat.getName());
292                 categoryData.getCategoryDataDefinition().setIcons(newCat.getIcons());
293                 categoryData.getCategoryDataDefinition().setNormalizedName(ValidationUtils.normalizeCategoryName4Uniqueness(newCat.getName()));
294                 Either<CategoryData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(categoryData, CategoryData.class);
295                 if (updateNode.isRight()) {
296                         log.debug("Failed to update {} category {} error {}", nodetype, categoryData, updateNode.right().value());
297                         return Either.right(updateNode.right().value());
298                 }
299                 log.debug("Update {} category {} ", nodetype, categoryData);
300                 return Either.left(updateNode.left().value());
301         }
302
303         private boolean updateResourceCategories(List<MigrationCategory> categories) {
304                 log.debug("updateResourceCategories STARTED");
305                 Either<List<CategoryDefinition>, ActionStatus> resourceCategories = elementOperation.getAllCategories(NodeTypeEnum.ResourceNewCategory, true);
306                 if (resourceCategories.isRight()) {
307                         log.debug("updateResourceCategories failed fetch all resource categories ,error " + resourceCategories.right().value());
308                         return false;
309                 }
310                 for (MigrationCategory newCat : categories) {
311                         if (newCat.getOldName() == null) {
312                                 // add new
313                                 // check if already created in previous running
314                                 boolean exist = false;
315                                 for (CategoryDefinition catInDB : resourceCategories.left().value()) {
316                                         if (newCat.getName().equals(catInDB.getName())) {
317                                                 exist = true;
318                                         }
319                                 }
320                                 if (!exist) {
321                                         CategoryDefinition categoryDefinition = new CategoryDefinition(newCat);
322                                         Either<CategoryDefinition, ActionStatus> resultCat = elementOperation.createCategory(categoryDefinition, NodeTypeEnum.ResourceNewCategory, true);
323                                         if (resultCat.isRight()) {
324                                                 log.debug("Failed to create resource category {} error {}", categoryDefinition, resultCat.right().value());
325                                                 return false;
326                                         }
327                                         log.debug("resource category {} created", categoryDefinition);
328
329                                         List<MigrationSubCategory> nSubCat = newCat.getSubcategories();
330                                         List<MigrationSubCategory> newSubcat = nSubCat;
331                                         List<MigrationSubCategory> subcategories = newSubcat;
332                                         for (MigrationSubCategory msubcat : subcategories) {
333                                                 SubCategoryDefinition subcat = new SubCategoryDefinition(msubcat);
334                                                 Either<SubCategoryDefinition, ActionStatus> resultSubcat = elementOperation.createSubCategory(resultCat.left().value().getUniqueId(), subcat, NodeTypeEnum.ResourceSubcategory, true);
335                                                 if (resultSubcat.isRight()) {
336                                                         log.debug("Failed to create resource sub category {} error {}", subcat, resultSubcat.right().value());
337                                                         return false;
338                                                 }
339                                                 log.debug("resource sub category {} created for category {}", categoryDefinition, resultCat.left().value().getName());
340                                         }
341                                 }
342                         } else {
343                                 // update exist
344                                 for (CategoryDefinition catInDB : resourceCategories.left().value()) {
345                                         if (newCat.getOldName().equals(catInDB.getName())) {
346                                                 Either<CategoryData, TitanOperationStatus> updateSingleResult = updateSingleResourceCategory(newCat, NodeTypeEnum.ResourceNewCategory);
347                                                 if (updateSingleResult.isRight()) {
348                                                         return false;
349                                                 }
350
351                                                 CategoryData categoryData = updateSingleResult.left().value();
352                                                 for (MigrationSubCategory migSubCat : newCat.getSubcategories()) {
353                                                         if (migSubCat.getOldName() == null) {
354                                                                 // create new one
355                                                                 boolean existSub = false;
356                                                                 for (SubCategoryDefinition subCatInDb : catInDB.getSubcategories()) {
357                                                                         if (subCatInDb.getName().equals(migSubCat.getName())) {
358                                                                                 existSub = true;
359                                                                         }
360                                                                 }
361                                                                 if (!existSub) {
362                                                                         SubCategoryDefinition subcat = new SubCategoryDefinition(migSubCat);
363
364                                                                         Either<SubCategoryDefinition, ActionStatus> resultSubcat = elementOperation.createSubCategory((String) categoryData.getUniqueId(), subcat, NodeTypeEnum.ResourceSubcategory, true);
365                                                                         if (resultSubcat.isRight()) {
366                                                                                 log.debug("Failed to create resource sub category {} error {}", subcat, resultSubcat.right().value());
367                                                                                 return false;
368                                                                         }
369                                                                         log.debug("resource sub category {} created for category {}", categoryData, resultSubcat.left().value().getName());
370                                                                 }
371                                                         } else {
372                                                                 if (updateSingleSubCategory(newCat, migSubCat, updateSingleResult.left().value()) == false) {
373                                                                         return false;
374                                                                 }
375                                                         }
376                                                 }
377                                                 break;
378                                         }
379                                 }
380                         }
381                 }
382                 return true;
383         }
384
385         private boolean updateSingleSubCategory(MigrationCategory newCat, MigrationSubCategory migSubCat, CategoryData categoryData) {
386
387                 Either<List<ImmutablePair<SubCategoryData, GraphEdge>>, TitanOperationStatus> subcategories = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceNewCategory), (String) categoryData.getUniqueId(),
388                                 GraphEdgeLabels.SUB_CATEGORY, NodeTypeEnum.ResourceSubcategory, SubCategoryData.class);
389
390                 if (subcategories.isRight()) {
391                         log.debug("Failed to get resource sub categories, for name {} error {}", newCat.getOldName(), subcategories.right().value());
392                         return false;
393                 }
394
395                 for (ImmutablePair<SubCategoryData, GraphEdge> pair : subcategories.left().value()) {
396                         if (pair.getKey().getSubCategoryDataDefinition().getName().equals(migSubCat.getOldName())) {
397                                 SubCategoryData subCategoryData = pair.getKey();
398                                 subCategoryData.getSubCategoryDataDefinition().setName(migSubCat.getName());
399                                 subCategoryData.getSubCategoryDataDefinition().setIcons(migSubCat.getIcons());
400                                 subCategoryData.getSubCategoryDataDefinition().setNormalizedName(ValidationUtils.normalizeCategoryName4Uniqueness(migSubCat.getName()));
401                                 Either<SubCategoryData, TitanOperationStatus> updateSubNode = titanGenericDao.updateNode(subCategoryData, SubCategoryData.class);
402                                 if (updateSubNode.isRight()) {
403                                         log.debug("Failed to update resource sub category {} error {}", subCategoryData, updateSubNode.right().value());
404                                         return false;
405                                 }
406                                 log.debug("Update resource subcategory category {} ", subCategoryData);
407                                 break;
408                         }
409                 }
410                 return true;
411         }
412
413         private Map<String, List<MigrationCategory>> createCategoriesFromYml(String categoriesTypesYml) {
414                 String yamlAsString;
415                 try {
416                         yamlAsString = new String(readAllBytes(get(categoriesTypesYml)));
417                 } catch (Exception e) {
418                         log.debug("Failed to load category import file exception : ", e);
419                         return null;
420                 }
421
422                 log.debug("received yaml: {}", yamlAsString);
423
424                 Map<String, Object> toscaJson = (Map<String, Object>) new Yaml().load(yamlAsString);
425                 Map<String, List<MigrationCategory>> allCategories = new HashMap<>();
426
427                 Iterator<Entry<String, Object>> categoryEntryItr = toscaJson.entrySet().iterator();
428                 while (categoryEntryItr.hasNext()) {
429                         Entry<String, Object> categoryTypeEntry = categoryEntryItr.next();
430                         String categoryType = categoryTypeEntry.getKey();
431                         List<MigrationCategory> categoriesPerType = null;
432                         Map<String, Object> categoryPerType = null;
433                         switch (categoryType) {
434                         case ComponentTypeEnum.SERVICE_PARAM_NAME:
435                                 categoryPerType = (Map<String, Object>) categoryTypeEntry.getValue();
436                                 categoriesPerType = createServiceCategories(categoryPerType);
437                                 break;
438                         case ComponentTypeEnum.RESOURCE_PARAM_NAME:
439                                 categoryPerType = (Map<String, Object>) categoryTypeEntry.getValue();
440                                 categoriesPerType = createResourceCategories(categoryPerType);
441                                 break;
442                         case ComponentTypeEnum.PRODUCT_PARAM_NAME:
443                                 // TODO
444                                 break;
445                         default:
446                                 log.debug("Not supported category type - {}", categoryType);
447                                 break;
448                         }
449                         if (categoriesPerType != null) {
450                                 allCategories.put(categoryType, categoriesPerType);
451                         }
452                 }
453                 return allCategories;
454         }
455
456         private List<MigrationCategory> createServiceCategories(Map<String, Object> categories) {
457                 List<MigrationCategory> categroiesDef = new ArrayList<>();
458                 String catName = null;
459                 List<String> icons = null;
460                 String oldName = null;
461                 for (Entry<String, Object> entry : categories.entrySet()) {
462                         MigrationCategory catDef = new MigrationCategory();
463                         Map<String, Object> category = (Map<String, Object>) entry.getValue();
464                         catName = (String) category.get("name");
465                         catDef.setName(catName);
466                         icons = (List<String>) category.get("icons");
467                         catDef.setIcons(icons);
468                         String normalizedName = ValidationUtils.normalizeCategoryName4Uniqueness(catName);
469                         catDef.setNormalizedName(normalizedName);
470                         oldName = (String) category.get("oldName");
471                         catDef.setOldName(oldName);
472                         categroiesDef.add(catDef);
473                 }
474
475                 return categroiesDef;
476         }
477
478         private List<MigrationCategory> createResourceCategories(Map<String, Object> categoryPerType) {
479                 List<MigrationCategory> categroiesDef = new ArrayList<>();
480                 for (Map.Entry<String, Object> entry : categoryPerType.entrySet()) {
481                         Map<String, Object> category = (Map<String, Object>) entry.getValue();
482                         MigrationCategory catDef = new MigrationCategory();
483                         String catName = (String) category.get("name");
484                         catDef.setName(catName);
485                         String normalizedName = ValidationUtils.normalizeCategoryName4Uniqueness(catName);
486                         catDef.setNormalizedName(normalizedName);
487                         String oldName = (String) category.get("oldName");
488                         catDef.setOldName(oldName);
489
490                         Map<String, Object> subcategories = (Map<String, Object>) category.get("subcategories");
491                         List<MigrationSubCategory> subcateDef = new ArrayList<>();
492                         for (Entry<String, Object> subcategory : subcategories.entrySet()) {
493                                 Map<String, Object> subcategoryInfo = (Map<String, Object>) subcategory.getValue();
494                                 MigrationSubCategory subDef = new MigrationSubCategory();
495                                 String subcategoryName = (String) subcategoryInfo.get("name");
496                                 subDef.setName(subcategoryName);
497                                 List<String> subcategoryIcons = (List<String>) subcategoryInfo.get("icons");
498                                 subDef.setIcons(subcategoryIcons);
499                                 normalizedName = ValidationUtils.normalizeCategoryName4Uniqueness(subcategoryName);
500                                 subDef.setNormalizedName(normalizedName);
501                                 oldName = (String) subcategoryInfo.get("oldName");
502                                 subDef.setOldName(oldName);
503
504                                 subcateDef.add(subDef);
505                         }
506
507                         catDef.setSubcategories(subcateDef);
508                         categroiesDef.add(catDef);
509                 }
510                 return categroiesDef;
511         }
512
513         private boolean updateCalculatedEdges() {
514                 log.debug("update calculated edges STARTED");
515
516                 Either<List<ComponentInstanceData>, TitanOperationStatus> allInstances = titanGenericDao.getByCriteria(NodeTypeEnum.ResourceInstance, null, ComponentInstanceData.class);
517                 if (allInstances.isRight() && !allInstances.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
518                         log.debug("updateCalculatedEdges failed fetch all resource instances ,error " + allInstances.right().value());
519                         return false;
520                 }
521                 if (allInstances.isRight() && allInstances.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
522                         log.debug("updateCalculatedEdges - no VFs");
523                         return true;
524                 }
525                 List<ComponentInstanceData> listOfInstances = allInstances.left().value();
526                 for (ComponentInstanceData instance : listOfInstances) {
527                         // check if already have calculated edges
528                         log.debug("start handle instance {}", instance.getUniqueId());
529                         boolean needProcess = true;
530                         Either<List<ImmutablePair<CapabilityData, GraphEdge>>, TitanOperationStatus> vfci = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), instance.getUniqueId(),
531                                         GraphEdgeLabels.CALCULATED_CAPABILITY, NodeTypeEnum.Capability, CapabilityData.class);
532                         if (vfci.isRight()) {
533                                 if (!vfci.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
534                                         log.debug("createCalculatedCapabilitiesForInstance failed to fetch instance for resource " + instance.getComponentInstDataDefinition().getComponentUid() + " error " + vfci.right().value());
535                                         return false;
536                                 }
537                         } else {
538                                 if (vfci.left().value().size() > 0) {
539                                         needProcess = false;
540                                 }
541                         }
542                         Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus> vfciReq = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), instance.getUniqueId(),
543                                         GraphEdgeLabels.CALCULATED_REQUIREMENT, NodeTypeEnum.Requirement, RequirementData.class);
544                         if (vfciReq.isRight()) {
545                                 if (!vfciReq.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
546                                         log.debug("createCalculatedCapabilitiesForInstance failed to fetch instance for resource " + instance.getComponentInstDataDefinition().getComponentUid() + " error " + vfciReq.right().value());
547                                         return false;
548                                 }
549                         } else {
550                                 if (vfciReq.left().value().size() > 0) {
551                                         needProcess = false;
552                                 }
553                         }
554                         Either<List<ImmutablePair<RequirementData, GraphEdge>>, TitanOperationStatus> vfciReqFF = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), instance.getUniqueId(),
555                                         GraphEdgeLabels.CALCULATED_REQUIREMENT_FULLFILLED, NodeTypeEnum.Requirement, RequirementData.class);
556                         if (vfciReqFF.isRight()) {
557
558                                 if (!vfciReqFF.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
559                                         log.debug("createCalculatedCapabilitiesForInstance failed to fetch instance for resource " + instance.getComponentInstDataDefinition().getComponentUid() + " error " + vfciReqFF.right().value());
560                                         return false;
561                                 }
562                         } else {
563                                 if (vfciReqFF.left().value().size() > 0) {
564                                         needProcess = false;
565                                 }
566                         }
567
568                         if (needProcess == false) {
569                                 log.debug("updateCalculatedEdges : for instance {} calculated capabilty/requirement already created", instance.getUniqueId());
570                                 continue;
571                         }
572                         String originId = instance.getComponentInstDataDefinition().getComponentUid();
573                         Either<Resource, StorageOperationStatus> resourceE = resourceOperation.getResource(originId, true);
574                         if (resourceE.isRight()) {
575                                 log.debug("updateCalculatedEdges failed to fetch origin resource with id {} error {}", originId, resourceE.right().value());
576                                 return false;
577                         }
578                         Resource resource = resourceE.left().value();
579                         Map<String, List<RequirementDefinition>> requirements = resource.getRequirements();
580                         if (createCalculatedRequirementsForInstance(instance, requirements) != true) {
581                                 return false;
582                         }
583                         Map<String, List<CapabilityDefinition>> capabilities = resource.getCapabilities();
584                         if (createCalculatedCapabilitiesForInstance(instance, capabilities) != true) {
585                                 return false;
586                         }
587                         log.debug("finish handle instance {}", instance.getUniqueId());
588                 }
589                 log.debug("update calculated edges ENDED");
590                 return true;
591         }
592
593         private boolean createCalculatedCapabilitiesForInstance(ComponentInstanceData instance, Map<String, List<CapabilityDefinition>> capabilities) {
594                 for (Map.Entry<String, List<CapabilityDefinition>> entry : capabilities.entrySet()) {
595                         for (CapabilityDefinition capability : entry.getValue()) {
596                                 Either<CapabilityData, TitanOperationStatus> capNode = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Capability), capability.getUniqueId(), CapabilityData.class);
597                                 if (capNode.isRight()) {
598                                         log.debug("createCalculatedCapabilitiesForInstance failed to fetch capability node  with id " + capability.getUniqueId() + " error " + capNode.right().value());
599                                         return false;
600                                 }
601                                 Map<String, Object> props = new HashMap<>();
602                                 props.put(GraphEdgePropertiesDictionary.NAME.getProperty(), capability.getName());
603                                 if (fillEdgeProperties(instance, props) != true) {
604                                         return false;
605                                 }
606
607                                 Either<GraphRelation, TitanOperationStatus> createRelation = titanGenericDao.createRelation(instance, capNode.left().value(), GraphEdgeLabels.CALCULATED_CAPABILITY, props);
608                                 if (createRelation.isRight()) {
609                                         TitanOperationStatus titanOperationStatus = createRelation.right().value();
610                                         log.debug("Failed to create calculated requirement from component instance {} to requirement {}, error: {}", instance.getUniqueId(), capNode.left().value().getUniqueId(), titanOperationStatus);
611                                         return false;
612                                 }
613                                 log.debug("CALCULATED_CAPABILITY was created from {} to {} with props : {}", capNode.left().value().getUniqueId(), instance.getUniqueId(), props);
614                         }
615                 }
616                 return true;
617         }
618
619         private boolean fillEdgeProperties(ComponentInstanceData instance, Map<String, Object> props) {
620                 if (instance.getComponentInstDataDefinition().getOriginType().equals(OriginTypeEnum.VF)) {
621                         Either<List<ImmutablePair<ComponentInstanceData, GraphEdge>>, TitanOperationStatus> vfci = titanGenericDao.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource),
622                                         instance.getComponentInstDataDefinition().getComponentUid(), GraphEdgeLabels.RESOURCE_INST, NodeTypeEnum.ResourceInstance, ComponentInstanceData.class);
623                         if (vfci.isRight()) {
624                                 log.debug("createCalculatedCapabilitiesForInstance failed to fetch instance for resource " + instance.getComponentInstDataDefinition().getComponentUid() + " error " + vfci.right().value());
625                                 return false;
626                         }
627                         ImmutablePair<ComponentInstanceData, GraphEdge> immutablePair = vfci.left().value().get(0);
628                         String vfciId = immutablePair.getLeft().getUniqueId();
629                         props.put(GraphEdgePropertiesDictionary.OWNER_ID.getProperty(), vfciId);
630                         props.put(GraphEdgePropertiesDictionary.SOURCE.getProperty(), immutablePair.getLeft().getComponentInstDataDefinition().getComponentUid());
631
632                 } else {
633                         props.put(GraphEdgePropertiesDictionary.OWNER_ID.getProperty(), instance.getUniqueId());
634                         props.put(GraphEdgePropertiesDictionary.SOURCE.getProperty(), instance.getComponentInstDataDefinition().getComponentUid());
635                 }
636                 return true;
637         }
638
639         private boolean createCalculatedRequirementsForInstance(ComponentInstanceData instance, Map<String, List<RequirementDefinition>> requirements) {
640                 for (Map.Entry<String, List<RequirementDefinition>> entry : requirements.entrySet()) {
641                         for (RequirementDefinition requirement : entry.getValue()) {
642                                 Either<RequirementData, TitanOperationStatus> reqNode = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Requirement), requirement.getUniqueId(), RequirementData.class);
643                                 if (reqNode.isRight()) {
644                                         log.debug("updateCalculatedEdges failed to fetch requirement node  with id " + requirement.getUniqueId() + " error " + reqNode.right().value());
645                                         return false;
646                                 }
647                                 Map<String, Object> props = new HashMap<>();
648                                 props.put(GraphEdgePropertiesDictionary.NAME.getProperty(), requirement.getName());
649
650                                 if (fillEdgeProperties(instance, props) != true) {
651                                         return false;
652                                 }
653
654                                 Either<GraphRelation, TitanOperationStatus> createRelation = titanGenericDao.createRelation(instance, reqNode.left().value(), GraphEdgeLabels.CALCULATED_REQUIREMENT, props);
655                                 if (createRelation.isRight()) {
656                                         TitanOperationStatus titanOperationStatus = createRelation.right().value();
657                                         log.debug("Failed to create calculated requirement from component instance {} to requirement {}, error: {}", instance.getUniqueId(), reqNode.left().value().getUniqueId(), titanOperationStatus);
658                                         return false;
659                                 }
660                                 log.debug("CALCULATED_REQUIREMENT was created from {} to {} with props : {}", reqNode.left().value().getUniqueId(), instance.getUniqueId(), props);
661                         }
662                 }
663                 return true;
664         }
665
666         private boolean updateRelations() {
667                 log.debug("update relations and edges STARTED");
668                 Either<List<RelationshipInstData>, TitanOperationStatus> allRelations = titanGenericDao.getByCriteria(NodeTypeEnum.RelationshipInst, null, RelationshipInstData.class);
669                 if (allRelations.isRight()) {
670                         if (allRelations.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
671                                 log.debug("updateRelations : No relations to update. updateRelations ENDED");
672                                 return true;
673                         }
674                         log.debug("updateRelations : failed to fetch all relation nodes , error ", allRelations.right().value());
675                         return false;
676                 }
677                 for (RelationshipInstData rel : allRelations.left().value()) {
678                         // rel.set
679                         if (rel.getCapabilityOwnerId() != null && rel.getRequirementOwnerId() != null) {
680                                 log.debug("updateRelations : for relation {} all fields alredy fixed -> {}", rel.getUniqueId(), rel);
681                                 continue;
682                         }
683                         // update capability parameters
684                         if (updateCapabiltyFieldsInRelation(rel) != true) {
685                                 return false;
686                         }
687
688                         // update requirement parameters and set calculated edge to full
689                         // filled
690                         if (updateRequirementFieldsInRelation(rel) != true) {
691                                 return false;
692                         }
693
694                         Either<RelationshipInstData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(rel, RelationshipInstData.class);
695                         if (updateNode.isRight()) {
696                                 log.debug("updateRelations : failed to update relation node with id {} , error {}", rel.getUniqueId(), updateNode.right().value());
697                                 return false;
698                         }
699                         log.debug("Relations was updated with values {}", rel);
700                 }
701                 log.debug("update relations and edges ENDED");
702                 return true;
703         }
704
705         private boolean updateRequirementFieldsInRelation(RelationshipInstData rel) {
706                 Either<ImmutablePair<ComponentInstanceData, GraphEdge>, TitanOperationStatus> reqInst = titanGenericDao.getParentNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipInst), rel.getUniqueId(), GraphEdgeLabels.RELATIONSHIP_INST,
707                                 NodeTypeEnum.ResourceInstance, ComponentInstanceData.class);
708                 if (reqInst.isRight()) {
709                         log.debug("updateRelations : failed to fetch capability component instance for relation {}, error {}", rel.getUniqueId(), reqInst.right().value());
710                         return false;
711                 }
712                 ComponentInstanceData requirementInstanceData = reqInst.left().value().getLeft();
713                 ComponentInstanceDataDefinition reqRI = requirementInstanceData.getComponentInstDataDefinition();
714                 if (reqRI.getOriginType().equals(OriginTypeEnum.VF)) {
715                         Either<ImmutablePair<ComponentInstanceData, GraphEdge>, TitanOperationStatus> vfcInstInOrigVf = titanGenericDao.getChildByEdgeCriteria(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), reqRI.getComponentUid(),
716                                         GraphEdgeLabels.RESOURCE_INST, NodeTypeEnum.ResourceInstance, ComponentInstanceData.class, null);
717                         if (vfcInstInOrigVf.isRight()) {
718                                 log.debug("updateRelations : failed to fetch VFC instance in origin VF with id  " + reqRI.getComponentUid() + ", error ", vfcInstInOrigVf.right().value());
719                                 return false;
720                         }
721                         rel.setRequirementOwnerId(vfcInstInOrigVf.left().value().getLeft().getUniqueId());
722                 } else {
723                         rel.setRequirementOwnerId(reqRI.getUniqueId());
724                 }
725                 // get vertex
726                 Either<TitanVertex, TitanOperationStatus> vertexReqRI = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), requirementInstanceData.getUniqueId());
727                 if (vertexReqRI.isRight()) {
728                         log.debug("updateRelations : failed to fetch veterx for instance  {}, error {}", requirementInstanceData.getUniqueId(), vertexReqRI.right().value());
729                         return false;
730                 }
731                 String[] splitIds = rel.getUniqueId().split("\\.");
732                 String reqName = splitIds[splitIds.length - 1];
733                 Map<String, Object> props = new HashMap<>();
734                 props.put(GraphEdgePropertiesDictionary.NAME.getProperty(), reqName);
735                 Either<List<Edge>, TitanOperationStatus> edgesForNode = titanGenericDao.getOutgoingEdgesByCriteria(vertexReqRI.left().value(), GraphEdgeLabels.CALCULATED_REQUIREMENT, props);
736                 if (edgesForNode.isRight()) {
737                         log.debug("updateRelations : failed to fetch edges for instance {}  error {}", requirementInstanceData.getUniqueId(), edgesForNode.right().value());
738                         return false;
739                 }
740                 Edge edge = edgesForNode.left().value().get(0);
741                 String reqId = (String) titanGenericDao.getProperty((TitanVertex) edge.inVertex(), GraphPropertiesDictionary.UNIQUE_ID.getProperty());
742                 rel.setRequirementId(reqId);
743
744                 // change edge label
745                 TitanEdge newEdge = (TitanEdge) vertexReqRI.left().value().addEdge(GraphEdgeLabels.CALCULATED_REQUIREMENT_FULLFILLED.getProperty(), edge.inVertex());
746                 titanGenericDao.setProperties(newEdge, titanGenericDao.getProperties(edge));
747                 edge.remove();
748
749                 log.debug("Edge was changed to CALCULATED_REQUIREMENT_FULLFILLED for relation between {} and {}", reqId, requirementInstanceData.getUniqueId());
750
751                 return true;
752         }
753
754         public boolean updateCapabiltyFieldsInRelation(RelationshipInstData rel) {
755                 // update capability parameters
756                 Either<ImmutablePair<ComponentInstanceData, GraphEdge>, TitanOperationStatus> capInst = titanGenericDao.getChildByEdgeCriteria(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.RelationshipInst), rel.getUniqueId(),
757                                 GraphEdgeLabels.CAPABILITY_NODE, NodeTypeEnum.ResourceInstance, ComponentInstanceData.class, null);
758                 if (capInst.isRight()) {
759                         log.debug("updateRelations : failed to fetch capabilty component instance for relation {}, error {}", rel.getUniqueId(), capInst.right().value());
760                         return false;
761                 }
762                 ComponentInstanceData capabiltyInstanceData = capInst.left().value().getLeft();
763                 ComponentInstanceDataDefinition capRI = capabiltyInstanceData.getComponentInstDataDefinition();
764                 if (capRI.getOriginType().equals(OriginTypeEnum.VF)) {
765                         Either<ImmutablePair<ComponentInstanceData, GraphEdge>, TitanOperationStatus> vfcInstInOrigVf = titanGenericDao.getChildByEdgeCriteria(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), capRI.getComponentUid(),
766                                         GraphEdgeLabels.RESOURCE_INST, NodeTypeEnum.ResourceInstance, ComponentInstanceData.class, null);
767                         if (vfcInstInOrigVf.isRight()) {
768                                 log.debug("updateRelations : failed to fetch VFC instance in origin VF with id  " + capRI.getComponentUid() + ", error ", vfcInstInOrigVf.right().value());
769                                 return false;
770                         }
771                         rel.setCapabilityOwnerId(vfcInstInOrigVf.left().value().getLeft().getUniqueId());
772                 } else {
773                         rel.setCapabilityOwnerId(capRI.getUniqueId());
774                 }
775
776                 // get vertex
777                 Either<TitanVertex, TitanOperationStatus> vertexCapRI = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), capabiltyInstanceData.getUniqueId());
778                 if (vertexCapRI.isRight()) {
779                         log.debug("updateRelations : failed to fetch veterx for instance {} , error {}", capabiltyInstanceData.getUniqueId(), vertexCapRI.right().value());
780                         return false;
781                 }
782                 // String[] splitIds = rel.getUniqueId().split("\\.");
783                 String capName = (String) capInst.left().value().getRight().getProperties().get(GraphEdgePropertiesDictionary.NAME.getProperty());// splitIds[splitIds.length
784                                                                                                                                                                                                                                                                                         // - 1];
785                 Map<String, Object> props = new HashMap<>();
786                 props.put(GraphEdgePropertiesDictionary.NAME.getProperty(), capName);
787                 Either<List<Edge>, TitanOperationStatus> edgesForNode = titanGenericDao.getOutgoingEdgesByCriteria(vertexCapRI.left().value(), GraphEdgeLabels.CALCULATED_CAPABILITY, props);
788                 if (edgesForNode.isRight()) {
789                         log.debug("updateRelations : failed to fetch edges for instance {} , error {}", capabiltyInstanceData.getUniqueId(), edgesForNode.right().value());
790                         return false;
791                 }
792                 Edge edge = edgesForNode.left().value().get(0);
793                 String capId = (String) titanGenericDao.getProperty((TitanVertex) edge.inVertex(), GraphPropertiesDictionary.UNIQUE_ID.getProperty());
794                 rel.setCapabiltyId(capId);
795
796                 return true;
797         }
798
799         private Either<List<String>, StorageOperationStatus> handleVfGroup(ResourceMetadataData metadata) {
800                 Map<String, Object> props = new HashMap<String, Object>();
801                 props.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VF.name());
802                 props.put(GraphPropertiesDictionary.NAME.getProperty(), metadata.getMetadataDataDefinition().getName());
803
804                 List<String> finished = new ArrayList<>();
805
806                 Either<List<ResourceMetadataData>, TitanOperationStatus> allVFByName = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, props, ResourceMetadataData.class);
807                 if (allVFByName.isRight()) {
808                         log.debug("fixDerivedFv failed fetch all VF resources,error {}", allVFByName.right().value());
809                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
810                 }
811                 Set<String> nonDuplicatedId = new HashSet<>();
812                 String uuid10 = null;
813                 for (ResourceMetadataData mdata : allVFByName.left().value()) {
814                         String version = mdata.getMetadataDataDefinition().getVersion();
815                         if (version.equals("1.0")) {
816                                 uuid10 = mdata.getMetadataDataDefinition().getUUID();
817                                 // break;
818                         }
819                         nonDuplicatedId.add((String) mdata.getUniqueId());
820                 }
821                 if (uuid10 == null) {
822                         uuid10 = allVFByName.left().value().get(0).getMetadataDataDefinition().getUUID();
823                 }
824                 props.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VF.name());
825                 props.put(GraphPropertiesDictionary.UUID.getProperty(), uuid10);
826
827                 Either<List<ResourceMetadataData>, TitanOperationStatus> allVFByUUID = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, props, ResourceMetadataData.class);
828                 if (allVFByUUID.isRight()) {
829                         log.debug("fixDerivedFv failed fetch all VF resources by UUID {} ,error {}", uuid10, allVFByUUID.right().value());
830                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
831                 }
832                 for (ResourceMetadataData mdata : allVFByUUID.left().value()) {
833                         nonDuplicatedId.add((String) mdata.getUniqueId());
834                 }
835                 Either<TitanGraph, TitanOperationStatus> graph = titanGenericDao.getGraph();
836                 if (graph.isRight()) {
837                         log.debug("fixDerivedFv failed - No titan graph ,error {}", graph.right().value());
838                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
839                 }
840                 // Map<String, String> derivedMapping = new HashMap<>();
841                 for (String resourceId : nonDuplicatedId) {
842                         // StorageOperationStatus handleSingleVfResult =
843                         // handleSingleVf(finished, derivedMapping, resourceId);
844                         StorageOperationStatus handleSingleVfResult = handleSingleVf(finished, resourceId);
845                         if (!handleSingleVfResult.equals(StorageOperationStatus.OK)) {
846                                 log.debug("fixDerivedFv failed - handleSingleVfResult failed for resource {} ,error {}", resourceId, handleSingleVfResult);
847                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
848                         }
849                 }
850                 return Either.left(finished);
851         }
852
853         // private StorageOperationStatus handleSingleVf(List<String> finished,
854         // Map<String, String> derivedMapping, String resourceId) {
855         private StorageOperationStatus handleSingleVf(List<String> finished, String resourceId) {
856                 Either<TitanVertex, TitanOperationStatus> vertexByProperty = titanGenericDao.getVertexByProperty(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), resourceId);
857                 if (vertexByProperty.isRight()) {
858                         log.debug("fixDerivedFv failed to fetch resource by id {} ,error {}", resourceId, vertexByProperty.right().value());
859                         return StorageOperationStatus.GENERAL_ERROR;
860                 }
861                 Vertex vertexR = vertexByProperty.left().value();
862                 Iterator<Vertex> vertexDIter = vertexR.vertices(Direction.OUT, GraphEdgeLabels.DERIVED_FROM.getProperty());
863                 if (vertexDIter != null && vertexDIter.hasNext()) {
864                         // move edges
865                         // must be only one
866                         TitanVertex vertexD = (TitanVertex) vertexDIter.next();
867                         String idDerived = (String) titanGenericDao.getProperty(vertexD, GraphPropertiesDictionary.UNIQUE_ID.getProperty());
868
869                         // TODO clone resource
870
871                         // TODO add instance of new resource to VF
872
873                         // add to vf instance of vfc
874                         finished.add(resourceId);
875                 } else {
876                         log.debug("No derived edges for resource  id {}", resourceId);
877                 }
878                 return StorageOperationStatus.OK;
879         }
880
881         private boolean updateComponentInstanceType() {
882                 log.debug("update component instances type STARTED");
883                 Either<List<ComponentInstanceData>, TitanOperationStatus> allInstances = titanGenericDao.getByCriteria(NodeTypeEnum.ResourceInstance, null, ComponentInstanceData.class);
884                 if (allInstances.isRight()) {
885                         if (allInstances.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
886                                 log.debug("updateComponentInstanceType:  no instances ti update ");
887                                 return true;
888                         }
889                         log.debug("updateComponentInstanceType failed fetch all resource instances ,error " + allInstances.right().value());
890                         return false;
891                 }
892
893                 List<ComponentInstanceData> listOfInstances = allInstances.left().value();
894                 for (ComponentInstanceData instance : listOfInstances) {
895                         String originId = instance.getComponentInstDataDefinition().getComponentUid();
896                         Either<ComponentMetadataData, TitanOperationStatus> nodeResource = titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), originId, ComponentMetadataData.class);
897                         if (nodeResource.isRight()) {
898                                 log.debug("updateComponentInstanceType failed to fetch origin resource with id {} error {}", originId, nodeResource.right().value());
899                                 return false;
900                         }
901                         ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) nodeResource.left().value().getMetadataDataDefinition()).getResourceType();
902                         if (resourceType == null) {
903                                 log.debug("updateComponentInstanceType failed, no resource type for origin resource with id " + originId);
904                                 return false;
905                         }
906                         OriginTypeEnum originType;
907                         switch (resourceType) {
908                         case VF:
909                                 originType = OriginTypeEnum.VF;
910                                 break;
911                         case VFC:
912                                 originType = OriginTypeEnum.VFC;
913                                 break;
914                         case VL:
915                                 originType = OriginTypeEnum.VL;
916                                 break;
917                         case CP:
918                                 originType = OriginTypeEnum.CP;
919                                 break;
920                         case CVFC:
921                                 originType = OriginTypeEnum.CVFC;
922                                 break;
923                         default:
924                                 log.debug("updateComponentInstanceType failed, no supported resource type {} for origin resource with id {}", resourceType, originId);
925                                 return false;
926                         }
927                         instance.getComponentInstDataDefinition().setOriginType(originType);
928
929                         Either<ComponentInstanceData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(instance, ComponentInstanceData.class);
930                         if (updateNode.isRight()) {
931                                 log.debug("updateComponentInstanceType failed, failed to update component instance node with id  " + instance.getUniqueId() + " error " + updateNode.right().value());
932                                 return false;
933                         }
934                         log.debug("For instance with id {} the origin type was detected as {}", instance.getUniqueId(), originType);
935                 }
936                 log.debug("update component instances type ENDED");
937                 return true;
938         }
939
940         private boolean addResourceCounterToResources() {
941
942                 Either<List<ResourceMetadataData>, TitanOperationStatus> allResources = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, null, ResourceMetadataData.class);
943                 if (allResources.isRight()) {
944                         if (allResources.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
945                                 log.debug("addResourceCounterToResources - no resources");
946                                 return true;
947                         }
948                         log.debug("addResourceCounterToResources failed fetch all resources,error {}", allResources.right().value());
949                         return false;
950                 }
951                 for (ResourceMetadataData resource : allResources.left().value()) {
952                         Either<TitanVertex, TitanOperationStatus> vertexByProperty = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resource.getUniqueId());
953                         if (vertexByProperty.isRight()) {
954                                 log.error("failed to add instanceCounter to VF {} . error is: {}", resource.getUniqueId(), vertexByProperty.right().value().name());
955                                 return false;
956                         }
957                         Vertex vfVertex = vertexByProperty.left().value();
958                         if (!vfVertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty()).isPresent()) {
959                                 vfVertex.property(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty(), 0);
960                         }
961                 }
962                 return true;
963         }
964
965         private boolean fixDerivedVf() {
966
967                 Map<String, Object> props = new HashMap<String, Object>();
968                 props.put(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty(), ResourceTypeEnum.VF.name());
969                 Either<List<ResourceMetadataData>, TitanOperationStatus> allVF = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, props, ResourceMetadataData.class);
970                 if (allVF.isRight()) {
971                         if (allVF.right().value().equals(TitanOperationStatus.NOT_FOUND)) {
972                                 log.debug("fixDerivedVf - no VFs");
973                                 return true;
974                         }
975                         log.debug("fixDerivedFv failed fetch all VF resources,error {}", allVF.right().value());
976                         return false;
977                 }
978
979                 Map<String, String> vfUuidToVfcUuid = new HashMap<String, String>();
980                 for (ResourceMetadataData metadata : allVF.left().value()) {
981                         Either<Resource, StorageOperationStatus> eitherResource = resourceOperation.getResource(metadata.getMetadataDataDefinition().getUniqueId(), true);
982                         if (eitherResource.isRight()) {
983                                 log.error("failed to migrate VF {} from version 1602 to version 1604. error is: {}", metadata.getMetadataDataDefinition().getUniqueId(), eitherResource.right().value().name());
984                                 return false;
985                         }
986                         Resource vfResource = eitherResource.left().value();
987                         if (vfResource.getDerivedFrom() == null || vfResource.getDerivedFrom().isEmpty()) {
988                                 continue;
989                         }
990                         Boolean isVfDeleted = vfResource.getIsDeleted();
991                         String vfUUID = vfResource.getUUID();
992                         String vfcUUID = vfUuidToVfcUuid.getOrDefault(vfUUID, null);
993                         if (vfcUUID == null) {
994                                 vfcUUID = UUID.randomUUID().toString();
995                                 vfUuidToVfcUuid.put(vfUUID, vfcUUID);
996                         }
997
998                         // handle lifecycle
999                         String vfUniqueId = vfResource.getUniqueId();
1000                         LifecycleStateEnum vfcTargetState = vfResource.getLifecycleState();
1001                         if (vfcTargetState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION) || vfcTargetState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
1002                                 User user = new User();
1003                                 user.setUserId(vfResource.getLastUpdaterUserId());
1004                                 Either<? extends Component, StorageOperationStatus> checkinComponent = lifecycleOperaion.checkinComponent(NodeTypeEnum.Resource, vfResource, user, user, true);
1005                                 if (checkinComponent.isRight()) {
1006                                         log.error("failed to checkin VF {}. error={}", vfUniqueId, checkinComponent.right().value().name());
1007                                         return false;
1008                                 }
1009                         } else if (vfcTargetState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
1010                                 vfcTargetState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
1011                         }
1012
1013                         // delete VF Properties
1014                         List<PropertyDefinition> properties = vfResource.getProperties();
1015                         if (properties != null && !properties.isEmpty()) {
1016                                 Either<Map<String, PropertyDefinition>, StorageOperationStatus> deleteAllProperties = propertyOperation.deleteAllPropertiesAssociatedToNode(NodeTypeEnum.Resource, vfUniqueId);
1017                                 if (deleteAllProperties.isRight() && !deleteAllProperties.right().value().equals(StorageOperationStatus.NOT_FOUND) && !deleteAllProperties.right().value().equals(StorageOperationStatus.OK)) {
1018                                         log.error("failed to delete properties of VF {} . error is: {}", metadata.getMetadataDataDefinition().getUniqueId(), deleteAllProperties.right().value().name());
1019                                         return false;
1020                                 }
1021                         }
1022                         // delete VF Additional Info
1023                         List<AdditionalInformationDefinition> additionalInformation = vfResource.getAdditionalInformation();
1024                         if (additionalInformation != null && !additionalInformation.isEmpty()) {
1025                                 Either<AdditionalInformationDefinition, StorageOperationStatus> deleteAllAdditionalInformationParameters = additionalInformationOperation.deleteAllAdditionalInformationParameters(NodeTypeEnum.Resource, vfUniqueId, true);
1026                                 if (deleteAllAdditionalInformationParameters.isRight() && !deleteAllAdditionalInformationParameters.right().value().equals(StorageOperationStatus.OK)
1027                                                 && !deleteAllAdditionalInformationParameters.right().value().equals(StorageOperationStatus.NOT_FOUND)) {
1028                                         log.error("failed to delete properties of VF {} . error is: {}", metadata.getMetadataDataDefinition().getUniqueId(), deleteAllAdditionalInformationParameters.right().value().name());
1029                                         return false;
1030                                 }
1031                         }
1032                         // delete VF derivedFrom
1033                         GraphRelation derivedFromRelation = new GraphRelation(GraphEdgeLabels.DERIVED_FROM.getProperty());
1034                         derivedFromRelation.setFrom(new RelationEndPoint(NodeTypeEnum.Resource, UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), vfUniqueId));
1035                         Either<GraphRelation, TitanOperationStatus> deleteDerivedFromRelation = titanGenericDao.deleteOutgoingRelation(derivedFromRelation);
1036                         if (deleteDerivedFromRelation.isRight()) {
1037                                 log.error("failed to delete derivedFrom relation of VF {} . error is: {}", metadata.getMetadataDataDefinition().getUniqueId(), deleteDerivedFromRelation.right().value().name());
1038                                 return false;
1039                         }
1040
1041                         // create VFC
1042                         Either<Resource, StorageOperationStatus> createVFC = createVFC(metadata, vfResource, vfcUUID, vfcTargetState);
1043                         if (createVFC.isRight()) {
1044                                 log.error("failed to split VF {} to VFC. error is: {}", metadata.getMetadataDataDefinition().getUniqueId(), createVFC.right().value().name());
1045                                 return false;
1046                         }
1047                         Resource vfcResource = createVFC.left().value();
1048                         if (!createVfcInstanceOnVf(vfcResource, vfUniqueId)) {
1049                                 return false;
1050                         }
1051                         // update VFC to deleted if required
1052                         if (isVfDeleted != null && isVfDeleted) {
1053                                 Either<Component, StorageOperationStatus> markResourceToDelete = resourceOperation.markComponentToDelete(vfcResource, true);
1054                                 if (markResourceToDelete.isRight()) {
1055                                         log.error("failed to mark isDeleted on VFC {} . error is: {}", vfcResource.getUniqueId(), markResourceToDelete.right().value().name());
1056                                         return false;
1057                                 }
1058                         }
1059
1060                 }
1061                 return true;
1062         }
1063
1064         private Either<Resource, StorageOperationStatus> createVFC(ResourceMetadataData metadata, Resource vfcResource, String uuid, LifecycleStateEnum vfcTargetState) {
1065
1066                 Boolean highestVersion = vfcResource.isHighestVersion();
1067                 // Resource vfcResource = new Resource((ResourceMetadataDefinition)
1068                 // vfResource.getComponentMetadataDefinition());
1069                 // String componentName = vfcResource.getName()+"VFC";
1070                 // vfcResource.setName(componentName);
1071                 // vfcResource.setNormalizedName(ValidationUtils.normaliseComponentName(componentName));
1072                 // vfcResource.setSystemName(ValidationUtils.convertToSystemName(componentName));
1073                 vfcResource.setUniqueId(null);
1074                 vfcResource.setUUID(uuid);
1075                 vfcResource.setAllVersions(null);
1076                 vfcResource.setArtifacts(null);
1077                 vfcResource.setDeploymentArtifacts(null);
1078                 vfcResource.setComponentInstances(null);
1079                 vfcResource.setComponentInstancesProperties(null);
1080                 vfcResource.setComponentInstancesRelations(null);
1081                 vfcResource.setResourceType(ResourceTypeEnum.VFC);
1082                 vfcResource.setIsDeleted(false);
1083
1084                 vfcResource.setLifecycleState(vfcTargetState);
1085                 // vfcResource.setDerivedFrom(vfResource.getDerivedFrom());
1086                 // vfcResource.setProperties(vfResource.getProperties());
1087                 // vfcResource.setAdditionalInformation(vfResource.getAdditionalInformation());
1088                 // vfcResource.setCategories(vfResource.getCategories());
1089                 // vfcResource.setTags(vfResource.getTags());
1090
1091                 Either<Resource, StorageOperationStatus> createResource = resourceOperation.createResource(vfcResource, true);
1092                 if (createResource.isRight()) {
1093                         return createResource;
1094                 }
1095                 Resource afterCreateResource = createResource.left().value();
1096                 Either<TitanVertex, TitanOperationStatus> vertexByProperty = titanGenericDao.getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), afterCreateResource.getUniqueId());
1097                 if (vertexByProperty.isRight()) {
1098                         return createResource;
1099                 }
1100                 Vertex newVfcVertex = vertexByProperty.left().value();
1101                 newVfcVertex.property(GraphPropertiesDictionary.UUID.getProperty(), uuid);
1102                 if (!highestVersion) {
1103                         newVfcVertex.property(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), false);
1104                 }
1105                 return createResource;
1106         }
1107
1108         private boolean createVfcInstanceOnVf(Resource vfcResource, String vfUniqueId) {
1109                 // create VFC instance on VF
1110                 ComponentInstance componentInstance = new ComponentInstance();
1111                 componentInstance.setComponentUid(vfcResource.getUniqueId());
1112                 componentInstance.setPosX("550");
1113                 componentInstance.setPosY("300");
1114                 componentInstance.setName(vfcResource.getName());
1115                 componentInstance.setIcon(vfcResource.getIcon());
1116                 componentInstance.setToscaComponentName(vfcResource.getToscaResourceName());
1117                 Either<String, Boolean> handleNameLogic = handleNameLogic(componentInstance, vfUniqueId, vfcResource.getName());
1118                 if (handleNameLogic.isRight()) {
1119                         log.error("failed to create logical name for vfc instance");
1120                         return false;
1121                 }
1122                 Either<ComponentInstance, StorageOperationStatus> createComponentInstance = componentInstanceOperaion.createComponentInstance(vfUniqueId, NodeTypeEnum.Resource, handleNameLogic.left().value(), componentInstance, NodeTypeEnum.Resource, true);
1123
1124                 if (createComponentInstance.isRight()) {
1125                         log.error("failed to create vfc instance on vf {}. error: {}", vfUniqueId, createComponentInstance.right().value().name());
1126                         return false;
1127                 }
1128                 return true;
1129         }
1130
1131         private Either<String, Boolean> handleNameLogic(ComponentInstance componentInstance, String containerComponentId, String resourceName) {
1132
1133                 Either<Integer, StorageOperationStatus> componentInNumberStatus = resourceOperation.increaseAndGetComponentInstanceCounter(containerComponentId, true);
1134
1135                 if (componentInNumberStatus.isRight()) {
1136                         log.debug("Failed to get component instance number for container component {} ", containerComponentId);
1137                         return Either.right(false);
1138                 }
1139                 String resourceInNumber = componentInNumberStatus.left().value().toString();
1140                 componentInstance.setComponentName(resourceName);
1141                 componentInstance.setName(resourceName);
1142                 String logicalName = componentInstanceOperaion.createComponentInstLogicalName(resourceInNumber, resourceName);
1143
1144                 Boolean eitherValidation = validateComponentInstanceName(logicalName, componentInstance, true);
1145                 if (!eitherValidation) {
1146                         return Either.right(false);
1147                 }
1148
1149                 return Either.left(resourceInNumber);
1150         }
1151
1152         private Boolean validateComponentInstanceName(String resourceInstanceName, ComponentInstance resourceInstance, boolean isCreate) {
1153
1154                 if (!ValidationUtils.validateStringNotEmpty(resourceInstanceName)) {
1155                         return false;
1156                 }
1157                 resourceInstance.setNormalizedName(ValidationUtils.normalizeComponentInstanceName(resourceInstanceName));
1158                 if (!isCreate) {
1159                         if (!ValidationUtils.validateResourceInstanceNameLength(resourceInstanceName)) {
1160                                 return false;
1161                         }
1162                         if (!ValidationUtils.validateResourceInstanceName(resourceInstanceName)) {
1163                                 return false;
1164                         }
1165                 }
1166
1167                 return true;
1168
1169         }
1170
1171         public boolean migrate1604to1607(String appConfigDir) {
1172                 log.debug("Started the migration procedure from version 1604 to version 1607 ...");
1173                 log.debug("Getting all resources with resources");
1174                 boolean result = false;
1175                 Either<Boolean, StorageOperationStatus> resourceEither = null;
1176                 try {
1177                         Either<List<ResourceMetadataData>, TitanOperationStatus> allResources = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, null, ResourceMetadataData.class);
1178                         if (allResources.isRight()) {
1179                                 log.error("Couldn't get resources from DB, error: {}", allResources.right().value());
1180                                 result = false;
1181                                 return result;
1182                         }
1183                         List<ResourceMetadataData> resourcesList = allResources.left().value();
1184                         if (resourcesList == null) {
1185                                 log.error("Couldn't get resources from DB, no resources found");
1186                                 result = false;
1187                                 return result;
1188                         }
1189                         log.debug("Found {} resources", resourcesList.size());
1190                         for (ResourceMetadataData resource : resourcesList) {
1191                                 String resourceName = resource.getMetadataDataDefinition().getName();
1192                                 log.debug("Checking resource {}", resourceName);
1193                                 if (isNormative(resourceName)) {
1194                                         resourceEither = changeNormativeTypeName(resource);
1195                                         if (resourceEither.isRight()) {
1196                                                 log.error("DB error during name changing");
1197                                                 result = false;
1198                                                 return result;
1199                                         }
1200                                 }
1201                                 if (((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).getResourceType().name().equals("VF")) {
1202                                         resourceEither = setVfToscaResourceName(resource);
1203                                         if (resourceEither.isRight()) {
1204                                                 log.error("DB error during tosca resource name setting");
1205                                                 result = false;
1206                                                 return result;
1207                                         }
1208                                 }
1209                         }
1210                         result = addInvariantUUIDs(appConfigDir);
1211                 } finally {
1212                         if (!result) {
1213                                 titanGenericDao.rollback();
1214                                 log.debug("**********************************************");
1215                                 log.debug("The migration procedure from version 1604 to version 1607 FAILED!!");
1216                                 log.debug("**********************************************");
1217                         } else {
1218                                 titanGenericDao.commit();
1219                                 log.debug("**********************************************");
1220                                 log.debug("The migration procedure from version 1604 to version 1607 ended successfully!");
1221                                 log.debug("**********************************************");
1222                         }
1223                 }
1224
1225                 return result;
1226         }
1227
1228         private boolean addInvariantUUIDs(String appConfigDir) {
1229                 log.debug("Started adding of InvariantUUID ...");
1230                 log.debug("Getting all resources with highest version");
1231
1232                 Map<String, Object> props = new HashMap<>();
1233                 props.put(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
1234
1235                 List<ComponentMetadataData> fullComponentList = new ArrayList<ComponentMetadataData>();
1236
1237                 // getting resources
1238                 Either<List<ResourceMetadataData>, TitanOperationStatus> allHighestVersionResources = titanGenericDao.getByCriteria(NodeTypeEnum.Resource, props, ResourceMetadataData.class);
1239                 if (allHighestVersionResources.isRight()) {
1240                         log.error("Couldn't get resources with highest version from DB, error: {}", allHighestVersionResources.right().value());
1241                         return false;
1242                 }
1243                 List<ResourceMetadataData> allHighestVersionResourcesAL = allHighestVersionResources.left().value();
1244                 if (allHighestVersionResourcesAL == null) {
1245                         log.error("Couldn't get resources with highest version from DB, no resources found");
1246                         return false;
1247                 }
1248                 log.debug("Found {} resources", allHighestVersionResourcesAL.size());
1249                 fullComponentList.addAll(allHighestVersionResourcesAL);
1250
1251                 // getting services
1252                 Either<List<ServiceMetadataData>, TitanOperationStatus> allHighestVersionServices = titanGenericDao.getByCriteria(NodeTypeEnum.Service, props, ServiceMetadataData.class);
1253                 if (allHighestVersionServices.isRight()) {
1254                         log.error("Couldn't get services with highest version from DB, error: {}", allHighestVersionServices.right().value());
1255                         return false;
1256                 }
1257                 List<ServiceMetadataData> allHighestVersionServicesAL = allHighestVersionServices.left().value();
1258                 if (allHighestVersionServicesAL == null) {
1259                         log.error("Couldn't get services with highest version from DB, no services found");
1260                         return false;
1261                 }
1262                 log.debug("Found {} services", allHighestVersionServicesAL.size());
1263                 fullComponentList.addAll(allHighestVersionServicesAL);
1264
1265                 List<ComponentMetadataData> reducedComponentsAL = reduceHighestVersionResourcesList(fullComponentList);
1266
1267                 // getting products
1268                 Either<List<ProductMetadataData>, TitanOperationStatus> allHighestVersionProducts = titanGenericDao.getByCriteria(NodeTypeEnum.Product, props, ProductMetadataData.class);
1269                 if (allHighestVersionProducts.isRight()) {
1270                         log.error("Couldn't get products with highest version from DB, error: {}", allHighestVersionProducts.right().value());
1271                         return false;
1272                 }
1273                 List<ProductMetadataData> allHighestVersionProductsAL = allHighestVersionProducts.left().value();
1274                 if (allHighestVersionProductsAL == null) {
1275                         log.error("Couldn't get products with highest version from DB, no products found");
1276                         return false;
1277                 }
1278                 log.debug("Found {} products", allHighestVersionProductsAL.size());
1279
1280                 List<ComponentMetadataData> fullProductList = new ArrayList<ComponentMetadataData>();
1281                 fullProductList.addAll(allHighestVersionProductsAL);
1282                 List<ComponentMetadataData> reducedProductAL = reduceHighestVersionResourcesList(fullProductList);
1283
1284                 for (ComponentMetadataData product : reducedProductAL) {
1285                         if (!setProductInvariantUUIDIfExists((ProductMetadataData) product)) {
1286                                 return false;
1287                         }
1288                 }
1289                 reducedComponentsAL.addAll(reducedProductAL);
1290
1291                 log.debug("Reduced list of Highest Version Components contains {} components", reducedComponentsAL.size());
1292                 for (ComponentMetadataData componentMetaData : reducedComponentsAL) {
1293
1294                         String invariantUUID = componentMetaData.getMetadataDataDefinition().getInvariantUUID();
1295                         log.debug("old invariantUUID {}", invariantUUID);
1296                         if (invariantUUID == null || invariantUUID.isEmpty()) {
1297                                 invariantUUID = UniqueIdBuilder.buildInvariantUUID();
1298                                 componentMetaData.getMetadataDataDefinition().setInvariantUUID(invariantUUID);
1299                         }
1300                         log.debug("new invariantUUID {}", componentMetaData.getMetadataDataDefinition().getInvariantUUID());
1301                         Either<ComponentMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(componentMetaData, ComponentMetadataData.class);
1302                         if (updateNode.isRight()) {
1303                                 log.error("DB error during while updating component {}, error: {}", componentMetaData.getMetadataDataDefinition().getName(), updateNode.right().value());
1304                                 return false;
1305                         }
1306                         log.debug("updated invariantUUID {}", updateNode.left().value().getMetadataDataDefinition().getInvariantUUID());
1307                         if (!isOnlyVersion(componentMetaData)) {
1308                                 ComponentOperation componentOperation = null;
1309                                 switch (NodeTypeEnum.getByName(componentMetaData.getLabel())) {
1310                                 case Resource:
1311                                         componentOperation = resourceOperation;
1312                                         break;
1313                                 case Service:
1314                                         componentOperation = serviceOperation;
1315                                         break;
1316                                 case Product:
1317                                         componentOperation = productOperation;
1318                                         break;
1319                                 default:
1320                                         break;
1321                                 }
1322                                 Either<Component, StorageOperationStatus> getComponentResult = componentOperation.getComponent((String) componentMetaData.getUniqueId(), true);
1323                                 if (getComponentResult.isRight()) {
1324                                         log.error("DB error during while getting component with uniqueID {}, error: {}", componentMetaData.getUniqueId(), getComponentResult.right().value());
1325                                         return false;
1326                                 }
1327                                 Component component = getComponentResult.left().value();
1328                                 if (component == null) {
1329                                         log.error("The component received from DB is empty");
1330                                         return false;
1331                                 }
1332
1333                                 Map<String, String> allVersions = component.getAllVersions();
1334                                 log.debug("found {} versions for component {}", allVersions.size(), component.getName());
1335                                 Either<Boolean, StorageOperationStatus> resEither = updateAllVersions(allVersions, invariantUUID);
1336                                 if (resEither.isRight()) {
1337                                         log.error("DB error during invariantUUID adding");
1338                                         return false;
1339                                 }
1340                         }
1341                 }
1342                 return true;
1343         }
1344
1345         private boolean isOnlyVersion(ComponentMetadataData componentMetaData) {
1346                 String version = componentMetaData.getMetadataDataDefinition().getVersion();
1347                 if (version.equals("0.1"))
1348                         return true;
1349                 return false;
1350         }
1351
1352         private boolean setProductInvariantUUIDIfExists(ProductMetadataData product) {
1353                 Either<TitanVertex, TitanOperationStatus> getVertexRes = titanGenericDao.getVertexByProperty(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), product.getUniqueId());
1354                 if (getVertexRes.isRight()) {
1355                         log.error("DB error during retrieving product vertex {}", product.getMetadataDataDefinition().getName());
1356                         return false;
1357                 }
1358                 Vertex productVertex = getVertexRes.left().value();
1359                 String invariantUUID = productVertex.value(GraphPropertiesDictionary.CONSTANT_UUID.getProperty());
1360                 if (invariantUUID != null && !invariantUUID.isEmpty()) {
1361                         product.getMetadataDataDefinition().setInvariantUUID(invariantUUID);
1362                 }
1363                 return true;
1364         }
1365
1366         private Either<Boolean, StorageOperationStatus> updateAllVersions(Map<String, String> allVersions, String invariantUUID) {
1367
1368                 if (allVersions != null) {
1369                         for (String uniqueID : allVersions.values()) {
1370                                 Either<ComponentMetadataData, TitanOperationStatus> getNodeResult = titanGenericDao.getNode(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueID, ComponentMetadataData.class);
1371                                 if (getNodeResult.isRight()) {
1372                                         log.error("DB error during while getting component with uniqueID {}, error: {}", uniqueID, getNodeResult.right().value());
1373                                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
1374                                 }
1375                                 ComponentMetadataData component = getNodeResult.left().value();
1376                                 component.getMetadataDataDefinition().setInvariantUUID(invariantUUID);
1377                                 Either<ComponentMetadataData, TitanOperationStatus> updateNodeResult = titanGenericDao.updateNode(component, ComponentMetadataData.class);
1378                                 log.debug("updated child invariantUUID {}", updateNodeResult.left().value().getMetadataDataDefinition().getInvariantUUID());
1379                                 if (updateNodeResult.isRight()) {
1380                                         log.error("DB error during while updating component {}, error: {}", component.getMetadataDataDefinition().getName(), updateNodeResult.right().value());
1381                                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
1382                                 }
1383                         }
1384                 }
1385                 return Either.left(true);
1386         }
1387
1388         private List<ComponentMetadataData> reduceHighestVersionResourcesList(List<ComponentMetadataData> allHighestVersionResources) {
1389                 List<ComponentMetadataData> resultList = null;
1390                 Map<String, ComponentMetadataData> resultHM = new HashMap<String, ComponentMetadataData>();
1391                 for (ComponentMetadataData resource : allHighestVersionResources) {
1392                         if (resource.getMetadataDataDefinition().getInvariantUUID() != null && !resource.getMetadataDataDefinition().getInvariantUUID().isEmpty()) {
1393                                 log.debug("invariantUUID {} ", resource.getMetadataDataDefinition().getInvariantUUID());
1394                                 continue;
1395                         }
1396                         String curUUID = resource.getMetadataDataDefinition().getUUID();
1397                         if (resultHM.containsKey(curUUID)) {
1398                                 int isHighest = resultHM.get(curUUID).getMetadataDataDefinition().getVersion().compareTo(resource.getMetadataDataDefinition().getVersion());
1399                                 if (isHighest > 0) {
1400                                         log.debug("version {} is great than {} ", resultHM.get(curUUID).getMetadataDataDefinition().getVersion(), resource.getMetadataDataDefinition().getVersion());
1401                                         continue;
1402                                 }
1403                         }
1404                         resultHM.put(curUUID, resource);
1405                 }
1406                 resultList = new ArrayList<ComponentMetadataData>(resultHM.values());
1407                 return resultList;
1408         }
1409
1410         private boolean isNormative(String resourceName) {
1411                 for (int i = 0; i < NORMATIVE_OLD_NAMES.length; ++i) {
1412                         if (NORMATIVE_OLD_NAMES[i].equals(resourceName))
1413                                 return true;
1414                 }
1415                 return false;
1416         }
1417
1418         private Either<Boolean, StorageOperationStatus> changeNormativeTypeName(ResourceMetadataData resource) {
1419
1420                 String resourceName = resource.getMetadataDataDefinition().getName();
1421
1422                 if (resourceName != null && !resourceName.isEmpty()) {
1423                         log.debug("Found normative type to change - {}", resourceName);
1424                         String oldName = resourceName;
1425                         String[] splitedName = resourceName.split("\\.");
1426                         String newName = splitedName[splitedName.length - 1];
1427                         String newSystemName = ValidationUtils.convertToSystemName(newName);
1428                         String newNormalizedName = ValidationUtils.normaliseComponentName(newName);
1429                         log.debug("Setting name to be {}", newName);
1430
1431                         resource.getMetadataDataDefinition().setName(newName);
1432                         log.debug("Setting system name to be {}", newSystemName);
1433                         resource.getMetadataDataDefinition().setSystemName(newSystemName);
1434                         log.debug("Setting normalized name to be {}", newNormalizedName);
1435                         resource.getMetadataDataDefinition().setNormalizedName(newNormalizedName);
1436                         log.debug("Updating tag in metadata to be {}", newName);
1437                         resource.getMetadataDataDefinition().getTags().remove(oldName);
1438                         resource.getMetadataDataDefinition().getTags().add(newName);
1439
1440                         log.debug("Creating tag node with name {}", newName);
1441                         TagData tagData = new TagData();
1442                         tagData.setName(newName);
1443                         Either<TagData, TitanOperationStatus> createNode = titanGenericDao.createNode(tagData, TagData.class);
1444                         if (createNode.isRight()) {
1445                                 log.error("Error while creating tag node {}, error: {}.", newName, createNode.right().value());
1446                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1447                         }
1448                         
1449                         Either<ResourceMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(resource, ResourceMetadataData.class);
1450                         if (updateNode.isRight()) {
1451                                 log.error("DB error during while updating normative type {}, error: {}", resource.getMetadataDataDefinition().getName(), updateNode.right().value());
1452                                 return Either.right(StorageOperationStatus.GENERAL_ERROR);
1453                         }
1454                         log.debug("Normative type {} was successfully updated", resource.getMetadataDataDefinition().getName());
1455                         return Either.left(true);
1456                 }
1457
1458                 return Either.left(false);
1459         }
1460
1461         private Either<Boolean, StorageOperationStatus> generateAndSetToscaResourceName(ResourceMetadataData resource, String toscaResourceName) {
1462                 if (toscaResourceName == null) {
1463                         toscaResourceName = CommonBeUtils.generateToscaResourceName(((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).getResourceType().name(), resource.getMetadataDataDefinition().getSystemName());
1464                 }
1465                 Either<Boolean, StorageOperationStatus> validateToscaResourceNameExists = resourceOperation.validateToscaResourceNameExists(toscaResourceName);
1466                 if (validateToscaResourceNameExists.isRight()) {
1467                         StorageOperationStatus storageOperationStatus = validateToscaResourceNameExists.right().value();
1468                         log.error("Couldn't validate toscaResourceName uniqueness - error: {}", storageOperationStatus);
1469                         return Either.right(storageOperationStatus);
1470                 }
1471                 if (validateToscaResourceNameExists.left().value()) {
1472                         log.debug("Setting tosca resource name to be {}", toscaResourceName);
1473                         ((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).setToscaResourceName(toscaResourceName);
1474                         return Either.left(true);
1475                 } else {
1476                         // As agreed with Renana - cannot be fixed automatically
1477                         log.warn("toscaResourceName {} is not unique! Cannot set it. Continuing...");
1478                         return Either.left(false);
1479                 }
1480         }
1481
1482         public boolean testRemoveHeatPlaceHolders(String appConfigDir) {
1483
1484                 if (!AllowMultipleHeats.removeAndUpdateHeatPlaceHolders(titanGenericDao, log, false)) {
1485                         log.error("Failed to update heat place holders");
1486                         return false;
1487                 }
1488                 return true;
1489         }
1490
1491         private Either<Boolean, StorageOperationStatus> setVfToscaResourceName(ResourceMetadataData resource) {
1492                 String resourceName = resource.getMetadataDataDefinition().getName();
1493                 String resourceType = ((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).getResourceType().name();
1494                 String toscaResourceName = CommonBeUtils.generateToscaResourceName(resourceType, resource.getMetadataDataDefinition().getSystemName());
1495                 log.debug("Setting tosca resource name {} to VF {}", toscaResourceName, resourceName);
1496                 ((ResourceMetadataDataDefinition) resource.getMetadataDataDefinition()).setToscaResourceName(toscaResourceName);
1497
1498                 Either<ResourceMetadataData, TitanOperationStatus> updateNode = titanGenericDao.updateNode(resource, ResourceMetadataData.class);
1499                 if (updateNode.isRight()) {
1500                         log.error("DB error during while updating VF tosca resource name {}, error: {}", resource.getMetadataDataDefinition().getName(), updateNode.right().value());
1501                         return Either.right(StorageOperationStatus.GENERAL_ERROR);
1502                 }
1503                 log.debug("Tosca resource name of VF {} was successfully updated", resource.getMetadataDataDefinition().getName());
1504                 return Either.left(true);
1505         }
1506
1507         public boolean testAddGroupUuids(String appConfigDir) {
1508
1509                 if (!AddGroupUuid.addGroupUuids(titanGenericDao, log, false)) {
1510                         log.error("Failed to update group UUIDs");
1511                         return false;
1512                 }
1513                 return true;
1514         }
1515 }