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.
17 package org.openecomp.core.zusammen.plugin.main;
20 import com.amdocs.zusammen.datatypes.Id;
21 import com.amdocs.zusammen.datatypes.SessionContext;
22 import com.amdocs.zusammen.datatypes.Space;
23 import com.amdocs.zusammen.datatypes.item.ElementContext;
24 import com.amdocs.zusammen.datatypes.item.ItemVersion;
25 import com.amdocs.zusammen.datatypes.item.ItemVersionData;
26 import com.amdocs.zusammen.datatypes.response.Response;
27 import com.amdocs.zusammen.plugin.statestore.cassandra.StateStoreImpl;
28 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
29 import com.amdocs.zusammen.sdk.state.types.StateElement;
30 import org.openecomp.core.zusammen.plugin.ZusammenPluginUtil;
31 import org.openecomp.core.zusammen.plugin.collaboration.ElementPrivateStore;
32 import org.openecomp.core.zusammen.plugin.collaboration.impl.ElementPrivateStoreImpl;
33 import org.openecomp.core.zusammen.plugin.dao.ElementRepository;
34 import org.openecomp.core.zusammen.plugin.dao.ElementRepositoryFactory;
35 import org.openecomp.core.zusammen.plugin.dao.VersionDao;
36 import org.openecomp.core.zusammen.plugin.dao.VersionDaoFactory;
37 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
38 import org.openecomp.core.zusammen.plugin.dao.types.VersionDataElement;
39 import org.openecomp.core.zusammen.plugin.dao.types.VersionEntity;
41 import java.util.Collection;
42 import java.util.Date;
43 import java.util.stream.Collectors;
45 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.convertToItemVersion;
46 import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName;
48 public class CassandraStateStorePluginImpl extends StateStoreImpl {
50 private ElementPrivateStore elementPrivateStore = new ElementPrivateStoreImpl();
53 public Response<Collection<ItemVersion>> listItemVersions(SessionContext context, Space space,
55 String spaceName = getSpaceName(context, space);
56 return new Response<>(getVersionDao(context).list(context, spaceName, itemId).stream()
57 .map(versionEntity -> getItemVersion(context, spaceName, itemId, versionEntity))
58 .collect(Collectors.toList()));
62 public Response<Boolean> isItemVersionExist(SessionContext context, Space space, Id itemId,
64 return new Response<>(
65 getVersionDao(context).get(context, getSpaceName(context, space), itemId, versionId)
70 public Response<ItemVersion> getItemVersion(SessionContext context, Space space, Id itemId,
72 String spaceName = getSpaceName(context, space);
73 return new Response<>(getVersionDao(context).get(context, spaceName, itemId, versionId)
74 .map(versionEntity -> getItemVersion(context, spaceName, itemId, versionEntity))
79 public Response<Void> createItemVersion(SessionContext context, Space space, Id itemId,
80 Id baseVersionId, Id versionId, ItemVersionData data,
82 // done by collaboration store
83 return new Response(Void.TYPE);
87 public Response<Void> updateItemVersion(SessionContext context, Space space, Id itemId,
88 Id versionId, ItemVersionData data,
89 Date modificationTime) {
90 // done by collaboration store
91 return new Response(Void.TYPE);
95 public Response<Void> deleteItemVersion(SessionContext context, Space space, Id itemId,
97 // done by collaboration store
98 return new Response(Void.TYPE);
102 public Response<Void> createElement(SessionContext context, StateElement element) {
103 ElementEntity elementEntity = new ElementEntity(element.getId());
104 elementEntity.setNamespace(element.getNamespace());
106 ElementRepositoryFactory.getInstance().createInterface(context)
107 .createNamespace(context,
108 new ElementEntityContext(getSpaceName(context, element.getSpace()),
109 element.getItemId(), element.getVersionId()), elementEntity);
110 // create element is done by collaboration store
111 return new Response(Void.TYPE);
115 public Response<Void> updateElement(SessionContext context, StateElement element) {
116 // done by collaboration store
117 return new Response(Void.TYPE);
121 public Response<Void> deleteElement(SessionContext context, StateElement element) {
122 // done by collaboration store
123 return new Response(Void.TYPE);
127 public Response<Collection<StateElement>> listElements(SessionContext context,
128 ElementContext elementContext,
130 return new Response(elementPrivateStore.listSubs(context, elementContext, elementId).stream()
131 .map(elementEntity -> ZusammenPluginUtil.getStateElement(elementContext, elementEntity))
132 .collect(Collectors.toList()));
137 public Response<StateElement> getElement(SessionContext context, ElementContext elementContext,
140 return new Response(elementPrivateStore.get(context, elementContext, elementId)
141 .map(elementEntity -> ZusammenPluginUtil
142 .getStateElement(elementContext, elementEntity))
149 private ItemVersion getItemVersion(SessionContext context, String spaceName, Id itemId,
150 VersionEntity versionEntity) {
152 ItemVersionData itemVersionData = getElementRepository(context)
153 .get(context, new ElementEntityContext(spaceName, itemId, versionEntity.getId(), null),
154 new VersionDataElement())
155 .map(ZusammenPluginUtil::convertToVersionData)
156 .orElseThrow(() -> new IllegalStateException("Version must have data"));
158 return convertToItemVersion(versionEntity, itemVersionData);
161 protected VersionDao getVersionDao(SessionContext context) {
162 return VersionDaoFactory.getInstance().createInterface(context);
165 protected ElementRepository getElementRepository(SessionContext context) {
166 return ElementRepositoryFactory.getInstance().createInterface(context);