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