Reset all version failure
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / store / VspGeneralLoader.java
1 package org.openecomp.core.tools.store;
2
3 import com.amdocs.zusammen.datatypes.Id;
4 import com.amdocs.zusammen.datatypes.SessionContext;
5 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
6 import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository;
7 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
8
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Optional;
13
14 public class VspGeneralLoader {
15
16   public static final String NAME = "name";
17   public static final String GENERAL = "General";
18
19   private static CassandraElementRepository cassandraElementRepository =
20           new CassandraElementRepository();
21
22   public static Map<String, ElementEntity> load(SessionContext context,
23                                                 Map<String, List<String>> vspItemVersionsMap,
24                                                 Map<String, List<String>> vspItemChangeRefssMap) {
25     Map<String, ElementEntity> elementEntityMap = new HashMap<>();
26     System.setProperty("cassandra.dox.keystore", "zusammen_dox");
27
28     Id entityId;
29     Id itemId;
30     Id changeRefId;
31     for (Map.Entry<String, List<String>> entry : vspItemVersionsMap.entrySet()) {
32
33       for (String version : entry.getValue()) {
34
35
36         itemId = new Id(entry.getKey());
37         changeRefId = new Id(version);
38         entityId = getEntityIdByInfoNameValue(context, itemId, changeRefId, null, null, NAME,
39                 GENERAL);
40         if (entityId != null) {
41           Optional<ElementEntity> result =
42                   cassandraElementRepository.get(context, new ElementEntityContext(
43                                   context.getUser().getUserName(),
44                                   itemId,
45                                   changeRefId),
46                           new ElementEntity(entityId));
47           if (result.isPresent()) {
48             elementEntityMap.put(buildKey(context, entry, version), result.get());
49           }
50         }
51       }
52     }
53
54
55     for (Map.Entry<String, List<String>> entry : vspItemChangeRefssMap.entrySet()) {
56
57       for (String changeRef : entry.getValue()) {
58
59
60         itemId = new Id(entry.getKey());
61
62         entityId = getEntityIdByInfoNameValue(context, itemId, null, changeRef,null, NAME,
63                 GENERAL);
64         if (entityId != null) {
65           ElementEntityContext elementContext = new ElementEntityContext(
66                   context.getUser().getUserName(),
67                   itemId,
68                   null);
69           elementContext.setChangeRef(changeRef);
70           Optional<ElementEntity> result =
71                   cassandraElementRepository.get(context, elementContext,
72                           new ElementEntity(entityId));
73           if (result.isPresent()) {
74             elementEntityMap.put(buildKey(context, entry, changeRef), result.get());
75           }
76         }
77       }
78     }
79
80
81     return elementEntityMap;
82   }
83
84   public static String buildKey(SessionContext context, Map.Entry<String, List<String>> entry, String version) {
85     return String.format("%s_%s_%s", context.getUser().getUserName(), entry.getKey(), version);
86   }
87
88   private static Id getEntityIdByInfoNameValue(SessionContext context,
89                                                Id itemId,
90                                                Id versionId,
91                                                String changeRef,
92                                                Id elementId,
93                                                String name,
94                                                String value) {
95
96
97     ElementEntityContext elementContext = new ElementEntityContext(
98             context.getUser().getUserName(),
99             itemId,
100             versionId);
101     if (changeRef != null) {
102       elementContext.setChangeRef(changeRef);
103     }
104     Optional<ElementEntity> result =
105             cassandraElementRepository.get(context, elementContext,
106                     new ElementEntity(Id.ZERO));
107     if (result.isPresent()) {
108       ElementEntity elementEntity = result.get();
109       return elementEntity.getSubElementIds().stream().filter(subelementId -> {
110         ElementEntityContext subElementContext = new ElementEntityContext(
111                 context.getUser().getUserName(),
112                 itemId,
113                 versionId);
114         if(changeRef!= null){
115           subElementContext.setChangeRef(changeRef);
116         }
117         Optional<ElementEntity> subElementEntity =
118                 cassandraElementRepository.get(context, subElementContext,
119                         new ElementEntity(subelementId));
120         if (subElementEntity.isPresent()) {
121           if (NAME.equals(name)) {
122             if (value.equals(subElementEntity.get().getInfo().getName())) {
123               return true;
124             }
125           }
126           if (value.equals(subElementEntity.get().getInfo().getProperty(name))) {
127             return true;
128           }
129         }
130         return false;
131
132       }).findFirst().orElse(null);
133     }
134     return null;
135
136
137   }
138
139
140 }