2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.action.dao.impl;
23 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY_DB;
24 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG;
25 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
26 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_CODE;
27 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_MSG;
29 import com.datastax.driver.core.exceptions.NoHostAvailableException;
30 import com.datastax.driver.mapping.Mapper;
31 import com.datastax.driver.mapping.Result;
32 import com.datastax.driver.mapping.annotations.Accessor;
33 import com.datastax.driver.mapping.annotations.Query;
34 import org.openecomp.core.dao.impl.CassandraBaseDao;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import org.openecomp.core.nosqldb.api.NoSqlDb;
38 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
39 import org.openecomp.sdc.action.dao.ActionArtifactDao;
40 import org.openecomp.sdc.action.dao.types.ActionArtifactEntity;
41 import org.openecomp.sdc.action.errors.ActionException;
42 import org.openecomp.sdc.action.logging.CategoryLogLevel;
43 import org.openecomp.sdc.action.logging.StatusCode;
44 import org.openecomp.sdc.action.types.ActionArtifact;
45 import org.openecomp.sdc.action.types.ActionSubOperation;
46 import org.openecomp.sdc.action.util.ActionUtil;
48 import java.util.Collection;
49 import java.util.List;
52 public class ActionArtifactDaoImpl extends CassandraBaseDao<ActionArtifactEntity>
53 implements ActionArtifactDao {
54 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
55 private static Mapper<ActionArtifactEntity> mapper =
56 noSqlDb.getMappingManager().mapper(ActionArtifactEntity.class);
57 private static ActionArtifactAccessor accessor =
58 noSqlDb.getMappingManager().createAccessor(ActionArtifactAccessor.class);
59 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
62 protected Mapper<ActionArtifactEntity> getMapper() {
67 protected Object[] getKeys(ActionArtifactEntity entity) {
68 return new Object[]{entity.getArtifactUuId(), entity.getEffectiveVersion()};
72 public Collection<ActionArtifactEntity> list(ActionArtifactEntity entity) {
78 public void uploadArtifact(ActionArtifact data) {
79 log.debug(" entering uploadArtifact with artifactName= " + data.getArtifactName());
81 ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
82 this.create(data.toEntity());
83 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
85 } catch (NoHostAvailableException noHostAvailableException) {
86 logGenericException(noHostAvailableException);
87 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
88 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
90 log.debug(" exit uploadArtifact with artifactName= " + data.getArtifactName());
94 public ActionArtifact downloadArtifact(int effectiveVersion, String artifactUuId) {
95 log.debug(" entering downloadArtifact with artifactUUID= " + artifactUuId);
96 ActionArtifact actionArtifact = null;
99 .actionLogPreProcessor(ActionSubOperation.GET_ARTIFACT_BY_ARTIFACTUUID, TARGET_ENTITY_DB);
100 Result<ActionArtifactEntity> result = null;
101 result = accessor.getArtifactByUuId(effectiveVersion, artifactUuId);
102 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
104 List<ActionArtifactEntity> artifactEntities = result.all();
105 if (artifactEntities != null && !artifactEntities.isEmpty()) {
106 ActionArtifactEntity artifactEntity = artifactEntities.get(0);
107 actionArtifact = artifactEntity.toDto();
109 } catch (NoHostAvailableException noHostAvailableException) {
110 logGenericException(noHostAvailableException);
111 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
112 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
114 log.debug(" exit downloadArtifact with artifactUUID= " + artifactUuId);
115 return actionArtifact;
119 public void updateArtifact(ActionArtifact data) {
120 log.debug(" entering updateArtifact with artifactName= " + data.getArtifactName());
122 ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
123 this.update(data.toEntity());
124 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
126 } catch (NoHostAvailableException noHostAvailableException) {
127 logGenericException(noHostAvailableException);
128 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
129 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
131 log.debug(" exit updateArtifact with artifactName= " + data.getArtifactName());
134 private void logGenericException(Exception exception) {
135 ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
136 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
138 ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
139 ACTION_QUERY_FAILURE_MSG);
140 log.error(exception.getMessage());
144 interface ActionArtifactAccessor {
147 "SELECT * FROM action_artifact WHERE effective_version <= ? and artifactuuid = ? limit 1")
148 Result<ActionArtifactEntity> getArtifactByUuId(int effectiveVersion, String artifactUuId);