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 deleteItem(SessionContext context, Id itemId){
113 Response<Void> response = itemAdaptorFactory.createInterface(context).delete(context, itemId);
114 if (!response.isSuccessful()) {
115 throw new SdcRuntimeException(
116 "Failed to delete Item. message:" + response.getReturnCode().toString());
121 public void updateItem(SessionContext context, Id itemId, Info info) {
122 Response<Void> response =
123 itemAdaptorFactory.createInterface(context).update(context, itemId, info);
125 if (!response.isSuccessful()) {
126 throw new SdcRuntimeException("failed to update Item . ItemId:" + itemId + "" +
127 " message:" + response.getReturnCode().toString());
132 public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) {
133 Response<Collection<ItemVersion>> versions =
134 versionAdaptorFactory.createInterface(context).list(context, Space.PUBLIC, itemId);
135 if (!versions.isSuccessful()) {
136 throw new SdcRuntimeException("failed to list public versions. message: " +
137 versions.getReturnCode().toString());
139 return versions.getValue();
143 public ItemVersion getPublicVersion(SessionContext context, Id itemId, Id versionId) {
144 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
145 .get(context, Space.PUBLIC, itemId, versionId);
146 if (!response.isSuccessful()) {
147 throw new SdcRuntimeException(
148 String.format("failed to get public Item Version. ItemId: %s, versionId: %s, message: %s",
149 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
151 return response.getValue();
155 public Id createVersion(SessionContext context, Id itemId, Id baseVersionId,
156 ItemVersionData itemVersionData) {
157 Response<Id> response = versionAdaptorFactory.createInterface(context).create(context, itemId,
158 baseVersionId, itemVersionData);
159 if (response.isSuccessful()) {
160 return response.getValue();
162 throw new SdcRuntimeException(String.format(
163 "failed to create Item Version. ItemId: %s, base versionId: %s, message: %s",
164 itemId.getValue(), baseVersionId.getValue(), response.getReturnCode().toString()));
169 public void updateVersion(SessionContext context, Id itemId, Id versionId,
170 ItemVersionData itemVersionData) {
171 Response<Void> response = versionAdaptorFactory.createInterface(context)
172 .update(context, itemId, versionId, itemVersionData);
173 if (!response.isSuccessful()) {
174 throw new SdcRuntimeException(
175 String.format("failed to update Item Version. ItemId: %s, versionId: %s, message: %s",
176 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
181 public ItemVersion getVersion(SessionContext context, Id itemId, Id versionId) {
182 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
183 .get(context, Space.PRIVATE, itemId, versionId);
184 if (!response.isSuccessful()) {
185 throw new SdcRuntimeException(
186 String.format("failed to get Item Version. ItemId: %s, versionId: %s, message: %s",
187 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
189 return response.getValue();
193 public ItemVersionStatus getVersionStatus(SessionContext context, Id itemId, Id versionId) {
194 Response<ItemVersionStatus> response =
195 versionAdaptorFactory.createInterface(context).getStatus(context, itemId, versionId);
196 if (!response.isSuccessful()) {
197 throw new SdcRuntimeException(
198 String.format("failed to get Item Version status. ItemId: %s, versionId: %s, message: %s",
199 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
201 return response.getValue();
205 public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) {
206 Response<Void> response = versionAdaptorFactory.createInterface(context)
207 .tag(context, itemId, versionId, null, tag);
208 if (!response.isSuccessful()) {
209 throw new SdcRuntimeException(String.format(
210 "failed to tag Item Version with tag %s. ItemId: %s, versionId: %s, message: %s",
211 tag.getName(), itemId.getValue(), versionId.getValue(),
212 response.getReturnCode().toString()));
217 public void resetVersionRevision(SessionContext context, Id itemId, Id versionId,
219 Response<Void> response = versionAdaptorFactory.createInterface(context)
220 .resetRevision(context, itemId, versionId, revisionId);
221 if (!response.isSuccessful()) {
222 throw new SdcRuntimeException(String.format(
223 "failed to reset Item Version back to revision: %s. ItemId: %s, versionId: %s, message:" +
225 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
226 response.getReturnCode().toString()));
231 public void revertVersionRevision(SessionContext context, Id itemId, Id versionId,
233 Response<Void> response = versionAdaptorFactory.createInterface(context)
234 .revertRevision(context, itemId, versionId, revisionId);
235 if (!response.isSuccessful()) {
236 throw new SdcRuntimeException(String.format(
237 "failed to revert Item Version back to revision: %s. ItemId: %s, versionId: %s, " +
239 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
240 response.getReturnCode().toString()));
245 public ItemVersionRevisions listVersionRevisions(SessionContext context, Id itemId,
247 Response<ItemVersionRevisions> response =
248 versionAdaptorFactory.createInterface(context)
249 .listRevisions(context, itemId, versionId);
250 if (!response.isSuccessful()) {
251 throw new SdcRuntimeException(String.format(
252 "failed to list revisions of Item Version. ItemId: %s, versionId: %s, message: %s",
253 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
255 return response.getValue();
260 public void publishVersion(SessionContext context, Id itemId, Id versionId, String message) {
261 Response<Void> response =
262 versionAdaptorFactory.createInterface(context).publish(context, itemId, versionId, message);
263 if (!response.isSuccessful()) {
264 throw new SdcRuntimeException(String.format(
265 "failed to publish item Version. ItemId: %s, versionId: %s, message: %s",
266 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
271 public void syncVersion(SessionContext context, Id itemId, Id versionId) {
272 Response<MergeResult> response =
273 versionAdaptorFactory.createInterface(context).sync(context, itemId, versionId);
274 if (!response.isSuccessful()) {
275 throw new SdcRuntimeException(String.format(
276 "failed to sync item Version. ItemId: %s, versionId: %s, message: %s",
277 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
282 public void forceSyncVersion(SessionContext context, Id itemId, Id versionId) {
283 Response<MergeResult> response =
284 versionAdaptorFactory.createInterface(context).forceSync(context, itemId, versionId);
285 if (!response.isSuccessful()) {
286 throw new SdcRuntimeException(String.format(
287 "failed to force sync item Version. ItemId: %s, versionId: %s, message: %s",
288 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
293 public ItemVersionConflict getVersionConflict(SessionContext context, Id itemId, Id versionId) {
294 Response<ItemVersionConflict> response =
295 versionAdaptorFactory.createInterface(context).getConflict(context, itemId, versionId);
296 if (!response.isSuccessful()) {
297 throw new SdcRuntimeException(String
298 .format("failed to get Item Version conflict. ItemId: %s, versionId: %s, message: %s",
299 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
301 return response.getValue();
305 public Collection<ElementInfo> listElements(SessionContext context,
306 ElementContext elementContext,
307 Id parentElementId) {
308 Response<Collection<ElementInfo>> response = elementAdaptorFactory
309 .createInterface(context).list(context, elementContext, parentElementId);
310 if (response.isSuccessful()) {
311 return response.getValue();
313 throw new SdcRuntimeException(response.getReturnCode().toString());
319 public ElementInfo getElementInfo(SessionContext context, ElementContext elementContext,
321 Response<ElementInfo> response =
322 elementAdaptorFactory.createInterface(context).getInfo(context, elementContext, elementId);
323 if (!response.isSuccessful()) {
324 throw buildGetElementException(elementContext, elementId,
325 response.getReturnCode().toString());
328 return response.getValue();
332 public Element getElement(SessionContext context, ElementContext elementContext,
334 Response<Element> response =
335 elementAdaptorFactory.createInterface(context).get(context, elementContext, elementId);
336 if (!response.isSuccessful()) {
337 throw buildGetElementException(elementContext, elementId,
338 response.getReturnCode().toString());
340 return response.getValue();
344 public ElementConflict getElementConflict(SessionContext context, ElementContext elementContext,
346 Response<ElementConflict> response = elementAdaptorFactory.createInterface(context)
347 .getConflict(context, elementContext, elementId);
348 if (!response.isSuccessful()) {
349 throw new SdcRuntimeException(String.format(
350 "Failed to get element conflict. Item Id: %s, version Id: %s, element Id: %s message: %s",
351 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
352 elementId.getValue(), response.getReturnCode().toString()));
354 return response.getValue();
358 public Element saveElement(SessionContext context, ElementContext elementContext,
359 ZusammenElement element, String message) {
360 Response<Element> response = elementAdaptorFactory.createInterface(context)
361 .save(context, elementContext, element, message);
362 if (!response.isSuccessful()) {
363 throw new SdcRuntimeException(String
364 .format("Failed to create element %s. ItemId: %s, versionId: %s, message: %s",
365 element.getElementId().getValue(), elementContext.getItemId().getValue(),
366 elementContext.getVersionId().getValue(), response.getReturnCode().toString()));
368 return response.getValue();
372 public void resolveElementConflict(SessionContext context, ElementContext elementContext,
373 ZusammenElement element,
374 Resolution resolution) {
375 Response<Void> response = elementAdaptorFactory.createInterface(context)
376 .resolveConflict(context, elementContext, element, resolution);
377 if (!response.isSuccessful()) {
378 throw new SdcRuntimeException(
379 "Failed to resolve conflict. message:" + response.getReturnCode().toString());
384 public void resetVersionHistory(SessionContext context, Id itemId, Id versionId,
386 // no-op, required by the interface
389 private SdcRuntimeException buildGetElementException(ElementContext elementContext, Id elementId,
390 String zusammenErrorMessage) {
391 if (elementContext.getRevisionId() == null) {
392 return new SdcRuntimeException(String.format(GET_ELEMENT_ERR_MSG,
393 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
394 elementId.getValue(), zusammenErrorMessage));
396 return new SdcRuntimeException(String.format(GET_ELEMENT_IN_REV_ERR_MSG,
397 elementContext.getItemId().getValue(), elementContext.getVersionId().getValue(),
398 elementContext.getRevisionId().getValue(),
399 elementId.getValue(), zusammenErrorMessage));