3516263fbcc5c20a71182ae77e6b79b66775cf18
[sdc.git] /
1 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl;
2
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
4 import com.amdocs.zusammen.datatypes.Id;
5 import com.amdocs.zusammen.datatypes.SessionContext;
6 import com.amdocs.zusammen.datatypes.item.Action;
7 import com.amdocs.zusammen.datatypes.item.ElementContext;
8 import com.amdocs.zusammen.datatypes.item.Resolution;
9 import com.datastax.driver.core.ResultSet;
10 import com.datastax.driver.core.Row;
11 import com.datastax.driver.mapping.annotations.Accessor;
12 import com.datastax.driver.mapping.annotations.Query;
13 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
14 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
15 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
16 import org.openecomp.sdc.datatypes.model.ElementType;
17 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDao;
18 import org.openecomp.sdc.versioning.dao.types.Version;
19
20 import java.util.List;
21 import java.util.stream.Collectors;
22
23 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
24 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
25
26 public class VspMergeDaoImpl implements VspMergeDao {
27   private static final String VSP_MODEL_NOT_EXIST =
28       "Vsp model does not exist for Vsp %s, version %s.";
29
30   private static VspMergeHintAccessor accessor =
31       NoSqlDbFactory.getInstance().createInterface()
32           .getMappingManager().createAccessor(VspMergeHintAccessor.class);
33
34   private ZusammenAdaptor zusammenAdaptor;
35
36   public VspMergeDaoImpl(ZusammenAdaptor zusammenAdaptor) {
37     this.zusammenAdaptor = zusammenAdaptor;
38   }
39
40   @Override
41   public boolean isConflicted(String vspId, Version version) {
42     SessionContext context = createSessionContext();
43     ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
44
45     return listVspModels(context, elementContext).size() > 1;
46   }
47
48   @Override
49   public void updateHint(String vspId, Version version) {
50     SessionContext context = createSessionContext();
51     ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
52
53     String vspModelId = zusammenAdaptor
54         .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name())
55         .orElseThrow(() -> new IllegalStateException(
56             String.format(VSP_MODEL_NOT_EXIST, vspId, version.getId())))
57         .getId().getValue();
58
59     updateVspModelId(vspId, version, vspModelId);
60   }
61
62   @Override
63   public void deleteHint(String vspId, Version version) {
64     accessor.delete(getUser(),vspId,version.getId());
65   }
66
67   @Override
68   public void updateConflictResolution(String vspId, Version version,
69                                        Resolution resolution) {
70     accessor.updateModelResolution(resolution, getUser(), vspId, version.getId());
71   }
72
73   @Override
74   public void applyConflictResolution(String vspId, Version version) {
75     //called only when no conflicts
76
77     SessionContext context = createSessionContext();
78     ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
79
80     List<ElementInfo> vspModels = listVspModels(context, elementContext);
81     if (vspModels.size() == 1) {
82       updateVspModelId(vspId, version, vspModels.iterator().next().getId().getValue());
83       return;
84     }
85
86     if (vspModels.size() != 2) {
87       return;
88     }
89
90     String user = getUser();
91     Row row = accessor.get(user, vspId, version.getId()).one();
92     if (row == null) {
93       throw new IllegalStateException(
94           "Vsp model id must exists if its conflict is being resolved");
95     }
96     String resolutionValue = row.getString("model_resolution");
97     if (resolutionValue == null) {
98       return; // model conflict is not resolved yet
99     }
100
101     Resolution resolution = Resolution.valueOf(resolutionValue);
102     String localModelId = row.getString("model_id");
103
104     String chosenModelId =
105         keepOnlyChosenVspModel(context, elementContext, vspModels, resolution, localModelId);
106
107     accessor.update(chosenModelId, null, user, vspId, version.getId());
108   }
109
110   private String keepOnlyChosenVspModel(SessionContext context, ElementContext elementContext,
111                                         List<ElementInfo> vspModels, Resolution resolution,
112                                         String localModelId) {
113     String newLocalModelId = null;
114     for (ElementInfo vspModel : vspModels) {
115       if (isRedundantModel(vspModel.getId().getValue(), localModelId, resolution)) {
116         zusammenAdaptor
117             .saveElement(context, elementContext, buildElement(vspModel.getId(), Action.DELETE),
118                 "Delete Redundant Vsp Model");
119       } else {
120         newLocalModelId = vspModel.getId().getValue();
121       }
122     }
123     return newLocalModelId;
124   }
125
126   private boolean isRedundantModel(String modelId, String localModelId, Resolution resolution) {
127     return resolution == Resolution.THEIRS && modelId.equals(localModelId) ||
128         resolution == Resolution.YOURS && !modelId.equals(localModelId);
129   }
130
131   private List<ElementInfo> listVspModels(SessionContext context, ElementContext elementContext) {
132     return zusammenAdaptor.listElements(context, elementContext, null).stream()
133         .filter(elementInfo -> ElementType.VspModel.name().equals(elementInfo.getInfo().getName()))
134         .collect(Collectors.toList());
135   }
136
137   private void updateVspModelId(String vspId, Version version, String vspModelId) {
138     accessor.update(vspModelId, null, getUser(), vspId, version.getId());
139   }
140
141   private String getUser() {
142     return SessionContextProviderFactory.getInstance().createInterface()
143         .get().getUser().getUserId();
144   }
145
146   @Accessor
147   interface VspMergeHintAccessor {
148
149     @Query("SELECT model_id, model_resolution FROM vsp_merge_hint " +
150         "WHERE space=? AND item_id=? AND version_id=?")
151     ResultSet get(String space, String itemId, String versionId);
152
153     @Query("UPDATE vsp_merge_hint SET model_id=?, model_resolution=? " +
154         "WHERE space=? AND item_id=? AND version_id=?")
155     void update(String vspModelId, Resolution modelResolution, String space,
156                 String itemId, String versionId);
157
158     @Query(
159         "UPDATE vsp_merge_hint SET model_resolution=? WHERE space=? AND item_id=? AND version_id=?")
160     void updateModelResolution(Resolution modelResolution, String space, String itemId,
161                                String versionId);
162
163     @Query("DELETE from vsp_merge_hint WHERE space=? AND item_id=? AND version_id=?")
164     void delete(String space, String itemId, String versionId);
165   }
166 }