Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-action-lib / openecomp-sdc-action-core / src / main / java / org / openecomp / sdc / action / dao / impl / ActionArtifactDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.action.dao.impl;
22
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;
41
42 import java.util.Collection;
43 import java.util.List;
44
45 import static org.openecomp.sdc.action.ActionConstants.TARGET_ENTITY_DB;
46 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG;
47 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_INTERNAL_SERVER_ERR_CODE;
48 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_CODE;
49 import static org.openecomp.sdc.action.errors.ActionErrorConstants.ACTION_QUERY_FAILURE_MSG;
50
51
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());
60
61   @Override
62   protected Mapper<ActionArtifactEntity> getMapper() {
63     return mapper;
64   }
65
66   @Override
67   protected Object[] getKeys(ActionArtifactEntity entity) {
68     return new Object[]{entity.getArtifactUuId(), entity.getEffectiveVersion()};
69   }
70
71   @Override
72   public Collection<ActionArtifactEntity> list(ActionArtifactEntity entity) {
73     return null;
74   }
75
76
77   @Override
78   public void uploadArtifact(ActionArtifact data) {
79     log.debug(" entering uploadArtifact with artifactName= " + data.getArtifactName());
80     try {
81       ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
82       this.create(data.toEntity());
83       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
84       log.metrics("");
85     } catch (NoHostAvailableException noHostAvailableException) {
86       logGenericException(noHostAvailableException);
87       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
88           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
89     }
90     log.debug(" exit uploadArtifact with artifactName= " + data.getArtifactName());
91   }
92
93   @Override
94   public ActionArtifact downloadArtifact(int effectiveVersion, String artifactUuId) {
95     log.debug(" entering downloadArtifact with artifactUUID= " + artifactUuId);
96     ActionArtifact actionArtifact = null;
97     try {
98       ActionUtil
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);
103       log.metrics("");
104       List<ActionArtifactEntity> artifactEntities = result.all();
105       if (artifactEntities != null && !artifactEntities.isEmpty()) {
106         ActionArtifactEntity artifactEntity = artifactEntities.get(0);
107         actionArtifact = artifactEntity.toDto();
108       }
109     } catch (NoHostAvailableException noHostAvailableException) {
110       logGenericException(noHostAvailableException);
111       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
112           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
113     }
114     log.debug(" exit downloadArtifact with artifactUUID= " + artifactUuId);
115     return actionArtifact;
116   }
117
118   @Override
119   public void updateArtifact(ActionArtifact data) {
120     log.debug(" entering updateArtifact with artifactName= " + data.getArtifactName());
121     try {
122       ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
123       this.update(data.toEntity());
124       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
125       log.metrics("");
126     } catch (NoHostAvailableException noHostAvailableException) {
127       logGenericException(noHostAvailableException);
128       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
129           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
130     }
131     log.debug(" exit updateArtifact with artifactName= " + data.getArtifactName());
132   }
133
134   private void logGenericException(Exception exception) {
135     ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
136         ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
137     log.metrics("");
138     ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
139         ACTION_QUERY_FAILURE_MSG);
140     log.error(exception.getMessage());
141   }
142
143   @Accessor
144   interface ActionArtifactAccessor {
145
146     @Query(
147         "SELECT * FROM action_artifact WHERE effective_version <= ? and artifactuuid = ? limit 1")
148     Result<ActionArtifactEntity> getArtifactByUuId(int effectiveVersion, String artifactUuId);
149   }
150 }