eb9a9ee40873c77405dd04a45eac61e0d01dd539
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.core.tools.Commands;
18
19 import com.amdocs.zusammen.datatypes.Id;
20 import com.amdocs.zusammen.datatypes.SessionContext;
21 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao;
22 import com.google.common.collect.Sets;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openecomp.core.tools.store.ElementHandler;
25 import org.openecomp.core.tools.store.VersionInfoCassandraLoader;
26 import org.openecomp.core.tools.store.VspGeneralLoader;
27 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
28 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.Set;
42
43 import static org.openecomp.core.tools.store.VspGeneralLoader.buildKey;
44
45 public class ResetOldVersion {
46
47
48   private static final String OLD_VERSION = "oldVersion";
49
50   private static final Logger LOGGER = LoggerFactory.getLogger(ResetOldVersion.class);
51   private static final String CASSANDRA_DOX_KEYSTORE = "cassandra.dox.keystore";
52   private static int count = 0;
53
54   private ResetOldVersion() {
55   }
56
57   public static void reset(SessionContext context, String oldVersion, String emptyOldVersion) {
58     Map<String, List<String>> itemVersionMap = new HashMap<>();
59     Map<String, List<String>> itemChangeRefMap = new HashMap<>();
60
61     CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
62
63     loadItemVersionInfo(context, itemChangeRefMap, itemVersionMap);
64
65     Map<String, ElementEntity> generalElementMap =
66         VspGeneralLoader.load(context, itemVersionMap, itemChangeRefMap);
67
68     generalElementMap.values().forEach(elementEntity -> updateOldVersionFlag(elementEntity,
69         oldVersion, Boolean.TRUE.toString().equals(emptyOldVersion)));
70
71
72     itemVersionMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
73
74     itemChangeRefMap.entrySet().forEach(entry -> updateElements(context, generalElementMap, entry));
75     LOGGER.info("number of element updated:" + count);
76   }
77
78   private static void updateElements(SessionContext context, Map<String,
79       ElementEntity> generalElementMap, Map.Entry<String, List<String>> entry) {
80
81     entry.getValue().stream()
82         .filter(changeRef -> generalElementMap.containsKey(buildKey(context, entry, changeRef)))
83         .forEach(changeref -> ElementHandler.update(context, entry.getKey(), changeref, changeref,
84             generalElementMap.get(buildKey(context, entry, changeref))));
85
86   }
87
88   private static void updateOldVersionFlag(ElementEntity elementEntity, String oldVersion,
89                                            boolean emptyOldVersion) {
90
91     if (!emptyOldVersion
92         || StringUtils.isBlank(elementEntity.getInfo().getProperty(OLD_VERSION))) {
93       elementEntity.getInfo().addProperty(OLD_VERSION, oldVersion);
94       count++;
95     }
96   }
97
98   private static void loadItemVersionInfo(SessionContext context,
99                                           Map<String, List<String>> itemChangeRefMap,
100                                           Map<String, List<String>> itemVersionMap) {
101
102     List<String> items = new ArrayList<>();
103     System.setProperty(CASSANDRA_DOX_KEYSTORE, "dox");
104     VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
105     Collection<VersionInfoEntity> versions = versionInfoCassandraLoader.list();
106
107     versions.stream().filter(versionInfoEntity -> versionInfoEntity.getEntityType()
108         .equals(VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE))
109         .forEach(versionInfoEntity -> handleVersionInfoEntity(items, versionInfoEntity,
110             itemChangeRefMap));
111
112     System.setProperty(CASSANDRA_DOX_KEYSTORE, "zusammen_dox");
113     VersionCassandraDao versionCassandraDao = new VersionCassandraDao();
114
115     items.forEach(itemId -> versionCassandraDao.list(context, context.getUser().getUserName(),
116         new Id(itemId)).forEach(itemVersion -> addItemVersion(itemId, itemVersion.getId(),
117         itemVersionMap)));
118
119   }
120
121   private static void handleVersionInfoEntity(List<String> items,
122                                               VersionInfoEntity versionInfoEntity,
123                                               Map<String, List<String>> itemChangeRefMap) {
124     items.add(versionInfoEntity.getEntityId());
125     Set<Version> viewableVersions;
126     if (versionInfoEntity.getViewableVersions() != null
127         && !versionInfoEntity.getViewableVersions().isEmpty()) {
128       viewableVersions = versionInfoEntity.getViewableVersions();
129     } else {
130       viewableVersions = Sets.newHashSet(versionInfoEntity.getActiveVersion());
131     }
132     addItemChangeRef(versionInfoEntity.getEntityId(), maxChangeRef(viewableVersions),
133         itemChangeRefMap);
134   }
135
136   private static Id maxChangeRef(Set<Version> viewableVersions) {
137     Optional<Version> maxVersion = viewableVersions.stream()
138         .max(ResetOldVersion::evaluateMaxVersion);
139
140     return maxVersion.map(version -> new Id(version.toString())).orElse(null);
141   }
142
143   private static int evaluateMaxVersion(Version version1, Version version2) {
144     if (version1.getMajor() > version2.getMajor()) {
145       return 1;
146     } else if (version1.getMajor() == version2.getMajor()) {
147       return Integer.compare(version1.getMinor(), version2.getMinor());
148     } else {
149       return -1;
150     }
151   }
152
153   private static void addItemChangeRef(String itemId, Id changeRef,
154                                        Map<String, List<String>> itemChangeRefMap) {
155     addItemVersion(itemChangeRefMap, itemId, changeRef);
156   }
157
158   private static void addItemVersion(String itemId, Id versionId,
159                                      Map<String, List<String>> itemVersionMap) {
160     addItemVersion(itemVersionMap, itemId, versionId);
161   }
162
163   private static void addItemVersion(Map<String, List<String>> itemVersions, String itemId, Id id) {
164
165     if (!itemVersions.containsKey(itemId)) {
166       itemVersions.put(itemId, new ArrayList<>());
167     }
168
169     itemVersions.get(itemId).add(id.getValue());
170   }
171 }