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 com.datastax.driver.core.exceptions.NoHostAvailableException;
24 import com.datastax.driver.mapping.Mapper;
25 import com.datastax.driver.mapping.Result;
26 import com.datastax.driver.mapping.annotations.Accessor;
27 import com.datastax.driver.mapping.annotations.Query;
28 import org.openecomp.core.dao.impl.CassandraBaseDao;
29 import org.openecomp.core.nosqldb.api.NoSqlDb;
30 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
31 import org.openecomp.sdc.action.dao.ActionArtifactDao;
32 import org.openecomp.sdc.action.dao.types.ActionArtifactEntity;
33 import org.openecomp.sdc.action.errors.ActionException;
34 import org.openecomp.sdc.action.logging.CategoryLogLevel;
35 import org.openecomp.sdc.action.logging.StatusCode;
36 import org.openecomp.sdc.action.types.ActionArtifact;
37 import org.openecomp.sdc.action.types.ActionSubOperation;
38 import org.openecomp.sdc.action.util.ActionUtil;
39 import org.openecomp.sdc.logging.api.Logger;
40 import org.openecomp.sdc.logging.api.LoggerFactory;
42 import java.util.Collection;
43 import java.util.List;
45 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY_DB;
46 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
49 public class ActionArtifactDaoImpl extends CassandraBaseDao<ActionArtifactEntity>
50 implements ActionArtifactDao {
51 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
52 private static Mapper<ActionArtifactEntity> mapper =
53 noSqlDb.getMappingManager().mapper(ActionArtifactEntity.class);
54 private static ActionArtifactAccessor accessor =
55 noSqlDb.getMappingManager().createAccessor(ActionArtifactAccessor.class);
56 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
59 protected Mapper<ActionArtifactEntity> getMapper() {
64 protected Object[] getKeys(ActionArtifactEntity entity) {
65 return new Object[]{entity.getArtifactUuId(), entity.getEffectiveVersion()};
69 public Collection<ActionArtifactEntity> list(ActionArtifactEntity entity) {
75 public void uploadArtifact(ActionArtifact data) {
76 log.debug(" entering uploadArtifact with artifactName= " + data.getArtifactName());
78 ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
79 this.create(data.toEntity());
80 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
82 } catch (NoHostAvailableException noHostAvailableException) {
83 logGenericException(noHostAvailableException);
84 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
85 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
87 log.debug(" exit uploadArtifact with artifactName= " + data.getArtifactName());
91 public ActionArtifact downloadArtifact(int effectiveVersion, String artifactUuId) {
92 log.debug(" entering downloadArtifact with artifactUUID= " + artifactUuId);
93 ActionArtifact actionArtifact = null;
96 .actionLogPreProcessor(ActionSubOperation.GET_ARTIFACT_BY_ARTIFACTUUID, TARGET_ENTITY_DB);
97 Result<ActionArtifactEntity> result = null;
98 result = accessor.getArtifactByUuId(effectiveVersion, artifactUuId);
99 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
101 List<ActionArtifactEntity> artifactEntities = result.all();
102 if (artifactEntities != null && !artifactEntities.isEmpty()) {
103 ActionArtifactEntity artifactEntity = artifactEntities.get(0);
104 actionArtifact = artifactEntity.toDto();
106 } catch (NoHostAvailableException noHostAvailableException) {
107 logGenericException(noHostAvailableException);
108 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
109 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
111 log.debug(" exit downloadArtifact with artifactUUID= " + artifactUuId);
112 return actionArtifact;
116 public void updateArtifact(ActionArtifact data) {
117 log.debug(" entering updateArtifact with artifactName= " + data.getArtifactName());
119 ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
120 this.update(data.toEntity());
121 ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
123 } catch (NoHostAvailableException noHostAvailableException) {
124 logGenericException(noHostAvailableException);
125 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
126 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
128 log.debug(" exit updateArtifact with artifactName= " + data.getArtifactName());
131 private void logGenericException(Exception exception) {
132 ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
133 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
135 ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
136 ACTION_QUERY_FAILURE_MSG);
137 log.error(exception.getMessage());
141 interface ActionArtifactAccessor {
144 "SELECT * FROM action_artifact WHERE effective_version <= ? and artifactuuid = ? limit 1")
145 Result<ActionArtifactEntity> getArtifactByUuId(int effectiveVersion, String artifactUuId);