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.*;
23 import com.amdocs.zusammen.commons.health.data.HealthInfo;
24 import com.amdocs.zusammen.datatypes.Id;
25 import com.amdocs.zusammen.datatypes.SessionContext;
26 import com.amdocs.zusammen.datatypes.Space;
27 import com.amdocs.zusammen.datatypes.item.*;
28 import com.amdocs.zusammen.datatypes.itemversion.ItemVersionRevisions;
29 import com.amdocs.zusammen.datatypes.itemversion.Tag;
30 import com.amdocs.zusammen.datatypes.response.Response;
31 import org.openecomp.core.zusammen.db.ZusammenConnector;
32 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
33 import org.openecomp.sdc.common.errors.SdcRuntimeException;
35 import java.util.Collection;
37 public class ZusammenConnectorImpl implements ZusammenConnector {
39 private static final String GET_ELEMENT_ERR_MSG =
40 "Failed to get element. Item Id: %s, version Id: %s, element Id: %s message: %s";
41 private static final String GET_ELEMENT_IN_REV_ERR_MSG =
42 "Failed to get element. Item Id: %s, version Id: %s, revision Id: %s, element Id: %s message: %s";
43 private final ItemAdaptorFactory itemAdaptorFactory;
44 private final ItemVersionAdaptorFactory versionAdaptorFactory;
45 private final ElementAdaptorFactory elementAdaptorFactory;
46 private final HealthAdaptorFactory healthAdaptorFactory;
48 public ZusammenConnectorImpl(
49 ItemAdaptorFactory itemAdaptorFactory,
50 ItemVersionAdaptorFactory versionAdaptorFactory,
51 ElementAdaptorFactory elementAdaptorFactory,
52 HealthAdaptorFactory healthAdaptorFactory) {
53 this.itemAdaptorFactory = itemAdaptorFactory;
54 this.versionAdaptorFactory = versionAdaptorFactory;
55 this.elementAdaptorFactory = elementAdaptorFactory;
56 this.healthAdaptorFactory = healthAdaptorFactory;
57 CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
61 public Collection<HealthInfo> checkHealth(SessionContext sessionContext) {
62 return healthAdaptorFactory.createInterface(sessionContext).getHealthStatus(sessionContext);
66 public String getVersion(SessionContext sessionContext) {
67 return healthAdaptorFactory.createInterface(sessionContext).getVersion();
71 public Collection<Item> listItems(SessionContext context) {
72 Response<Collection<Item>> response = itemAdaptorFactory.createInterface(context).list(context);
73 if (!response.isSuccessful()) {
74 throw new SdcRuntimeException(
75 "Failed to list Items. message:" + response.getReturnCode().toString());
77 return response.getValue();
81 public Item getItem(SessionContext context, Id itemId) {
82 Response<Item> response = itemAdaptorFactory.createInterface(context).get(context, itemId);
83 if (!response.isSuccessful()) {
84 throw new SdcRuntimeException(
85 "Failed to get Item. message:" + response.getReturnCode().toString());
87 return response.getValue();
91 public Id createItem(SessionContext context, Info info) {
92 Response<Id> response = itemAdaptorFactory.createInterface(context).create(context, info);
93 if (!response.isSuccessful()) {
94 throw new SdcRuntimeException(
95 "Failed to create Item. message:" + response.getReturnCode().toString());
97 return response.getValue();
101 public void deleteItem(SessionContext context, Id itemId){
102 Response<Void> response = itemAdaptorFactory.createInterface(context).delete(context, itemId);
103 if (!response.isSuccessful()) {
104 throw new SdcRuntimeException(
105 "Failed to delete Item. message:" + response.getReturnCode().toString());
110 public void updateItem(SessionContext context, Id itemId, Info info) {
111 Response<Void> response =
112 itemAdaptorFactory.createInterface(context).update(context, itemId, info);
114 if (!response.isSuccessful()) {
115 throw new SdcRuntimeException("failed to update Item . ItemId:" + itemId + "" +
116 " message:" + response.getReturnCode().toString());
121 public Collection<ItemVersion> listPublicVersions(SessionContext context, Id itemId) {
122 Response<Collection<ItemVersion>> versions =
123 versionAdaptorFactory.createInterface(context).list(context, Space.PUBLIC, itemId);
124 if (!versions.isSuccessful()) {
125 throw new SdcRuntimeException("failed to list public versions. message: " +
126 versions.getReturnCode().toString());
128 return versions.getValue();
132 public ItemVersion getPublicVersion(SessionContext context, Id itemId, Id versionId) {
133 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
134 .get(context, Space.PUBLIC, itemId, versionId);
135 if (!response.isSuccessful()) {
136 throw new SdcRuntimeException(
137 String.format("failed to get public Item Version. ItemId: %s, versionId: %s, message: %s",
138 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
140 return response.getValue();
144 public Id createVersion(SessionContext context, Id itemId, Id baseVersionId,
145 ItemVersionData itemVersionData) {
146 Response<Id> response = versionAdaptorFactory.createInterface(context).create(context, itemId,
147 baseVersionId, itemVersionData);
148 if (response.isSuccessful()) {
149 return response.getValue();
151 throw new SdcRuntimeException(String.format(
152 "failed to create Item Version. ItemId: %s, base versionId: %s, message: %s",
153 itemId.getValue(), baseVersionId.getValue(), response.getReturnCode().toString()));
158 public void updateVersion(SessionContext context, Id itemId, Id versionId,
159 ItemVersionData itemVersionData) {
160 Response<Void> response = versionAdaptorFactory.createInterface(context)
161 .update(context, itemId, versionId, itemVersionData);
162 if (!response.isSuccessful()) {
163 throw new SdcRuntimeException(
164 String.format("failed to update Item Version. ItemId: %s, versionId: %s, message: %s",
165 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
170 public ItemVersion getVersion(SessionContext context, Id itemId, Id versionId) {
171 Response<ItemVersion> response = versionAdaptorFactory.createInterface(context)
172 .get(context, Space.PRIVATE, itemId, versionId);
173 if (!response.isSuccessful()) {
174 throw new SdcRuntimeException(
175 String.format("failed to get Item Version. ItemId: %s, versionId: %s, message: %s",
176 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
178 return response.getValue();
182 public ItemVersionStatus getVersionStatus(SessionContext context, Id itemId, Id versionId) {
183 Response<ItemVersionStatus> response =
184 versionAdaptorFactory.createInterface(context).getStatus(context, itemId, versionId);
185 if (!response.isSuccessful()) {
186 throw new SdcRuntimeException(
187 String.format("failed to get Item Version status. ItemId: %s, versionId: %s, message: %s",
188 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
190 return response.getValue();
194 public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) {
195 Response<Void> response = versionAdaptorFactory.createInterface(context)
196 .tag(context, itemId, versionId, null, tag);
197 if (!response.isSuccessful()) {
198 throw new SdcRuntimeException(String.format(
199 "failed to tag Item Version with tag %s. ItemId: %s, versionId: %s, message: %s",
200 tag.getName(), itemId.getValue(), versionId.getValue(),
201 response.getReturnCode().toString()));
206 public void resetVersionRevision(SessionContext context, Id itemId, Id versionId,
208 Response<Void> response = versionAdaptorFactory.createInterface(context)
209 .resetRevision(context, itemId, versionId, revisionId);
210 if (!response.isSuccessful()) {
211 throw new SdcRuntimeException(String.format(
212 "failed to reset Item Version back to revision: %s. ItemId: %s, versionId: %s, message:" +
214 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
215 response.getReturnCode().toString()));
220 public void revertVersionRevision(SessionContext context, Id itemId, Id versionId,
222 Response<Void> response = versionAdaptorFactory.createInterface(context)
223 .revertRevision(context, itemId, versionId, revisionId);
224 if (!response.isSuccessful()) {
225 throw new SdcRuntimeException(String.format(
226 "failed to revert Item Version back to revision: %s. ItemId: %s, versionId: %s, " +
228 revisionId.getValue(), itemId.getValue(), versionId.getValue(),
229 response.getReturnCode().toString()));
234 public ItemVersionRevisions listVersionRevisions(SessionContext context, Id itemId,
236 Response<ItemVersionRevisions> response =
237 versionAdaptorFactory.createInterface(context)
238 .listRevisions(context, itemId, versionId);
239 if (!response.isSuccessful()) {
240 throw new SdcRuntimeException(String.format(
241 "failed to list revisions of Item Version. ItemId: %s, versionId: %s, message: %s",
242 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
244 return response.getValue();
249 public void publishVersion(SessionContext context, Id itemId, Id versionId, String message) {
250 Response<Void> response =
251 versionAdaptorFactory.createInterface(context).publish(context, itemId, versionId, message);
252 if (!response.isSuccessful()) {
253 throw new SdcRuntimeException(String.format(
254 "failed to publish item Version. ItemId: %s, versionId: %s, message: %s",
255 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
260 public void syncVersion(SessionContext context, Id itemId, Id versionId) {
261 Response<MergeResult> response =
262 versionAdaptorFactory.createInterface(context).sync(context, itemId, versionId);
263 if (!response.isSuccessful()) {
264 throw new SdcRuntimeException(String.format(
265 "failed to sync item Version. ItemId: %s, versionId: %s, message: %s",
266 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
271 public void forceSyncVersion(SessionContext context, Id itemId, Id versionId) {
272 Response<MergeResult> response =
273 versionAdaptorFactory.createInterface(context).forceSync(context, itemId, versionId);
274 if (!response.isSuccessful()) {
275 throw new SdcRuntimeException(String.format(
276 "failed to force sync item Version. ItemId: %s, versionId: %s, message: %s",
277 itemId.getValue(), versionId.getValue(), response.getReturnCode().toString()));
282 public void cleanVersion(SessionContext context, Id itemId, Id versionId) {
283 Response<Void> response =
284 versionAdaptorFactory.createInterface(context).delete(context, itemId, versionId);
285 if (!response.isSuccessful()) {
286 throw new SdcRuntimeException(String.format(
287 "failed to clean 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));