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 org.openecomp.core.zusammen.db.ZusammenConnector;
43 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
44 import org.openecomp.sdc.common.errors.SdcRuntimeException;
46 import java.util.Collection;
48 public class ZusammenConnectorImpl implements ZusammenConnector {
50 private static final String GET_ELEMENT_ERR_MSG =
51 "Failed to get element. Item Id: %s, version Id: %s, element Id: %s message: %s";
52 private static final String GET_ELEMENT_IN_REV_ERR_MSG =
53 "Failed to get element. Item Id: %s, version Id: %s, revision Id: %s, element Id: %s message: %s";
54 private final ItemAdaptorFactory itemAdaptorFactory;
55 private final ItemVersionAdaptorFactory versionAdaptorFactory;
56 private final ElementAdaptorFactory elementAdaptorFactory;
57 private final HealthAdaptorFactory healthAdaptorFactory;
59 public ZusammenConnectorImpl(
60 ItemAdaptorFactory itemAdaptorFactory,
61 ItemVersionAdaptorFactory versionAdaptorFactory,
62 ElementAdaptorFactory elementAdaptorFactory,
63 HealthAdaptorFactory healthAdaptorFactory) {
64 this.itemAdaptorFactory = itemAdaptorFactory;
65 this.versionAdaptorFactory = versionAdaptorFactory;
66 this.elementAdaptorFactory = elementAdaptorFactory;
67 this.healthAdaptorFactory = healthAdaptorFactory;
68 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
72 public Collection<HealthInfo> checkHealth(SessionContext sessionContext) {
73 return healthAdaptorFactory.createInterface(sessionContext).getHealthStatus(sessionContext);
77 public String getVersion(SessionContext sessionContext) {
78 return healthAdaptorFactory.createInterface(sessionContext).getVersion();
82 public Collection<Item> listItems(SessionContext context) {
83 Response<Collection<Item>> response = itemAdaptorFactory.createInterface(context).list(context);
84 if (!response.isSuccessful()) {
85 throw new SdcRuntimeException(
86 "Failed to list Items. message:" + response.getReturnCode().toString());
88 return response.getValue();
92 public Item getItem(SessionContext context, Id itemId) {
93 Response<Item> response = itemAdaptorFactory.createInterface(context).get(context, itemId);
94 if (!response.isSuccessful()) {
95 throw new SdcRuntimeException(
96 "Failed to get Item. message:" + response.getReturnCode().toString());
98 return response.getValue();
102 public Id createItem(SessionContext context, Info info) {
103 Response<Id> response = itemAdaptorFactory.createInterface(context).create(context, info);
104 if (!response.isSuccessful()) {
105 throw new SdcRuntimeException(
106 "Failed to create Item. message:" + response.getReturnCode().toString());
108 return response.getValue();
112 public void updateItem(SessionContext context, Id itemId, Info info) {
113 Response<Void> response =
114 itemAdaptorFactory.createInterface(context).update(context, itemId, info);
116 if (!response.isSuccessful()) {
117 throw new SdcRuntimeException("failed to update Item . ItemId:" + itemId + "" +
118 " message:" + response.getReturnCode().toString());
123 public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) {
124 Response<Collection<ItemVersion>> versions =
125 versionAdaptorFactory.createInterface(context).list(context, Space.PUBLIC, itemId);
126 if (!versions.isSuccessful()) {
127 throw new SdcRuntimeException("failed to list public versions. message: " +
128 versions.getReturnCode().toString());
130 return versions.getValue();
134 public ItemVersion getPublicVersion(SessionContext context, Id itemId, Id versionId) {
135 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
136 .get(context, Space.PUBLIC, itemId, versionId);
137 if (!response.isSuccessful()) {
138 throw new SdcRuntimeException(
139 String.format("failed to get public Item Version. ItemId: %s, versionId: %s, message: %s",
140 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
142 return response.getValue();
146 public Id createVersion(SessionContext context, Id itemId, Id baseVersionId,
147 ItemVersionData itemVersionData) {
148 Response<Id> response = versionAdaptorFactory.createInterface(context).create(context, itemId,
149 baseVersionId, itemVersionData);
150 if (response.isSuccessful()) {
151 return response.getValue();
153 throw new SdcRuntimeException(String.format(
154 "failed to create Item Version. ItemId: %s, base versionId: %s, message: %s",
155 itemId.getValue(), baseVersionId.getValue(), response.getReturnCode().toString()));
160 public void updateVersion(SessionContext context, Id itemId, Id versionId,
161 ItemVersionData itemVersionData) {
162 Response<Void> response = versionAdaptorFactory.createInterface(context)
163 .update(context, itemId, versionId, itemVersionData);
164 if (!response.isSuccessful()) {
165 throw new SdcRuntimeException(
166 String.format("failed to update Item Version. ItemId: %s, versionId: %s, message: %s",
167 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
172 public ItemVersion getVersion(SessionContext context, Id itemId, Id versionId) {
173 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
174 .get(context, Space.PRIVATE, itemId, versionId);
175 if (!response.isSuccessful()) {
176 throw new SdcRuntimeException(
177 String.format("failed to get Item Version. ItemId: %s, versionId: %s, message: %s",
178 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
180 return response.getValue();
184 public ItemVersionStatus getVersionStatus(SessionContext context, Id itemId, Id versionId) {
185 Response<ItemVersionStatus> response =
186 versionAdaptorFactory.createInterface(context).getStatus(context, itemId, versionId);
187 if (!response.isSuccessful()) {
188 throw new SdcRuntimeException(
189 String.format("failed to get Item Version status. ItemId: %s, versionId: %s, message: %s",
190 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
192 return response.getValue();
196 public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) {
197 Response<Void> response = versionAdaptorFactory.createInterface(context)
198 .tag(context, itemId, versionId, null, tag);
199 if (!response.isSuccessful()) {
200 throw new SdcRuntimeException(String.format(
201 "failed to tag Item Version with tag %s. ItemId: %s, versionId: %s, message: %s",
202 tag.getName(), itemId.getValue(), versionId.getValue(),
203 response.getReturnCode().toString()));
208 public void resetVersionRevision(SessionContext context, Id itemId, Id versionId,
210 Response<Void> response = versionAdaptorFactory.createInterface(context)
211 .resetRevision(context, itemId, versionId, revisionId);
212 if (!response.isSuccessful()) {
213 throw new SdcRuntimeException(String.format(
214 "failed to reset Item Version back to revision: %s. ItemId: %s, versionId: %s, message:" +
216 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
217 response.getReturnCode().toString()));
222 public void revertVersionRevision(SessionContext context, Id itemId, Id versionId,
224 Response<Void> response = versionAdaptorFactory.createInterface(context)
225 .revertRevision(context, itemId, versionId, revisionId);
226 if (!response.isSuccessful()) {
227 throw new SdcRuntimeException(String.format(
228 "failed to revert Item Version back to revision: %s. ItemId: %s, versionId: %s, " +
230 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
231 response.getReturnCode().toString()));
236 public ItemVersionRevisions listVersionRevisions(SessionContext context, Id itemId,
238 Response<ItemVersionRevisions> response =
239 versionAdaptorFactory.createInterface(context)
240 .listRevisions(context, itemId, versionId);
241 if (!response.isSuccessful()) {
242 throw new SdcRuntimeException(String.format(
243 "failed to list revisions of Item Version. ItemId: %s, versionId: %s, message: %s",
244 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
246 return response.getValue();
251 public void publishVersion(SessionContext context, Id itemId, Id versionId, String message) {
252 Response<Void> response =
253 versionAdaptorFactory.createInterface(context).publish(context, itemId, versionId, message);
254 if (!response.isSuccessful()) {
255 throw new SdcRuntimeException(String.format(
256 "failed to publish item Version. ItemId: %s, versionId: %s, message: %s",
257 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
262 public void syncVersion(SessionContext context, Id itemId, Id versionId) {
263 Response<MergeResult> response =
264 versionAdaptorFactory.createInterface(context).sync(context, itemId, versionId);
265 if (!response.isSuccessful()) {
266 throw new SdcRuntimeException(String.format(
267 "failed to sync item Version. ItemId: %s, versionId: %s, message: %s",
268 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
273 public void forceSyncVersion(SessionContext context, Id itemId, Id versionId) {
274 Response<MergeResult> response =
275 versionAdaptorFactory.createInterface(context).forceSync(context, itemId, versionId);
276 if (!response.isSuccessful()) {
277 throw new SdcRuntimeException(String.format(
278 "failed to force sync item Version. ItemId: %s, versionId: %s, message: %s",
279 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
284 public ItemVersionConflict getVersionConflict(SessionContext context, Id itemId, Id versionId) {
285 Response<ItemVersionConflict> response =
286 versionAdaptorFactory.createInterface(context).getConflict(context, itemId, versionId);
287 if (!response.isSuccessful()) {
288 throw new SdcRuntimeException(String
289 .format("failed to get Item Version conflict. ItemId: %s, versionId: %s, message: %s",
290 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
292 return response.getValue();
296 public Collection<ElementInfo> listElements(SessionContext context,
297 ElementContext elementContext,
298 Id parentElementId) {
299 Response<Collection<ElementInfo>> response = elementAdaptorFactory
300 .createInterface(context).list(context, elementContext, parentElementId);
301 if (response.isSuccessful()) {
302 return response.getValue();
304 throw new SdcRuntimeException(response.getReturnCode().toString());
310 public ElementInfo getElementInfo(SessionContext context, ElementContext elementContext,
312 Response<ElementInfo> response =
313 elementAdaptorFactory.createInterface(context).getInfo(context, elementContext, elementId);
314 if (!response.isSuccessful()) {
315 throw buildGetElementException(elementContext, elementId,
316 response.getReturnCode().toString());
319 return response.getValue();
323 public Element getElement(SessionContext context, ElementContext elementContext,
325 Response<Element> response =
326 elementAdaptorFactory.createInterface(context).get(context, elementContext, elementId);
327 if (!response.isSuccessful()) {
328 throw buildGetElementException(elementContext, elementId,
329 response.getReturnCode().toString());
331 return response.getValue();
335 public ElementConflict getElementConflict(SessionContext context, ElementContext elementContext,
337 Response<ElementConflict> response = elementAdaptorFactory.createInterface(context)
338 .getConflict(context, elementContext, elementId);
339 if (!response.isSuccessful()) {
340 throw new SdcRuntimeException(String.format(
341 "Failed to get element conflict. Item Id: %s, version Id: %s, element Id: %s message: %s",
342 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
343 elementId.getValue(), response.getReturnCode().toString()));
345 return response.getValue();
349 public Element saveElement(SessionContext context, ElementContext elementContext,
350 ZusammenElement element, String message) {
351 Response<Element> response = elementAdaptorFactory.createInterface(context)
352 .save(context, elementContext, element, message);
353 if (!response.isSuccessful()) {
354 throw new SdcRuntimeException(String
355 .format("Failed to create element %s. ItemId: %s, versionId: %s, message: %s",
356 element.getElementId().getValue(), elementContext.getItemId().getValue(),
357 elementContext.getVersionId().getValue(), response.getReturnCode().toString()));
359 return response.getValue();
363 public void resolveElementConflict(SessionContext context, ElementContext elementContext,
364 ZusammenElement element,
365 Resolution resolution) {
366 Response<Void> response = elementAdaptorFactory.createInterface(context)
367 .resolveConflict(context, elementContext, element, resolution);
368 if (!response.isSuccessful()) {
369 throw new SdcRuntimeException(
370 "Failed to resolve conflict. message:" + response.getReturnCode().toString());
375 public void resetVersionHistory(SessionContext context, Id itemId, Id versionId,
377 // no-op, required by the interface
380 private SdcRuntimeException buildGetElementException(ElementContext elementContext, Id elementId,
381 String zusammenErrorMessage) {
382 if (elementContext.getRevisionId() == null) {
383 return new SdcRuntimeException(String.format(GET_ELEMENT_ERR_MSG,
384 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
385 elementId.getValue(), zusammenErrorMessage));
387 return new SdcRuntimeException(String.format(GET_ELEMENT_IN_REV_ERR_MSG,
388 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
389 elementContext.getRevisionId().getValue(),
390 elementId.getValue(), zusammenErrorMessage));