2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openecomp.core.zusammen.db.impl;
18 import com.amdocs.zusammen.adaptor.inbound.api.health.HealthAdaptorFactory;
19 import com.amdocs.zusammen.adaptor.inbound.api.item.ElementAdaptorFactory;
20 import com.amdocs.zusammen.adaptor.inbound.api.item.ItemAdaptorFactory;
21 import com.amdocs.zusammen.adaptor.inbound.api.item.ItemVersionAdaptorFactory;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementConflict;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ItemVersionConflict;
26 import com.amdocs.zusammen.adaptor.inbound.api.types.item.MergeResult;
27 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
28 import com.amdocs.zusammen.commons.health.data.HealthInfo;
29 import com.amdocs.zusammen.datatypes.Id;
30 import com.amdocs.zusammen.datatypes.SessionContext;
31 import com.amdocs.zusammen.datatypes.Space;
32 import com.amdocs.zusammen.datatypes.item.ElementContext;
33 import com.amdocs.zusammen.datatypes.item.Info;
34 import com.amdocs.zusammen.datatypes.item.Item;
35 import com.amdocs.zusammen.datatypes.item.ItemVersion;
36 import com.amdocs.zusammen.datatypes.item.ItemVersionData;
37 import com.amdocs.zusammen.datatypes.item.ItemVersionStatus;
38 import com.amdocs.zusammen.datatypes.item.Resolution;
39 import com.amdocs.zusammen.datatypes.itemversion.ItemVersionRevisions;
40 import com.amdocs.zusammen.datatypes.itemversion.Tag;
41 import com.amdocs.zusammen.datatypes.response.Response;
42 import com.amdocs.zusammen.datatypes.response.ReturnCode;
43 import org.openecomp.core.zusammen.db.ZusammenConnector;
44 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
45 import org.openecomp.core.zusammen.impl.ItemElementLoggerTargetServiceName;
46 import org.openecomp.sdc.common.errors.SdcRuntimeException;
47 import org.openecomp.sdc.datatypes.error.ErrorLevel;
48 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
49 import org.openecomp.sdc.logging.types.LoggerConstants;
50 import org.openecomp.sdc.logging.types.LoggerErrorCode;
52 import java.util.Collection;
54 public class ZusammenConnectorImpl implements ZusammenConnector {
56 private static final String GET_ELEMENT_ERR_MSG =
57 "Failed to get element. Item Id: %s, version Id: %s, element Id: %s message: %s";
58 private static final String GET_ELEMENT_IN_REV_ERR_MSG =
59 "Failed to get element. Item Id: %s, version Id: %s, revision Id: %s, element Id: %s message: %s";
60 private ItemAdaptorFactory itemAdaptorFactory;
61 private ItemVersionAdaptorFactory versionAdaptorFactory;
62 private ElementAdaptorFactory elementAdaptorFactory;
63 private HealthAdaptorFactory healthAdaptorFactory;
65 public ZusammenConnectorImpl(
66 ItemAdaptorFactory itemAdaptorFactory,
67 ItemVersionAdaptorFactory versionAdaptorFactory,
68 ElementAdaptorFactory elementAdaptorFactory,
69 HealthAdaptorFactory healthAdaptorFactory) {
70 this.itemAdaptorFactory = itemAdaptorFactory;
71 this.versionAdaptorFactory = versionAdaptorFactory;
72 this.elementAdaptorFactory = elementAdaptorFactory;
73 this.healthAdaptorFactory = healthAdaptorFactory;
74 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
78 public Collection<HealthInfo> checkHealth(SessionContext sessionContext) {
79 return healthAdaptorFactory.createInterface(sessionContext).getHealthStatus(sessionContext);
83 public String getVersion(SessionContext sessionContext) {
84 return healthAdaptorFactory.createInterface(sessionContext).getVersion();
88 public Collection<Item> listItems(SessionContext context) {
89 Response<Collection<Item>> response = itemAdaptorFactory.createInterface(context).list(context);
90 if (!response.isSuccessful()) {
91 throw new SdcRuntimeException(
92 "Failed to list Items. message:" + response.getReturnCode().toString());
94 return response.getValue();
98 public Item getItem(SessionContext context, Id itemId) {
99 Response<Item> response = itemAdaptorFactory.createInterface(context).get(context, itemId);
100 if (!response.isSuccessful()) {
101 throw new SdcRuntimeException(
102 "Failed to get Item. message:" + response.getReturnCode().toString());
104 return response.getValue();
108 public Id createItem(SessionContext context, Info info) {
109 Response<Id> response = itemAdaptorFactory.createInterface(context).create(context, info);
110 if (!response.isSuccessful()) {
111 throw new SdcRuntimeException(
112 "Failed to create Item. message:" + response.getReturnCode().toString());
114 return response.getValue();
118 public void updateItem(SessionContext context, Id itemId, Info info) {
119 Response<Void> response =
120 itemAdaptorFactory.createInterface(context).update(context, itemId, info);
122 if (!response.isSuccessful()) {
123 throw new SdcRuntimeException("failed to update Item . ItemId:" + itemId + "" +
124 " message:" + response.getReturnCode().toString());
129 public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) {
130 Response<Collection<ItemVersion>> versions =
131 versionAdaptorFactory.createInterface(context).list(context, Space.PUBLIC, itemId);
132 if (!versions.isSuccessful()) {
133 logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ITEM_VERSION_RETRIEVAL, versions
135 throw new SdcRuntimeException("failed to list public versions. message: " +
136 versions.getReturnCode().toString());
138 return versions.getValue();
142 public ItemVersion getPublicVersion(SessionContext context, Id itemId, Id versionId) {
143 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
144 .get(context, Space.PUBLIC, itemId, versionId);
145 if (!response.isSuccessful()) {
146 throw new SdcRuntimeException(
147 String.format("failed to get public Item Version. ItemId: %s, versionId: %s, message: %s",
148 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
150 return response.getValue();
154 public Id createVersion(SessionContext context, Id itemId, Id baseVersionId,
155 ItemVersionData itemVersionData) {
156 Response<Id> response = versionAdaptorFactory.createInterface(context).create(context, itemId,
157 baseVersionId, itemVersionData);
158 if (response.isSuccessful()) {
159 return response.getValue();
161 throw new SdcRuntimeException(String.format(
162 "failed to create Item Version. ItemId: %s, base versionId: %s, message: %s",
163 itemId.getValue(), baseVersionId.getValue(), response.getReturnCode().toString()));
168 public void updateVersion(SessionContext context, Id itemId, Id versionId,
169 ItemVersionData itemVersionData) {
170 Response<Void> response = versionAdaptorFactory.createInterface(context)
171 .update(context, itemId, versionId, itemVersionData);
172 if (!response.isSuccessful()) {
173 throw new SdcRuntimeException(
174 String.format("failed to update Item Version. ItemId: %s, versionId: %s, message: %s",
175 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
180 public ItemVersion getVersion(SessionContext context, Id itemId, Id versionId) {
181 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
182 .get(context, Space.PRIVATE, itemId, versionId);
183 if (!response.isSuccessful()) {
184 throw new SdcRuntimeException(
185 String.format("failed to get Item Version. ItemId: %s, versionId: %s, message: %s",
186 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
188 return response.getValue();
192 public ItemVersionStatus getVersionStatus(SessionContext context, Id itemId, Id versionId) {
193 Response<ItemVersionStatus> response =
194 versionAdaptorFactory.createInterface(context).getStatus(context, itemId, versionId);
195 if (!response.isSuccessful()) {
196 throw new SdcRuntimeException(
197 String.format("failed to get Item Version status. ItemId: %s, versionId: %s, message: %s",
198 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
200 return response.getValue();
204 public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) {
205 Response<Void> response = versionAdaptorFactory.createInterface(context)
206 .tag(context, itemId, versionId, null, tag);
207 if (!response.isSuccessful()) {
208 throw new SdcRuntimeException(String.format(
209 "failed to tag Item Version with tag %s. ItemId: %s, versionId: %s, message: %s",
210 tag.getName(), itemId.getValue(), versionId.getValue(),
211 response.getReturnCode().toString()));
216 public void resetVersionRevision(SessionContext context, Id itemId, Id versionId,
218 Response<Void> response = versionAdaptorFactory.createInterface(context)
219 .resetRevision(context, itemId, versionId, revisionId);
220 if (!response.isSuccessful()) {
221 throw new SdcRuntimeException(String.format(
222 "failed to reset Item Version back to revision: %s. ItemId: %s, versionId: %s, message:" +
224 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
225 response.getReturnCode().toString()));
230 public void revertVersionRevision(SessionContext context, Id itemId, Id versionId,
232 Response<Void> response = versionAdaptorFactory.createInterface(context)
233 .revertRevision(context, itemId, versionId, revisionId);
234 if (!response.isSuccessful()) {
235 throw new SdcRuntimeException(String.format(
236 "failed to revert Item Version back to revision: %s. ItemId: %s, versionId: %s, " +
238 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
239 response.getReturnCode().toString()));
244 public ItemVersionRevisions listVersionRevisions(SessionContext context, Id itemId,
246 Response<ItemVersionRevisions> response =
247 versionAdaptorFactory.createInterface(context)
248 .listRevisions(context, itemId, versionId);
249 if (!response.isSuccessful()) {
250 throw new SdcRuntimeException(String.format(
251 "failed to list revisions of Item Version. ItemId: %s, versionId: %s, message: %s",
252 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
254 return response.getValue();
259 public void publishVersion(SessionContext context, Id itemId, Id versionId, String message) {
260 Response<Void> response =
261 versionAdaptorFactory.createInterface(context).publish(context, itemId, versionId, message);
262 if (!response.isSuccessful()) {
263 throw new SdcRuntimeException(String.format(
264 "failed to publish item Version. ItemId: %s, versionId: %s, message: %s",
265 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
270 public void syncVersion(SessionContext context, Id itemId, Id versionId) {
271 Response<MergeResult> response =
272 versionAdaptorFactory.createInterface(context).sync(context, itemId, versionId);
273 if (!response.isSuccessful()) {
274 throw new SdcRuntimeException(String.format(
275 "failed to sync item Version. ItemId: %s, versionId: %s, message: %s",
276 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
281 public void forceSyncVersion(SessionContext context, Id itemId, Id versionId) {
282 Response<MergeResult> response =
283 versionAdaptorFactory.createInterface(context).forceSync(context, itemId, versionId);
284 if (!response.isSuccessful()) {
285 throw new SdcRuntimeException(String.format(
286 "failed to force sync item Version. ItemId: %s, versionId: %s, message: %s",
287 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
292 public ItemVersionConflict getVersionConflict(SessionContext context, Id itemId, Id versionId) {
293 Response<ItemVersionConflict> response =
294 versionAdaptorFactory.createInterface(context).getConflict(context, itemId, versionId);
295 if (!response.isSuccessful()) {
296 throw new SdcRuntimeException(String
297 .format("failed to get Item Version conflict. ItemId: %s, versionId: %s, message: %s",
298 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
300 return response.getValue();
304 public Collection<ElementInfo> listElements(SessionContext context,
305 ElementContext elementContext,
306 Id parentElementId) {
307 Response<Collection<ElementInfo>> response = elementAdaptorFactory
308 .createInterface(context).list(context, elementContext, parentElementId);
309 if (response.isSuccessful()) {
310 return response.getValue();
312 logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ELEMENT_GET_BY_PROPERTY,
313 response.getReturnCode());
314 throw new SdcRuntimeException(response.getReturnCode().toString());
320 public ElementInfo getElementInfo(SessionContext context, ElementContext elementContext,
322 Response<ElementInfo> response =
323 elementAdaptorFactory.createInterface(context).getInfo(context, elementContext, elementId);
324 if (!response.isSuccessful()) {
325 throw buildGetElementException(elementContext, elementId,
326 response.getReturnCode().toString());
329 return response.getValue();
333 public Element getElement(SessionContext context, ElementContext elementContext,
335 Response<Element> response =
336 elementAdaptorFactory.createInterface(context).get(context, elementContext, elementId);
337 if (!response.isSuccessful()) {
338 throw buildGetElementException(elementContext, elementId,
339 response.getReturnCode().toString());
341 return response.getValue();
345 public ElementConflict getElementConflict(SessionContext context, ElementContext elementContext,
347 Response<ElementConflict> response = elementAdaptorFactory.createInterface(context)
348 .getConflict(context, elementContext, elementId);
349 if (!response.isSuccessful()) {
350 throw new SdcRuntimeException(String.format(
351 "Failed to get element conflict. Item Id: %s, version Id: %s, element Id: %s message: %s",
352 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
353 elementId.getValue(), response.getReturnCode().toString()));
355 return response.getValue();
359 public Element saveElement(SessionContext context, ElementContext elementContext,
360 ZusammenElement element, String message) {
361 Response<Element> response = elementAdaptorFactory.createInterface(context)
362 .save(context, elementContext, element, message);
363 if (!response.isSuccessful()) {
364 throw new SdcRuntimeException(String
365 .format("Failed to create element %s. ItemId: %s, versionId: %s, message: %s",
366 element.getElementId().getValue(), elementContext.getItemId().getValue(),
367 elementContext.getVersionId().getValue(), response.getReturnCode().toString()));
369 return response.getValue();
373 public void resolveElementConflict(SessionContext context, ElementContext elementContext,
374 ZusammenElement element,
375 Resolution resolution) {
376 Response<Void> response = elementAdaptorFactory.createInterface(context)
377 .resolveConflict(context, elementContext, element, resolution);
378 if (!response.isSuccessful()) {
379 throw new SdcRuntimeException(
380 "Failed to resolve conflict. message:" + response.getReturnCode().toString());
385 public void resetVersionHistory(SessionContext context, Id itemId, Id versionId,
387 // no-op, required by the interface
390 private SdcRuntimeException buildGetElementException(ElementContext elementContext, Id elementId,
391 String zusammenErrorMessage) {
392 if (elementContext.getRevisionId() == null) {
393 return new SdcRuntimeException(String.format(GET_ELEMENT_ERR_MSG,
394 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
395 elementId.getValue(), zusammenErrorMessage));
397 return new SdcRuntimeException(String.format(GET_ELEMENT_IN_REV_ERR_MSG,
398 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
399 elementContext.getRevisionId().getValue(),
400 elementId.getValue(), zusammenErrorMessage));
403 private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName
404 itemElementLoggerTargetServiceName,
405 ReturnCode returnCode) {
406 logErrorMessageToMdc(itemElementLoggerTargetServiceName, returnCode.toString());
409 private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName
410 itemElementLoggerTargetServiceName,
412 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
413 itemElementLoggerTargetServiceName.getDescription(),
414 ErrorLevel.ERROR.name(),
415 LoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode(),