2 * Copyright © 2016-2018 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.sdc.action.dao.impl;
19 import com.datastax.driver.core.ResultSet;
20 import com.datastax.driver.core.Row;
21 import com.datastax.driver.core.Statement;
22 import com.datastax.driver.core.exceptions.NoHostAvailableException;
23 import com.datastax.driver.core.querybuilder.QueryBuilder;
24 import com.datastax.driver.mapping.Mapper;
25 import com.datastax.driver.mapping.Result;
26 import com.datastax.driver.mapping.annotations.Accessor;
27 import com.datastax.driver.mapping.annotations.Query;
28 import org.openecomp.core.dao.impl.CassandraBaseDao;
29 import org.openecomp.core.nosqldb.api.NoSqlDb;
30 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
31 import org.openecomp.core.utilities.json.JsonUtil;
32 import org.openecomp.sdc.action.ActionConstants;
33 import org.openecomp.sdc.action.dao.ActionDao;
34 import org.openecomp.sdc.action.dao.types.ActionEntity;
35 import org.openecomp.sdc.action.dao.types.OpenEcompComponentEntity;
36 import org.openecomp.sdc.action.errors.ActionException;
37 import org.openecomp.sdc.action.logging.CategoryLogLevel;
38 import org.openecomp.sdc.action.logging.StatusCode;
39 import org.openecomp.sdc.action.types.Action;
40 import org.openecomp.sdc.action.types.ActionStatus;
41 import org.openecomp.sdc.action.types.ActionSubOperation;
42 import org.openecomp.sdc.action.types.OpenEcompComponent;
43 import org.openecomp.sdc.action.util.ActionUtil;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
47 import org.openecomp.sdc.versioning.dao.VersionInfoDao;
48 import org.openecomp.sdc.versioning.dao.VersionInfoDaoFactory;
49 import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDao;
50 import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDaoFactory;
51 import org.openecomp.sdc.versioning.dao.types.Version;
52 import org.openecomp.sdc.versioning.dao.types.VersionInfoDeletedEntity;
53 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
54 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
58 import java.util.stream.Collectors;
60 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
61 import static org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory.getSession;
62 import static org.openecomp.sdc.action.ActionConstants.*;
63 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
66 public class ActionDaoImpl extends CassandraBaseDao<ActionEntity> implements ActionDao {
67 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
68 private static Mapper<ActionEntity> mapper =
69 noSqlDb.getMappingManager().mapper(ActionEntity.class);
70 private static ActionAccessor accessor =
71 noSqlDb.getMappingManager().createAccessor(ActionAccessor.class);
72 private static VersionInfoDao versionInfoDao =
73 VersionInfoDaoFactory.getInstance().createInterface();
74 private static VersionInfoDeletedDao versionInfoDeletedDao =
75 VersionInfoDeletedDaoFactory.getInstance().createInterface();
77 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
80 public void registerVersioning(String versionableEntityType) {
81 ActionVersioningManagerFactory.getInstance().createInterface()
82 .register(versionableEntityType, new VersionableEntityMetadata(
83 mapper.getTableMetadata().getName(),
84 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
85 mapper.getTableMetadata().getPartitionKey().get(1).getName()));
89 public Action createAction(Action action) {
91 ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ENTITY, TARGET_ENTITY_DB);
92 this.create(action.toEntity());
93 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
96 } catch (NoHostAvailableException exception) {
97 logGenericException(exception);
98 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
99 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
104 public Action updateAction(Action action) {
106 log.debug(" entering updateAction with actionUUID= " + action.getActionUuId());
107 ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION, TARGET_ENTITY_DB);
108 this.update(action.toEntity());
109 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
111 log.debug(" exit updateAction with actionUUID= " + action.getActionUuId());
113 } catch (NoHostAvailableException exception) {
114 logGenericException(exception);
115 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
116 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
121 public void deleteAction(String actionInvariantUuId) {
123 log.debug("entering deleteAction with actionInvariantUuId = " + actionInvariantUuId);
124 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
125 VersionInfoDeletedEntity activeVersionEntity = versionInfoDeletedDao.get(
126 new VersionInfoDeletedEntity(ActionConstants.ACTION_VERSIONABLE_TYPE,
127 actionInvariantUuId));
128 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
131 Version activeVersion = activeVersionEntity.getActiveVersion();
132 Statement getNameFromInvUuId = QueryBuilder.select().column("name").from("dox", "Action")
133 .where(eq("actioninvariantuuid", actionInvariantUuId))
134 .and(in("version", activeVersion));
136 .actionLogPreProcessor(ActionSubOperation.GET_NAME_BY_ACTIONINVID, TARGET_ENTITY_DB);
137 ResultSet results = getSession().execute(getNameFromInvUuId);
138 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
141 if (!results.isExhausted()) {
142 String name = results.one().getString("name");
143 List<Version> versions = getVersionsByName(name);
144 updateActionStatusForDelete(actionInvariantUuId, versions);
146 } catch (NoHostAvailableException noHostAvailableException) {
147 logGenericException(noHostAvailableException);
148 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
149 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
151 log.debug("exit deleteAction");
155 public List<Action> getFilteredActions(String filterType, String filterId) {
156 List<Action> actions = new ArrayList<>();
157 Result<ActionEntity> result = null;
159 " entering getFilteredActions By filterType = " + filterType + " With value = " + filterId);
161 switch (filterType) {
162 case FILTER_TYPE_VENDOR:
164 .actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_VENDOR,
166 result = accessor.getActionsByVendor(filterId);
167 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
170 case FILTER_TYPE_CATEGORY:
171 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_CATEGORY,
173 result = accessor.getActionsByCategory(filterId);
174 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
177 case FILTER_TYPE_MODEL:
179 .actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_MODEL,
181 result = accessor.getActionsByModel(filterId);
182 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
185 case FILTER_TYPE_OPEN_ECOMP_COMPONENT:
186 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_COMPONENT,
188 result = accessor.getActionsByOpenEcompComponent(filterId);
189 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
192 case FILTER_TYPE_NONE:
193 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ALL_ACTIONS, TARGET_ENTITY_DB);
194 result = accessor.getAllActions();
195 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
198 case FILTER_TYPE_NAME:
200 .actionLogPreProcessor(ActionSubOperation.GET_ACTIONINVID_BY_NAME, TARGET_ENTITY_DB);
201 result = accessor.getInvIdByName(filterId);
202 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
204 List<ActionEntity> actionEntities = result.all();
205 if (actionEntities != null && !actionEntities.isEmpty()) {
206 String actionInvariantUuId = actionEntities.get(0).getActionInvariantUuId();
207 if (actionInvariantUuId != null) {
208 return getActionsByActionInvariantUuId(actionInvariantUuId);
217 if (result != null) {
218 actions.addAll(result.all().stream().map(ActionEntity::toDto).collect(Collectors.toList()));
221 " exit getFilteredActions By filterType = " + filterType + " With value = " + filterId);
222 } catch (NoHostAvailableException noHostAvailableException) {
223 logGenericException(noHostAvailableException);
224 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
225 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
231 public Action getActionsByActionUuId(String actionUuId) {
233 log.debug(" entering getActionsByActionUuId with actionUUID= " + actionUuId);
234 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONUUID,
236 Result<ActionEntity> result = accessor.actionInvariantUuId(actionUuId);
237 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
239 if (result != null) {
240 log.debug(" exit getActionsByActionUuId with actionUUID= " + actionUuId);
241 ActionEntity entity = result.one();
242 if (entity != null) {
243 return entity.toDto();
246 log.debug(" exit getActionsByActionUuId with actionUUID= " + actionUuId);
248 } catch (NoHostAvailableException noHostAvailableException) {
249 logGenericException(noHostAvailableException);
250 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
251 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
256 public List<OpenEcompComponent> getOpenEcompComponents() {
257 List<OpenEcompComponent> openEcompComponents = new ArrayList<>();
258 Result<OpenEcompComponentEntity> result;
260 log.debug(" entering getOpenEcompComponents ");
262 .actionLogPreProcessor(ActionSubOperation.GET_OPEN_ECOMP_COMPONENTS_ENTITY,
264 result = accessor.getOpenEcompComponents();
265 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
267 if (result != null) {
268 openEcompComponents.addAll(
269 result.all().stream().map(OpenEcompComponentEntity::toDto)
270 .collect(Collectors.toList()));
272 } catch (NoHostAvailableException noHostAvailableException) {
273 logGenericException(noHostAvailableException);
274 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
275 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
277 log.debug(" exit getOpenEcompComponents ");
278 return openEcompComponents;
282 public List<Action> getActionsByActionInvariantUuId(String actionInvariantUuId) {
283 List<Action> actions = new ArrayList<Action>();
285 log.debug(" entering getActionsByActionInvariantUuId with actionInvariantUuId= "
286 + actionInvariantUuId);
287 Set<Version> viewableVersions = new HashSet<>();
288 VersionPredicate filter = new VersionPredicate();
289 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
290 VersionInfoEntity versionInfoEntity = versionInfoDao
291 .get(new VersionInfoEntity(ActionConstants.ACTION_VERSIONABLE_TYPE, actionInvariantUuId));
292 if (versionInfoEntity == null) {
293 //Check for action in the Delete version info table
294 VersionInfoDeletedEntity versionInfoDeletedEntity = versionInfoDeletedDao.get(
295 new VersionInfoDeletedEntity(ActionConstants.ACTION_VERSIONABLE_TYPE,
296 actionInvariantUuId));
297 if (versionInfoDeletedEntity != null) {
298 viewableVersions = versionInfoDeletedEntity.getViewableVersions();
299 //Remove intermediate minor versions from viewable versions
300 if (versionInfoDeletedEntity.getActiveVersion() != null) {
301 filter.activeVersion = versionInfoDeletedEntity.getActiveVersion();
302 filter.finalVersion = versionInfoDeletedEntity.getLatestFinalVersion();
303 viewableVersions.removeIf(filter::isIntermediateMinorVersion);
307 viewableVersions = versionInfoEntity.getViewableVersions();
308 //Remove intermediate minor versions from viewable versions
309 if (versionInfoEntity.getActiveVersion() != null) {
310 filter.activeVersion = versionInfoEntity.getActiveVersion();
311 filter.finalVersion = versionInfoEntity.getLatestFinalVersion();
312 viewableVersions.removeIf(filter::isIntermediateMinorVersion);
314 //Add candidate version if available
315 if (versionInfoEntity.getCandidate() != null) {
316 viewableVersions.add(versionInfoEntity.getCandidate().getVersion());
320 MDC.put(TARGET_ENTITY, TARGET_ENTITY_DB);
321 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
325 "Found " + viewableVersions + " viewable version for action with actionInvariantUuId "
326 + actionInvariantUuId);
328 //Fetch action data for the viewable versions
329 if (!viewableVersions.isEmpty()) {
330 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID,
332 Result<ActionEntity> result =
333 accessor.getActionsByInvId(actionInvariantUuId, viewableVersions);
334 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
336 if (result != null) {
338 .addAll(result.all().stream().map(ActionEntity::toDto).collect(Collectors.toList()));
341 } catch (NoHostAvailableException noHostAvailableException) {
342 logGenericException(noHostAvailableException);
343 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
344 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
347 " exit getActionsByActionInvariantUuId with actionInvariantUuId= " + actionInvariantUuId);
352 public Action getLockedAction(String actionInvariantUuId, String user) throws ActionException {
353 log.debug(" entering getLockedAction with actionInvariantUuId= " + actionInvariantUuId);
354 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
355 Action action = null;
356 VersionInfoEntity versionInfoEntity = versionInfoDao
357 .get(new VersionInfoEntity(ActionConstants.ACTION_VERSIONABLE_TYPE, actionInvariantUuId));
358 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
360 if (versionInfoEntity != null) {
361 if (versionInfoEntity.getCandidate() != null) {
362 String actionUser = versionInfoEntity.getCandidate().getUser();
363 if (actionUser != null && actionUser.equals(user)) {
364 Set<Version> versions = new HashSet<>();
365 versions.add(versionInfoEntity.getCandidate().getVersion());
366 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID,
368 Result<ActionEntity> result = accessor.getActionsByInvId(actionInvariantUuId, versions);
369 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
371 if (result != null) {
372 ActionEntity actionEntity = result.one();
373 action = actionEntity != null ? actionEntity.toDto() : null;
376 throw new ActionException(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE,
377 String.format(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER, actionUser));
380 throw new ActionException(ACTION_NOT_LOCKED_CODE, ACTION_NOT_LOCKED_MSG);
383 throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
388 private void logGenericException(Exception exception) {
389 ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
390 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
392 ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
393 ACTION_QUERY_FAILURE_MSG);
394 log.error(exception.getMessage());
398 protected Mapper<ActionEntity> getMapper() {
404 protected Object[] getKeys(ActionEntity entity) {
405 return new Object[]{entity.getActionInvariantUuId(), entity.getVersion()};
409 public Collection<ActionEntity> list(ActionEntity entity) {
410 return accessor.getAllActions().all();
413 private void updateActionStatusForDelete(String actionInvariantUuId, List<Version> versions) {
415 "entering updateActionStatusForDelete with actionInvariantUuId = " + actionInvariantUuId
416 + " for versions " + versions);
417 ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_STATUS, TARGET_ENTITY_DB);
418 //Update the status column of action table
419 Statement updateStatusStatement =
420 QueryBuilder.update("dox", "Action").with(set("status", ActionStatus.Deleted.name()))
421 .where(eq("actioninvariantuuid", actionInvariantUuId)).and(in("version", versions));
422 getSession().execute(updateStatusStatement);
423 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
425 //Update the status in the data field of action table
426 updateStatusInActionData(actionInvariantUuId, versions, ActionStatus.Deleted);
427 log.debug("exit updateActionStatusForDelete with actionInvariantUuId = " + actionInvariantUuId
428 + " for versions " + versions);
432 * Update status for a list of versions for a given action.
434 * @param actionInvariantUuId Invariant UUID of the action.
435 * @param versions List of {@link Version} for which the status has to be updated.
436 * @param status The status value.
438 private void updateStatusInActionData(String actionInvariantUuId, List<Version> versions,
439 ActionStatus status) {
440 log.debug("entering updateStatusInActionData for actionInvariantUuId = " + actionInvariantUuId
441 + " and status = " + status + " for versions " + versions);
442 for (Version v : versions) {
443 ActionEntity entity = this.get(new ActionEntity(actionInvariantUuId, v));
444 String currentData = entity.getData();
445 Map<String, Object> currentDataMap = JsonUtil.json2Object(currentData, LinkedHashMap.class);
446 currentDataMap.put(STATUS, status);
447 String updatedActionData = JsonUtil.object2Json(currentDataMap);
448 entity.setData(updatedActionData);
449 this.updateAction(entity.toDto());
451 log.debug("exit updateStatusInActionData");
455 * Get list of all major and minor version values for a given action by action name.
457 * @param name Name of the action.
458 * @return List of {@link Version} objects for the action.
460 private List<Version> getVersionsByName(String name) {
461 log.debug("entering getVersionsByName for Action Name = " + name);
462 ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
463 Statement statement =
464 QueryBuilder.select().column("version").from("dox", "Action").where(eq("name", name));
465 ResultSet results = getSession().execute(statement);
466 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
469 List<Version> versionList = new ArrayList<>();
470 for (Row row : results) {
471 Version version = row.get("version",Version.class);
472 versionList.add(version);
474 log.debug("exit getVersionsByName for Action Name = " + name);
479 interface ActionAccessor {
481 @Query("SELECT * FROM Action")
482 Result<ActionEntity> getAllActions();
484 @Query("SELECT * FROM Action where actionInvariantUuId = ? and version in ? ")
485 Result<ActionEntity> getActionsByInvId(String actionInvariantUuId, Set<Version> versions);
487 @Query("SELECT * FROM Action where supportedModels CONTAINS ?")
488 Result<ActionEntity> getActionsByModel(String resource);
490 @Query("SELECT * FROM Action where supportedComponents CONTAINS ?")
491 Result<ActionEntity> getActionsByOpenEcompComponent(String resource);
493 @Query("SELECT * FROM Action where vendor_list CONTAINS ?")
494 Result<ActionEntity> getActionsByVendor(String vendor);
496 @Query("SELECT * FROM Action where category_list CONTAINS ?")
497 Result<ActionEntity> getActionsByCategory(String vendor);
499 @Query("SELECT actionInvariantUuId FROM Action where name = ? limit 1")
500 Result<ActionEntity> getInvIdByName(String name);
502 @Query("SELECT * FROM EcompComponent")
503 Result<OpenEcompComponentEntity> getOpenEcompComponents();
505 @Query("SELECT * FROM Action where actionUUID = ?")
506 Result<ActionEntity> actionInvariantUuId(String actionUuId);
510 class VersionPredicate {
511 Version activeVersion;
512 Version finalVersion;
514 public boolean isIntermediateMinorVersion(Version version) {
515 int activeMajorVersion = activeVersion.getMajor();
516 int activeMinorVersion = activeVersion.getMinor();
517 int currentMinorVersion = version.getMinor();
518 int currentMajorVersion = version.getMajor();
520 if (finalVersion != null) {
521 if (finalVersion.getMajor() == activeMajorVersion
522 && currentMajorVersion == finalVersion.getMajor()) {
523 if (currentMinorVersion < activeMinorVersion && currentMinorVersion != 0) {
530 if (!version.equals(activeVersion)) {