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=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl;
22 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;
23 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.datatypes.item.Resolution;
31 import com.datastax.driver.core.ResultSet;
32 import com.datastax.driver.core.Row;
33 import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
34 import com.datastax.driver.mapping.MappingManager;
35 import com.datastax.driver.mapping.annotations.Accessor;
36 import com.datastax.driver.mapping.annotations.Query;
37 import java.util.List;
38 import java.util.stream.Collectors;
39 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
40 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
41 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
42 import org.openecomp.sdc.datatypes.model.ElementType;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDao;
44 import org.openecomp.sdc.versioning.dao.types.Version;
46 public class VspMergeDaoImpl implements VspMergeDao {
48 private static final String VSP_MODEL_NOT_EXIST = "Vsp model does not exist for Vsp %s, version %s.";
49 private static final VspMergeHintAccessor accessor;
52 MappingManager mappingManager = NoSqlDbFactory.getInstance().createInterface().getMappingManager();
53 mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry().register(new EnumNameCodec<>(Resolution.class));
54 accessor = mappingManager.createAccessor(VspMergeHintAccessor.class);
57 private ZusammenAdaptor zusammenAdaptor;
59 public VspMergeDaoImpl(ZusammenAdaptor zusammenAdaptor) {
60 this.zusammenAdaptor = zusammenAdaptor;
64 public boolean isConflicted(String vspId, Version version) {
65 SessionContext context = createSessionContext();
66 ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
67 return listVspModels(context, elementContext).size() > 1;
71 public void updateHint(String vspId, Version version) {
72 SessionContext context = createSessionContext();
73 ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
74 String vspModelId = zusammenAdaptor.getElementInfoByName(context, elementContext, null, ElementType.VspModel.name())
75 .orElseThrow(() -> new IllegalStateException(String.format(VSP_MODEL_NOT_EXIST, vspId, version.getId()))).getId().getValue();
76 updateVspModelId(vspId, version, vspModelId);
80 public void deleteHint(String vspId, Version version) {
81 accessor.delete(getUser(), vspId, version.getId());
85 public void updateConflictResolution(String vspId, Version version, Resolution resolution) {
86 accessor.updateModelResolution(resolution, getUser(), vspId, version.getId());
90 public void applyConflictResolution(String vspId, Version version) {
91 //called only when no conflicts
92 SessionContext context = createSessionContext();
93 ElementContext elementContext = new ElementContext(new Id(vspId), new Id(version.getId()));
94 List<ElementInfo> vspModels = listVspModels(context, elementContext);
95 if (vspModels.size() == 1) {
96 updateVspModelId(vspId, version, vspModels.iterator().next().getId().getValue());
99 if (vspModels.size() != 2) {
102 String user = getUser();
103 Row row = accessor.get(user, vspId, version.getId()).one();
105 throw new IllegalStateException("Vsp model id must exists if its conflict is being resolved");
107 String resolutionValue = row.getString("model_resolution");
108 if (resolutionValue == null) {
109 return; // model conflict is not resolved yet
111 Resolution resolution = Resolution.valueOf(resolutionValue);
112 String localModelId = row.getString("model_id");
113 String chosenModelId = keepOnlyChosenVspModel(context, elementContext, vspModels, resolution, localModelId);
114 accessor.update(chosenModelId, null, user, vspId, version.getId());
117 private String keepOnlyChosenVspModel(SessionContext context, ElementContext elementContext, List<ElementInfo> vspModels, Resolution resolution,
118 String localModelId) {
119 String newLocalModelId = null;
120 for (ElementInfo vspModel : vspModels) {
121 if (isRedundantModel(vspModel.getId().getValue(), localModelId, resolution)) {
122 zusammenAdaptor.saveElement(context, elementContext, buildElement(vspModel.getId(), Action.DELETE), "Delete Redundant Vsp Model");
124 newLocalModelId = vspModel.getId().getValue();
127 return newLocalModelId;
130 private boolean isRedundantModel(String modelId, String localModelId, Resolution resolution) {
131 return resolution == Resolution.THEIRS && modelId.equals(localModelId) || resolution == Resolution.YOURS && !modelId.equals(localModelId);
134 private List<ElementInfo> listVspModels(SessionContext context, ElementContext elementContext) {
135 return zusammenAdaptor.listElements(context, elementContext, null).stream()
136 .filter(elementInfo -> ElementType.VspModel.name().equals(elementInfo.getInfo().getName())).collect(Collectors.toList());
139 private void updateVspModelId(String vspId, Version version, String vspModelId) {
140 accessor.update(vspModelId, null, getUser(), vspId, version.getId());
143 private String getUser() {
144 return SessionContextProviderFactory.getInstance().createInterface().get().getUser().getUserId();
148 interface VspMergeHintAccessor {
150 @Query("SELECT model_id, model_resolution FROM vsp_merge_hint " + "WHERE space=? AND item_id=? AND version_id=?")
151 ResultSet get(String space, String itemId, String versionId);
153 @Query("UPDATE vsp_merge_hint SET model_id=?, model_resolution=? " + "WHERE space=? AND item_id=? AND version_id=?")
154 void update(String vspModelId, Resolution modelResolution, String space, String itemId, String versionId);
156 @Query("UPDATE vsp_merge_hint SET model_resolution=? WHERE space=? AND item_id=? AND version_id=?")
157 void updateModelResolution(Resolution modelResolution, String space, String itemId, String versionId);
159 @Query("DELETE from vsp_merge_hint WHERE space=? AND item_id=? AND version_id=?")
160 void delete(String space, String itemId, String versionId);