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