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