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