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