[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-action-lib / openecomp-sdc-action-core / src / main / java / org / openecomp / sdc / action / dao / impl / ActionDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.action.dao.impl;
22
23 import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
24 import static com.datastax.driver.core.querybuilder.QueryBuilder.in;
25 import static com.datastax.driver.core.querybuilder.QueryBuilder.set;
26 import static org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory.getSession;
27 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_CATEGORY;
28 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_OPEN_ECOMP_COMPONENT;
29 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_MODEL;
30 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_NAME;
31 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_NONE;
32 import static org.openecomp.sdc.action.ActionConstants.FILTER_TYPE_VENDOR;
33 import static org.openecomp.sdc.action.ActionConstants.STATUS;
34 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY;
35 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY_DB;
36 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER;
37 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE;
38 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG;
39 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST;
40 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE;
41 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
42 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_CODE;
43 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_NOT_LOCKED_MSG;
44 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_CODE;
45 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_MSG;
46
47 import com.datastax.driver.core.ResultSet;
48 import com.datastax.driver.core.Row;
49 import com.datastax.driver.core.Statement;
50 import com.datastax.driver.core.UDTValue;
51 import com.datastax.driver.core.exceptions.NoHostAvailableException;
52 import com.datastax.driver.core.querybuilder.QueryBuilder;
53 import com.datastax.driver.mapping.Mapper;
54 import com.datastax.driver.mapping.Result;
55 import com.datastax.driver.mapping.UDTMapper;
56 import com.datastax.driver.mapping.annotations.Accessor;
57 import com.datastax.driver.mapping.annotations.Query;
58 import org.openecomp.core.dao.impl.CassandraBaseDao;
59 import org.openecomp.sdc.logging.api.Logger;
60 import org.openecomp.sdc.logging.api.LoggerFactory;
61 import org.openecomp.core.nosqldb.api.NoSqlDb;
62 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
63 import org.openecomp.core.utilities.json.JsonUtil;
64 import org.openecomp.sdc.action.ActionConstants;
65 import org.openecomp.sdc.action.dao.ActionDao;
66 import org.openecomp.sdc.action.dao.types.ActionEntity;
67 import org.openecomp.sdc.action.dao.types.OpenEcompComponentEntity;
68 import org.openecomp.sdc.action.errors.ActionException;
69 import org.openecomp.sdc.action.logging.CategoryLogLevel;
70 import org.openecomp.sdc.action.logging.StatusCode;
71 import org.openecomp.sdc.action.types.Action;
72 import org.openecomp.sdc.action.types.ActionStatus;
73 import org.openecomp.sdc.action.types.ActionSubOperation;
74 import org.openecomp.sdc.action.types.OpenEcompComponent;
75 import org.openecomp.sdc.action.util.ActionUtil;
76 import org.openecomp.sdc.versioning.VersioningManagerFactory;
77 import org.openecomp.sdc.versioning.dao.VersionInfoDao;
78 import org.openecomp.sdc.versioning.dao.VersionInfoDaoFactory;
79 import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDao;
80 import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDaoFactory;
81 import org.openecomp.sdc.versioning.dao.types.Version;
82 import org.openecomp.sdc.versioning.dao.types.VersionInfoDeletedEntity;
83 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
84 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
85 import org.slf4j.MDC;
86
87 import java.util.ArrayList;
88 import java.util.Collection;
89 import java.util.HashSet;
90 import java.util.LinkedHashMap;
91 import java.util.List;
92 import java.util.Map;
93 import java.util.Set;
94 import java.util.stream.Collectors;
95
96
97 public class ActionDaoImpl extends CassandraBaseDao<ActionEntity> implements ActionDao {
98   private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
99   private static Mapper<ActionEntity> mapper =
100       noSqlDb.getMappingManager().mapper(ActionEntity.class);
101   private static ActionAccessor accessor =
102       noSqlDb.getMappingManager().createAccessor(ActionAccessor.class);
103   private static UDTMapper<Version> versionMapper =
104       noSqlDb.getMappingManager().udtMapper(Version.class);
105   private static VersionInfoDao versionInfoDao =
106       VersionInfoDaoFactory.getInstance().createInterface();
107   private static VersionInfoDeletedDao versionInfoDeletedDao =
108       VersionInfoDeletedDaoFactory.getInstance().createInterface();
109
110   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
111
112   @Override
113   public void registerVersioning(String versionableEntityType) {
114     VersioningManagerFactory.getInstance().createInterface()
115         .register(versionableEntityType, new VersionableEntityMetadata(
116             mapper.getTableMetadata().getName(),
117             mapper.getTableMetadata().getPartitionKey().get(0).getName(),
118             mapper.getTableMetadata().getPartitionKey().get(1).getName()));
119   }
120
121   @Override
122   public Action createAction(Action action) {
123     try {
124       ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ENTITY, TARGET_ENTITY_DB);
125       this.create(action.toEntity());
126       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
127       log.metrics("");
128       return action;
129     } catch (NoHostAvailableException exception) {
130       logGenericException(exception);
131       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
132           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
133     }
134   }
135
136   @Override
137   public Action updateAction(Action action) {
138     try {
139       log.debug(" entering updateAction with actionUUID= " + action.getActionUuId());
140       ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION, TARGET_ENTITY_DB);
141       this.update(action.toEntity());
142       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
143       log.metrics("");
144       log.debug(" exit updateAction with actionUUID= " + action.getActionUuId());
145       return action;
146     } catch (NoHostAvailableException exception) {
147       logGenericException(exception);
148       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
149           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
150     }
151   }
152
153   @Override
154   public void deleteAction(String actionInvariantUuId) {
155     try {
156       log.debug("entering deleteAction with actionInvariantUuId = " + actionInvariantUuId);
157       ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
158       VersionInfoDeletedEntity activeVersionEntity = versionInfoDeletedDao.get(
159           new VersionInfoDeletedEntity(ActionConstants.ACTION_VERSIONABLE_TYPE,
160               actionInvariantUuId));
161       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
162       log.metrics("");
163
164       Version activeVersion = activeVersionEntity.getActiveVersion();
165       Statement getNameFromInvUuId = QueryBuilder.select().column("name").from("dox", "Action")
166           .where(eq("actioninvariantuuid", actionInvariantUuId))
167           .and(in("version", versionMapper.toUDT(activeVersion)));
168       ActionUtil
169           .actionLogPreProcessor(ActionSubOperation.GET_NAME_BY_ACTIONINVID, TARGET_ENTITY_DB);
170       ResultSet results = getSession().execute(getNameFromInvUuId);
171       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
172       log.metrics("");
173
174       if (!results.isExhausted()) {
175         String name = results.one().getString("name");
176         List<Version> versions = getVersionsByName(name);
177         updateActionStatusForDelete(actionInvariantUuId, versions);
178       }
179     } catch (NoHostAvailableException noHostAvailableException) {
180       logGenericException(noHostAvailableException);
181       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
182           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
183     }
184     log.debug("exit deleteAction");
185   }
186
187   @Override
188   public List<Action> getFilteredActions(String filterType, String filterId) {
189     List<Action> actions = new ArrayList<>();
190     Result<ActionEntity> result = null;
191     log.debug(
192         " entering getFilteredActions By filterType = " + filterType + " With value = " + filterId);
193     try {
194       switch (filterType) {
195         case FILTER_TYPE_VENDOR:
196           ActionUtil
197               .actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_VENDOR,
198                   TARGET_ENTITY_DB);
199           result = accessor.getActionsByVendor(filterId);
200           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
201           log.metrics("");
202           break;
203         case FILTER_TYPE_CATEGORY:
204           ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_CATEGORY,
205               TARGET_ENTITY_DB);
206           result = accessor.getActionsByCategory(filterId);
207           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
208           log.metrics("");
209           break;
210         case FILTER_TYPE_MODEL:
211           ActionUtil
212               .actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_MODEL,
213                   TARGET_ENTITY_DB);
214           result = accessor.getActionsByModel(filterId);
215           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
216           log.metrics("");
217           break;
218         case FILTER_TYPE_OPEN_ECOMP_COMPONENT:
219           ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_COMPONENT,
220               TARGET_ENTITY_DB);
221           result = accessor.getActionsByOpenEcompComponent(filterId);
222           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
223           log.metrics("");
224           break;
225         case FILTER_TYPE_NONE:
226           ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ALL_ACTIONS, TARGET_ENTITY_DB);
227           result = accessor.getAllActions();
228           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
229           log.metrics("");
230           break;
231         case FILTER_TYPE_NAME:
232           ActionUtil
233               .actionLogPreProcessor(ActionSubOperation.GET_ACTIONINVID_BY_NAME, TARGET_ENTITY_DB);
234           result = accessor.getInvIdByName(filterId);
235           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE);
236           log.metrics("");
237           List<ActionEntity> actionEntities = result.all();
238           if (actionEntities != null && !actionEntities.isEmpty()) {
239             String actionInvariantUuId = actionEntities.get(0).getActionInvariantUuId();
240             if (actionInvariantUuId != null) {
241               return getActionsByActionInvariantUuId(actionInvariantUuId);
242             } else {
243               return actions;
244             }
245           }
246           break;
247         default:
248           break;
249       }
250       if (result != null) {
251         actions.addAll(result.all().stream().map(ActionEntity::toDto).collect(Collectors.toList()));
252       }
253       log.debug(
254           " exit getFilteredActions By filterType = " + filterType + " With value = " + filterId);
255     } catch (NoHostAvailableException noHostAvailableException) {
256       logGenericException(noHostAvailableException);
257       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
258           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
259     }
260     return actions;
261   }
262
263   @Override
264   public Action getActionsByActionUuId(String actionUuId) {
265     try {
266       log.debug(" entering getActionsByActionUuId with actionUUID= " + actionUuId);
267       ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONUUID,
268           TARGET_ENTITY_DB);
269       Result<ActionEntity> result = accessor.actionInvariantUuId(actionUuId);
270       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
271       log.metrics("");
272       if (result != null) {
273         log.debug(" exit getActionsByActionUuId with actionUUID= " + actionUuId);
274         ActionEntity entity = result.one();
275         if (entity != null) {
276           return entity.toDto();
277         }
278       }
279       log.debug(" exit getActionsByActionUuId with actionUUID= " + actionUuId);
280       return null;
281     } catch (NoHostAvailableException noHostAvailableException) {
282       logGenericException(noHostAvailableException);
283       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
284           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
285     }
286   }
287
288   @Override
289   public List<OpenEcompComponent> getOpenEcompComponents() {
290     List<OpenEcompComponent> openEcompComponents = new ArrayList<>();
291     Result<OpenEcompComponentEntity> result;
292     try {
293       log.debug(" entering getOpenEcompComponents ");
294       ActionUtil
295           .actionLogPreProcessor(ActionSubOperation.GET_OPEN_ECOMP_COMPONENTS_ENTITY, TARGET_ENTITY_DB);
296       result = accessor.getOpenEcompComponents();
297       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
298       log.metrics("");
299       if (result != null) {
300         openEcompComponents.addAll(
301             result.all().stream().map(OpenEcompComponentEntity::toDto).collect(Collectors.toList()));
302       }
303     } catch (NoHostAvailableException noHostAvailableException) {
304       logGenericException(noHostAvailableException);
305       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
306           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
307     }
308     log.debug(" exit getOpenEcompComponents ");
309     return openEcompComponents;
310   }
311
312   @Override
313   public List<Action> getActionsByActionInvariantUuId(String actionInvariantUuId) {
314     List<Action> actions = new ArrayList<Action>();
315     try {
316       log.debug(" entering getActionsByActionInvariantUuId with actionInvariantUuId= "
317           + actionInvariantUuId);
318       Set<Version> viewableVersions = new HashSet<>();
319       VersionPredicate filter = new VersionPredicate();
320       ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
321       VersionInfoEntity versionInfoEntity = versionInfoDao
322           .get(new VersionInfoEntity(ActionConstants.ACTION_VERSIONABLE_TYPE, actionInvariantUuId));
323       if (versionInfoEntity == null) {
324         //Check for action in the Delete version info table
325         VersionInfoDeletedEntity versionInfoDeletedEntity = versionInfoDeletedDao.get(
326             new VersionInfoDeletedEntity(ActionConstants.ACTION_VERSIONABLE_TYPE,
327                 actionInvariantUuId));
328         if (versionInfoDeletedEntity != null) {
329           viewableVersions = versionInfoDeletedEntity.getViewableVersions();
330           //Remove intermediate minor versions from viewable versions
331           if (versionInfoDeletedEntity.getActiveVersion() != null) {
332             filter.activeVersion = versionInfoDeletedEntity.getActiveVersion();
333             filter.finalVersion = versionInfoDeletedEntity.getLatestFinalVersion();
334             viewableVersions.removeIf(filter::isIntermediateMinorVersion);
335           }
336         }
337       } else {
338         viewableVersions = versionInfoEntity.getViewableVersions();
339         //Remove intermediate minor versions from viewable versions
340         if (versionInfoEntity.getActiveVersion() != null) {
341           filter.activeVersion = versionInfoEntity.getActiveVersion();
342           filter.finalVersion = versionInfoEntity.getLatestFinalVersion();
343           viewableVersions.removeIf(filter::isIntermediateMinorVersion);
344         }
345         //Add candidate version if available
346         if (versionInfoEntity.getCandidate() != null) {
347           viewableVersions.add(versionInfoEntity.getCandidate().getVersion());
348         }
349       }
350
351       MDC.put(TARGET_ENTITY, TARGET_ENTITY_DB);
352       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
353       log.metrics("");
354
355       log.debug(
356           "Found " + viewableVersions + " viewable version for action with actionInvariantUuId "
357               + actionInvariantUuId);
358
359       //Fetch action data for the viewable versions
360       if (!viewableVersions.isEmpty()) {
361         ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID,
362             TARGET_ENTITY_DB);
363         Result<ActionEntity> result =
364             accessor.getActionsByInvId(actionInvariantUuId, viewableVersions);
365         ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
366         log.metrics("");
367         if (result != null) {
368           actions
369               .addAll(result.all().stream().map(ActionEntity::toDto).collect(Collectors.toList()));
370         }
371       }
372     } catch (NoHostAvailableException noHostAvailableException) {
373       logGenericException(noHostAvailableException);
374       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
375           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
376     }
377     log.debug(
378         " exit getActionsByActionInvariantUuId with actionInvariantUuId= " + actionInvariantUuId);
379     return actions;
380   }
381
382   @Override
383   public Action getLockedAction(String actionInvariantUuId, String user) throws ActionException {
384     log.debug(" entering getLockedAction with actionInvariantUuId= " + actionInvariantUuId);
385     ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
386     Action action = null;
387     VersionInfoEntity versionInfoEntity = versionInfoDao
388         .get(new VersionInfoEntity(ActionConstants.ACTION_VERSIONABLE_TYPE, actionInvariantUuId));
389     ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
390     log.metrics("");
391     if (versionInfoEntity != null) {
392       if (versionInfoEntity.getCandidate() != null) {
393         String actionUser = versionInfoEntity.getCandidate().getUser();
394         if (actionUser != null && actionUser.equals(user)) {
395           Set<Version> versions = new HashSet<>();
396           versions.add(versionInfoEntity.getCandidate().getVersion());
397           ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID,
398               TARGET_ENTITY_DB);
399           Result<ActionEntity> result = accessor.getActionsByInvId(actionInvariantUuId, versions);
400           ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
401           log.metrics("");
402           if (result != null) {
403             ActionEntity actionEntity = result.one();
404             action = actionEntity != null ? actionEntity.toDto() : null;
405           }
406         } else {
407           throw new ActionException(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER_CODE,
408               String.format(ACTION_ARTIFACT_DEL_LOCKED_OTHER_USER, actionUser));
409         }
410       } else {
411         throw new ActionException(ACTION_NOT_LOCKED_CODE, ACTION_NOT_LOCKED_MSG);
412       }
413     } else {
414       throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
415     }
416     return action;
417   }
418
419   private void logGenericException(Exception exception) {
420     ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
421         ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
422     log.metrics("");
423     ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
424         ACTION_QUERY_FAILURE_MSG);
425     log.error(exception.getMessage());
426   }
427
428   @Override
429   protected Mapper<ActionEntity> getMapper() {
430     return mapper;
431   }
432
433
434   @Override
435   protected Object[] getKeys(ActionEntity entity) {
436     return new Object[]{entity.getActionInvariantUuId(), versionMapper.toUDT(entity.getVersion())};
437   }
438
439   @Override
440   public Collection<ActionEntity> list(ActionEntity entity) {
441     return accessor.getAllActions().all();
442   }
443
444   /**
445    *
446    * @param actionInvariantUUID.
447    * @param versions.
448    */
449   private void updateActionStatusForDelete(String actionInvariantUuId, List<Version> versions) {
450     log.debug(
451         "entering updateActionStatusForDelete with actionInvariantUuId = " + actionInvariantUuId
452             + " for versions " + versions);
453     List<UDTValue> versionUdt = new ArrayList<>();
454     for (Version v : versions) {
455       versionUdt.add(versionMapper.toUDT(v));
456     }
457
458     ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_STATUS, TARGET_ENTITY_DB);
459     //Update the status column of action table
460     Statement updateStatusStatement =
461         QueryBuilder.update("dox", "Action").with(set("status", ActionStatus.Deleted.name()))
462             .where(eq("actioninvariantuuid", actionInvariantUuId)).and(in("version", versionUdt));
463     getSession().execute(updateStatusStatement);
464     ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
465     log.metrics("");
466     //Update the status in the data field of action table
467     updateStatusInActionData(actionInvariantUuId, versions, ActionStatus.Deleted);
468     log.debug("exit updateActionStatusForDelete with actionInvariantUuId = " + actionInvariantUuId
469         + " for versions " + versions);
470   }
471
472   /**
473    * Update status for a list of versions for a given action.
474    *
475    * @param actionInvariantUuId Invariant UUID of the action.
476    * @param versions            List of {@link Version} for which the status has to be updated.
477    * @param status              The status value.
478    */
479   private void updateStatusInActionData(String actionInvariantUuId, List<Version> versions,
480                                         ActionStatus status) {
481     log.debug("entering updateStatusInActionData for actionInvariantUuId = " + actionInvariantUuId
482         +  " and status = " + status + " for versions " + versions);
483     for (Version v : versions) {
484       ActionEntity entity = this.get(new ActionEntity(actionInvariantUuId, v));
485       String currentData = entity.getData();
486       Map<String, Object> currentDataMap = JsonUtil.json2Object(currentData, LinkedHashMap.class);
487       currentDataMap.put(STATUS, status);
488       String updatedActionData = JsonUtil.object2Json(currentDataMap);
489       entity.setData(updatedActionData);
490       this.updateAction(entity.toDto());
491     }
492     log.debug("exit updateStatusInActionData");
493   }
494
495   /**
496    * Get list of all major and minor version values for a given action by action name.
497    *
498    * @param name Name of the action.
499    * @return List of {@link Version} objects for the action.
500    */
501   private List<Version> getVersionsByName(String name) {
502     log.debug("entering getVersionsByName for Action Name = " + name);
503     ActionUtil.actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
504     Statement statement =
505         QueryBuilder.select().column("version").from("dox", "Action").where(eq("name", name));
506     ResultSet results = getSession().execute(statement);
507     ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
508     log.metrics("");
509
510     List<Version> versionList = new ArrayList<>();
511     for (Row row : results) {
512       Version version = versionMapper.fromUDT((UDTValue) row.getObject("version"));
513       versionList.add(version);
514     }
515     log.debug("exit getVersionsByName for Action Name = " + name);
516     return versionList;
517   }
518
519   @Accessor
520   interface ActionAccessor {
521
522     @Query("SELECT * FROM Action")
523     Result<ActionEntity> getAllActions();
524
525     @Query("SELECT * FROM Action where actionInvariantUuId = ? and version in ? ")
526     Result<ActionEntity> getActionsByInvId(String actionInvariantUuId, Set<Version> versions);
527
528     @Query("SELECT * FROM Action where supportedModels CONTAINS ?")
529     Result<ActionEntity> getActionsByModel(String resource);
530
531     @Query("SELECT * FROM Action where supportedComponents CONTAINS ?")
532     Result<ActionEntity> getActionsByOpenEcompComponent(String resource);
533
534     @Query("SELECT * FROM Action where vendor_list CONTAINS ?")
535     Result<ActionEntity> getActionsByVendor(String vendor);
536
537     @Query("SELECT * FROM Action where category_list CONTAINS ?")
538     Result<ActionEntity> getActionsByCategory(String vendor);
539
540     @Query("SELECT actionInvariantUuId FROM Action where name = ? limit 1")
541     Result<ActionEntity> getInvIdByName(String name);
542
543     @Query("SELECT * FROM EcompComponent")
544     Result<OpenEcompComponentEntity> getOpenEcompComponents();
545
546     @Query("SELECT * FROM Action where actionUUID = ?")
547     Result<ActionEntity> actionInvariantUuId(String actionUuId);
548
549   }
550
551   class VersionPredicate {
552     Version activeVersion;
553     Version finalVersion;
554
555     public boolean isIntermediateMinorVersion(Version version) {
556       int activeMajorVersion = activeVersion.getMajor();
557       int activeMinorVersion = activeVersion.getMinor();
558       int currentMinorVersion = version.getMinor();
559       int currentMajorVersion = version.getMajor();
560
561       if (finalVersion != null) {
562         if (finalVersion.getMajor() == activeMajorVersion
563             && currentMajorVersion == finalVersion.getMajor()) {
564           if (currentMinorVersion < activeMinorVersion && currentMinorVersion != 0) {
565             return true;
566           }
567         } else {
568           return false;
569         }
570       } else {
571         if (!version.equals(activeVersion)) {
572           return true;
573         }
574       }
575
576       return false;
577     }
578
579   }
580 }