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.types.ActionArtifact;
36 import org.openecomp.sdc.action.types.ActionSubOperation;
37 import org.openecomp.sdc.action.util.ActionUtil;
38 import org.openecomp.sdc.logging.api.Logger;
39 import org.openecomp.sdc.logging.api.LoggerFactory;
41 import java.util.Collection;
42 import java.util.List;
44 import static org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus.COMPLETE;
45 import static org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus.ERROR;
46 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY_DB;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
50 public class ActionArtifactDaoImpl extends CassandraBaseDao<ActionArtifactEntity>
51 implements ActionArtifactDao {
52 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
53 private static Mapper<ActionArtifactEntity> mapper =
54 noSqlDb.getMappingManager().mapper(ActionArtifactEntity.class);
55 private static ActionArtifactAccessor accessor =
56 noSqlDb.getMappingManager().createAccessor(ActionArtifactAccessor.class);
57 private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
60 protected Mapper<ActionArtifactEntity> getMapper() {
65 protected Object[] getKeys(ActionArtifactEntity entity) {
66 return new Object[]{entity.getArtifactUuId(), entity.getEffectiveVersion()};
70 public Collection<ActionArtifactEntity> list(ActionArtifactEntity entity) {
76 public void uploadArtifact(ActionArtifact data) {
77 log.debug(" entering uploadArtifact with artifactName= " + data.getArtifactName());
79 ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
80 this.create(data.toEntity());
81 ActionUtil.actionLogPostProcessor(COMPLETE, null, "", false);
83 } catch (NoHostAvailableException noHostAvailableException) {
84 logGenericException(noHostAvailableException);
85 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
86 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
88 log.debug(" exit uploadArtifact with artifactName= " + data.getArtifactName());
92 public ActionArtifact downloadArtifact(int effectiveVersion, String artifactUuId) {
93 log.debug(" entering downloadArtifact with artifactUUID= " + artifactUuId);
94 ActionArtifact actionArtifact = null;
97 .actionLogPreProcessor(ActionSubOperation.GET_ARTIFACT_BY_ARTIFACTUUID, TARGET_ENTITY_DB);
98 Result<ActionArtifactEntity> result = null;
99 result = accessor.getArtifactByUuId(effectiveVersion, artifactUuId);
100 ActionUtil.actionLogPostProcessor(COMPLETE, null, "", false);
102 List<ActionArtifactEntity> artifactEntities = result.all();
103 if (artifactEntities != null && !artifactEntities.isEmpty()) {
104 ActionArtifactEntity artifactEntity = artifactEntities.get(0);
105 actionArtifact = artifactEntity.toDto();
107 } catch (NoHostAvailableException noHostAvailableException) {
108 logGenericException(noHostAvailableException);
109 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
110 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
112 log.debug(" exit downloadArtifact with artifactUUID= " + artifactUuId);
113 return actionArtifact;
117 public void updateArtifact(ActionArtifact data) {
118 log.debug(" entering updateArtifact with artifactName= " + data.getArtifactName());
120 ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
121 this.update(data.toEntity());
122 ActionUtil.actionLogPostProcessor(COMPLETE, null, "", false);
124 } catch (NoHostAvailableException noHostAvailableException) {
125 logGenericException(noHostAvailableException);
126 throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
127 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
129 log.debug(" exit updateArtifact with artifactName= " + data.getArtifactName());
132 private void logGenericException(Exception exception) {
133 ActionUtil.actionLogPostProcessor(ERROR, ACTION_QUERY_FAILURE_CODE,
134 ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
136 ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
137 ACTION_QUERY_FAILURE_MSG);
138 log.error(exception.getMessage());
142 interface ActionArtifactAccessor {
145 "SELECT * FROM action_artifact WHERE effective_version <= ? and artifactuuid = ? limit 1")
146 Result<ActionArtifactEntity> getArtifactByUuId(int effectiveVersion, String artifactUuId);