push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / dao / impl / UploadDataDaoImpl.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.vendorsoftwareproduct.dao.impl;
22
23 import com.datastax.driver.core.UDTValue;
24 import com.datastax.driver.mapping.Mapper;
25 import com.datastax.driver.mapping.Result;
26 import com.datastax.driver.mapping.UDTMapper;
27 import com.datastax.driver.mapping.annotations.Accessor;
28 import com.datastax.driver.mapping.annotations.Query;
29 import org.openecomp.core.dao.impl.CassandraBaseDao;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.UploadDataDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37 import java.nio.ByteBuffer;
38 import java.util.Collection;
39
40 public class UploadDataDaoImpl extends CassandraBaseDao<UploadDataEntity> implements UploadDataDao {
41
42   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
43   private static final Mapper<UploadDataEntity> mapper =
44       noSqlDb.getMappingManager().mapper(UploadDataEntity.class);
45   private static final UploadDataAccessor accessor =
46       noSqlDb.getMappingManager().createAccessor(UploadDataAccessor.class);
47   private static final UDTMapper<Version> versionMapper =
48       noSqlDb.getMappingManager().udtMapper(Version.class);
49
50   @Override
51   protected Mapper<UploadDataEntity> getMapper() {
52     return mapper;
53   }
54
55   @Override
56   protected Object[] getKeys(UploadDataEntity entity) {
57     return new Object[]{entity.getId(), versionMapper.toUDT(entity.getVersion())};
58   }
59
60   @Override
61   public Collection<UploadDataEntity> list(UploadDataEntity entity) {
62     return accessor.listAll().all();
63   }
64
65   @Override
66   public void deleteContentDataAndValidationData(String vspId, Version version) {
67     accessor.deleteContentDataAndValidationData(vspId, versionMapper.toUDT(version));
68   }
69
70   @Override
71   public ByteBuffer getContentData(String vspId, Version version) {
72     return accessor.getContentData(vspId, version).one().getContentData();
73   }
74
75
76   @Accessor
77   interface UploadDataAccessor {
78
79     @Query(
80         "SELECT package_name, package_version, content_data, validation_data FROM vsp_information")
81     Result<UploadDataEntity> listAll();
82
83     @Query(
84         "DELETE package_name, package_version, content_data, validation_data FROM vsp_information "
85             + "WHERE vsp_id=? and version=?")
86     Result<VspDetails> deleteContentDataAndValidationData(String vspId, UDTValue udtValue);
87
88     @Query("SELECT CONTENT_DATA FROM vsp_information WHERE vsp_id=? and version=?")
89     Result<UploadDataEntity> getContentData(String vspId, Version version);
90
91   }
92 }