re base code
[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.*;
47
48
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());
57
58   @Override
59   protected Mapper<ActionArtifactEntity> getMapper() {
60     return mapper;
61   }
62
63   @Override
64   protected Object[] getKeys(ActionArtifactEntity entity) {
65     return new Object[]{entity.getArtifactUuId(), entity.getEffectiveVersion()};
66   }
67
68   @Override
69   public Collection<ActionArtifactEntity> list(ActionArtifactEntity entity) {
70     return null;
71   }
72
73
74   @Override
75   public void uploadArtifact(ActionArtifact data) {
76     log.debug(" entering uploadArtifact with artifactName= " + data.getArtifactName());
77     try {
78       ActionUtil.actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
79       this.create(data.toEntity());
80       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
81       log.metrics("");
82     } catch (NoHostAvailableException noHostAvailableException) {
83       logGenericException(noHostAvailableException);
84       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
85           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
86     }
87     log.debug(" exit uploadArtifact with artifactName= " + data.getArtifactName());
88   }
89
90   @Override
91   public ActionArtifact downloadArtifact(int effectiveVersion, String artifactUuId) {
92     log.debug(" entering downloadArtifact with artifactUUID= " + artifactUuId);
93     ActionArtifact actionArtifact = null;
94     try {
95       ActionUtil
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);
100       log.metrics("");
101       List<ActionArtifactEntity> artifactEntities = result.all();
102       if (artifactEntities != null && !artifactEntities.isEmpty()) {
103         ActionArtifactEntity artifactEntity = artifactEntities.get(0);
104         actionArtifact = artifactEntity.toDto();
105       }
106     } catch (NoHostAvailableException noHostAvailableException) {
107       logGenericException(noHostAvailableException);
108       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
109           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
110     }
111     log.debug(" exit downloadArtifact with artifactUUID= " + artifactUuId);
112     return actionArtifact;
113   }
114
115   @Override
116   public void updateArtifact(ActionArtifact data) {
117     log.debug(" entering updateArtifact with artifactName= " + data.getArtifactName());
118     try {
119       ActionUtil.actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
120       this.update(data.toEntity());
121       ActionUtil.actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
122       log.metrics("");
123     } catch (NoHostAvailableException noHostAvailableException) {
124       logGenericException(noHostAvailableException);
125       throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE,
126           ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG);
127     }
128     log.debug(" exit updateArtifact with artifactName= " + data.getArtifactName());
129   }
130
131   private void logGenericException(Exception exception) {
132     ActionUtil.actionLogPostProcessor(StatusCode.ERROR, ACTION_QUERY_FAILURE_CODE,
133         ACTION_ENTITY_INTERNAL_SERVER_ERROR_MSG, false);
134     log.metrics("");
135     ActionUtil.actionErrorLogProcessor(CategoryLogLevel.FATAL, ACTION_QUERY_FAILURE_CODE,
136         ACTION_QUERY_FAILURE_MSG);
137     log.error(exception.getMessage());
138   }
139
140   @Accessor
141   interface ActionArtifactAccessor {
142
143     @Query(
144         "SELECT * FROM action_artifact WHERE effective_version <= ? and artifactuuid = ? limit 1")
145     Result<ActionArtifactEntity> getArtifactByUuId(int effectiveVersion, String artifactUuId);
146   }
147 }