Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-conflict-lib / openecomp-conflict-core / src / main / java / org / openecomp / conflicts / impl / VspMergeHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.conflicts.impl;
22
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;
32
33 import java.util.*;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36
37 import static org.openecomp.sdc.datatypes.model.ElementType.*;
38
39 public class VspMergeHandler implements ItemMergeHandler {
40
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);
50
51   static {
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));
56   }
57
58   private ConflictsDao conflictsDao;
59   private VspMergeDao vspMergeDao;
60
61   public VspMergeHandler(ConflictsDao conflictsDao, VspMergeDao vspMergeDao) {
62     this.conflictsDao = conflictsDao;
63     this.vspMergeDao = vspMergeDao;
64   }
65
66   @Override
67   public boolean isConflicted(String itemId, Version version) {
68     return vspMergeDao.isConflicted(itemId, version);
69   }
70
71   @Override
72   public void finalizeMerge(String itemId, Version version) {
73     if (!conflictsDao.isConflicted(itemId, version)) {
74       vspMergeDao.applyConflictResolution(itemId, version);
75     }
76   }
77
78   @Override
79   public void postListConflicts(String itemId, Version version, ItemVersionConflict conflicts) {
80     List<ConflictInfo> elementConflicts = new ArrayList<>();
81
82     boolean vspModelConflicted = false;
83     for (ConflictInfo elementConflict : conflicts.getElementConflicts()) {
84       if (elementConflict.getType() == VspModel) {
85         elementConflicts.add(
86             new ConflictInfo(elementConflict.getId(), NetworkPackage, NetworkPackage.name()));
87         vspModelConflicted = true;
88         continue;
89       }
90       if (!FILTERED_OUT_TYPES.contains(elementConflict.getType())) {
91         elementConflicts.add(elementConflict);
92       }
93     }
94
95     if (!vspModelConflicted && vspMergeDao.isConflicted(itemId, version)) {
96       elementConflicts
97           .add(new ConflictInfo(VSP_MODEL_CONFLICT_ID, NetworkPackage, NetworkPackage.name()));
98     }
99
100     conflicts.setElementConflicts(elementConflicts);
101   }
102
103   @Override
104   public Optional<Conflict> getConflict(String itemId, Version version, String conflictId) {
105     return VSP_MODEL_CONFLICT_ID.equals(conflictId)
106         ? Optional.of(buildVspModelConflict(conflictId))
107         : Optional.empty();
108   }
109
110   @Override
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());
118     }
119   }
120
121   @Override
122   public void preResolveConflict(String itemId, Version version, String conflictId,
123                                  ConflictResolution resolution) {
124     if (VSP_MODEL_CONFLICT_ID.equals(conflictId)) {
125       return;
126     }
127     resolveDependantConflicts(itemId, version, conflictId, resolution);
128   }
129
130   @Override
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()));
136       return true;
137     }
138     Conflict conflict = conflictsDao.getConflict(itemId, version, conflictId);
139     if (conflict == null) {
140       throw getConflictNotExistException(itemId, version, conflictId);
141     }
142     if (conflict.getType() == VspModel) {
143       vspMergeDao.updateConflictResolution(itemId, version,
144           com.amdocs.zusammen.datatypes.item.Resolution.valueOf(resolution.getResolution().name()));
145
146       conflictsDao.resolveConflict(itemId, version, conflictId, new ConflictResolution(
147           conflict.getTheirs() == null ? Resolution.YOURS : Resolution.THEIRS));
148       return true;
149     }
150     return false;
151   }
152
153   private void resolveDependantConflicts(String itemId, Version version, String conflictId,
154                                          ConflictResolution resolution) {
155     ItemVersionConflict conflicts = conflictsDao.getConflict(itemId, version);
156
157     Set<ElementType> conflictDependantTypes =
158         ELEMENT_TYPE_TO_CONFLICT_DEPENDANT_TYPES
159             .get(findConflictById(conflicts, conflictId).getType());
160
161     if (conflictDependantTypes == null) {
162       return;
163     }
164
165     findConflictsByTypes(conflicts, conflictDependantTypes)
166         .forEach(dependantConflict ->
167             conflictsDao.resolveConflict(itemId, version, dependantConflict.getId(), resolution));
168   }
169
170   private ConflictInfo findConflictById(ItemVersionConflict versionConflicts,
171                                         String conflictId) {
172     return versionConflicts.getElementConflicts().stream()
173         .filter(elementConflict -> conflictId.equals(elementConflict.getId()))
174         .findFirst()
175         .orElseThrow(() -> new IllegalStateException(
176             String.format("Conflict Id %s does not exist on conflicts list", conflictId)));
177   }
178
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());
184   }
185
186   private Conflict buildVspModelConflict(String conflictId) {
187     Conflict conflict = new Conflict(conflictId, NetworkPackage, NetworkPackage.name());
188
189     Map<String, String> yours = new HashMap<>();
190     yours.put("File", "Local (Me)");
191     conflict.setYours(yours);
192
193     Map<String, String> theirs = new HashMap<>();
194     theirs.put("File", "Last Committed");
195     conflict.setTheirs(theirs);
196     return conflict;
197   }
198
199   private CoreException getConflictNotExistException(String itemId, Version version,
200                                                      String conflictId) {
201     return new CoreException(new ErrorCode.ErrorCodeBuilder()
202         .withCategory(ErrorCategory.APPLICATION)
203         .withId(ELEMENT_CONFLICT_NOT_EXIST_ERR_ID)
204         .withMessage(
205             String.format(ELEMENT_CONFLICT_NOT_EXISTS_MSG, itemId, version.getId(), conflictId))
206         .build());
207   }
208 }