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