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