2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.conflicts.impl;
23 import org.openecomp.conflicts.ItemMergeHandler;
24 import org.openecomp.conflicts.dao.ConflictsDao;
25 import org.openecomp.conflicts.types.*;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.common.errors.ErrorCategory;
28 import org.openecomp.sdc.common.errors.ErrorCode;
29 import org.openecomp.sdc.datatypes.model.ElementType;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDao;
31 import org.openecomp.sdc.versioning.dao.types.Version;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
37 import static org.openecomp.sdc.datatypes.model.ElementType.*;
39 public class VspMergeHandler implements ItemMergeHandler {
41 private static final String VSP_MODEL_CONFLICT_ID = "vspModelConflictId";
42 private static final String ELEMENT_CONFLICT_NOT_EXIST_ERR_ID = "ELEMENT_CONFLICT_NOT_EXIST";
43 private static final String ELEMENT_CONFLICT_NOT_EXISTS_MSG =
44 "Item Id %s, version Id %s, element conflict with Id %s does not exists.";
45 private static final Set<ElementType> FILTERED_OUT_TYPES = Stream
46 .of(OrchestrationTemplateCandidateContent, OrchestrationTemplateValidationData)
47 .collect(Collectors.toSet());
48 private static final Map<ElementType, Set<ElementType>> ELEMENT_TYPE_TO_CONFLICT_DEPENDANT_TYPES =
49 new EnumMap<>(ElementType.class);
52 ELEMENT_TYPE_TO_CONFLICT_DEPENDANT_TYPES.put(OrchestrationTemplateCandidate,
53 Collections.singleton(OrchestrationTemplateCandidateContent));
54 ELEMENT_TYPE_TO_CONFLICT_DEPENDANT_TYPES.put(OrchestrationTemplate,
55 Collections.singleton(OrchestrationTemplateValidationData));
58 private ConflictsDao conflictsDao;
59 private VspMergeDao vspMergeDao;
61 public VspMergeHandler(ConflictsDao conflictsDao, VspMergeDao vspMergeDao) {
62 this.conflictsDao = conflictsDao;
63 this.vspMergeDao = vspMergeDao;
67 public boolean isConflicted(String itemId, Version version) {
68 return vspMergeDao.isConflicted(itemId, version);
72 public void finalizeMerge(String itemId, Version version) {
73 if (!conflictsDao.isConflicted(itemId, version)) {
74 vspMergeDao.applyConflictResolution(itemId, version);
79 public void postListConflicts(String itemId, Version version, ItemVersionConflict conflicts) {
80 List<ConflictInfo> elementConflicts = new ArrayList<>();
82 boolean vspModelConflicted = false;
83 for (ConflictInfo elementConflict : conflicts.getElementConflicts()) {
84 if (elementConflict.getType() == VspModel) {
86 new ConflictInfo(elementConflict.getId(), NetworkPackage, NetworkPackage.name()));
87 vspModelConflicted = true;
90 if (!FILTERED_OUT_TYPES.contains(elementConflict.getType())) {
91 elementConflicts.add(elementConflict);
95 if (!vspModelConflicted && vspMergeDao.isConflicted(itemId, version)) {
97 .add(new ConflictInfo(VSP_MODEL_CONFLICT_ID, NetworkPackage, NetworkPackage.name()));
100 conflicts.setElementConflicts(elementConflicts);
104 public Optional<Conflict> getConflict(String itemId, Version version, String conflictId) {
105 return VSP_MODEL_CONFLICT_ID.equals(conflictId)
106 ? Optional.of(buildVspModelConflict(conflictId))
111 public void postGetConflict(String itemId, Version version, Conflict conflict) {
112 if (conflict.getType() == VspModel) {
113 Conflict vspModelConflict = buildVspModelConflict(null);
114 conflict.setType(vspModelConflict.getType());
115 conflict.setName(vspModelConflict.getName());
116 conflict.setYours(vspModelConflict.getYours());
117 conflict.setTheirs(vspModelConflict.getTheirs());
122 public void preResolveConflict(String itemId, Version version, String conflictId,
123 ConflictResolution resolution) {
124 if (VSP_MODEL_CONFLICT_ID.equals(conflictId)) {
127 resolveDependantConflicts(itemId, version, conflictId, resolution);
131 public boolean resolveConflict(String itemId, Version version, String conflictId,
132 ConflictResolution resolution) {
133 if (VSP_MODEL_CONFLICT_ID.equals(conflictId)) {
134 vspMergeDao.updateConflictResolution(itemId, version,
135 com.amdocs.zusammen.datatypes.item.Resolution.valueOf(resolution.getResolution().name()));
138 Conflict conflict = conflictsDao.getConflict(itemId, version, conflictId);
139 if (conflict == null) {
140 throw getConflictNotExistException(itemId, version, conflictId);
142 if (conflict.getType() == VspModel) {
143 vspMergeDao.updateConflictResolution(itemId, version,
144 com.amdocs.zusammen.datatypes.item.Resolution.valueOf(resolution.getResolution().name()));
146 conflictsDao.resolveConflict(itemId, version, conflictId, new ConflictResolution(
147 conflict.getTheirs() == null ? Resolution.YOURS : Resolution.THEIRS));
153 private void resolveDependantConflicts(String itemId, Version version, String conflictId,
154 ConflictResolution resolution) {
155 ItemVersionConflict conflicts = conflictsDao.getConflict(itemId, version);
157 Set<ElementType> conflictDependantTypes =
158 ELEMENT_TYPE_TO_CONFLICT_DEPENDANT_TYPES
159 .get(findConflictById(conflicts, conflictId).getType());
161 if (conflictDependantTypes == null) {
165 findConflictsByTypes(conflicts, conflictDependantTypes)
166 .forEach(dependantConflict ->
167 conflictsDao.resolveConflict(itemId, version, dependantConflict.getId(), resolution));
170 private ConflictInfo findConflictById(ItemVersionConflict versionConflicts,
172 return versionConflicts.getElementConflicts().stream()
173 .filter(elementConflict -> conflictId.equals(elementConflict.getId()))
175 .orElseThrow(() -> new IllegalStateException(
176 String.format("Conflict Id %s does not exist on conflicts list", conflictId)));
179 private Collection<ConflictInfo> findConflictsByTypes(ItemVersionConflict versionConflicts,
180 Set<ElementType> elementTypes) {
181 return versionConflicts.getElementConflicts().stream()
182 .filter(elementConflict -> elementTypes.contains(elementConflict.getType()))
183 .collect(Collectors.toList());
186 private Conflict buildVspModelConflict(String conflictId) {
187 Conflict conflict = new Conflict(conflictId, NetworkPackage, NetworkPackage.name());
189 Map<String, String> yours = new HashMap<>();
190 yours.put("File", "Local (Me)");
191 conflict.setYours(yours);
193 Map<String, String> theirs = new HashMap<>();
194 theirs.put("File", "Last Committed");
195 conflict.setTheirs(theirs);
199 private CoreException getConflictNotExistException(String itemId, Version version,
201 return new CoreException(new ErrorCode.ErrorCodeBuilder()
202 .withCategory(ErrorCategory.APPLICATION)
203 .withId(ELEMENT_CONFLICT_NOT_EXIST_ERR_ID)
205 String.format(ELEMENT_CONFLICT_NOT_EXISTS_MSG, itemId, version.getId(), conflictId))