2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.model.impl;
19 import com.datastax.driver.core.ResultSet;
20 import com.datastax.driver.core.UDTValue;
21 import com.datastax.driver.mapping.Mapper;
22 import com.datastax.driver.mapping.Result;
23 import com.datastax.driver.mapping.UDTMapper;
24 import com.datastax.driver.mapping.annotations.Accessor;
25 import com.datastax.driver.mapping.annotations.Query;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.openecomp.core.model.dao.ServiceArtifactDao;
30 import org.openecomp.core.model.types.ServiceArtifact;
31 import org.openecomp.core.model.types.ServiceArtifactEntity;
32 import org.openecomp.core.nosqldb.api.NoSqlDb;
33 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
34 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
38 public class ServiceArtifactDaoCassandraImpl implements ServiceArtifactDao {
40 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
41 private static final Mapper<ServiceArtifactEntity> mapper = noSqlDb.getMappingManager().mapper(
42 ServiceArtifactEntity.class);
43 private static final VspServiceArtifactAccessor accessor =
44 noSqlDb.getMappingManager().createAccessor(
45 VspServiceArtifactAccessor.class);
46 private static final UDTMapper<Version> versionMapper =
47 noSqlDb.getMappingManager().udtMapper(Version.class);
50 public void registerVersioning(String versionableEntityType) {
51 ActionVersioningManagerFactory.getInstance().createInterface().register(versionableEntityType,
52 new VersionableEntityMetadata(mapper.getTableMetadata().getName(),
53 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
54 mapper.getTableMetadata().getPartitionKey().get(1).getName()));
58 public Collection<ServiceArtifact> list(String vspId, Version version) {
59 List<ServiceArtifactEntity> entityList;
60 if (vspId != null && version != null) {
61 entityList = accessor.list(vspId, versionMapper.toUDT(version)).all();
63 entityList = accessor.listAll().all();
66 return entityList.stream().map(entity -> entity.getServiceArtifact())
67 .collect(Collectors.toList());
71 public void create(ServiceArtifact entity) {
72 ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
73 mapper.save(vspServiceArtifactEntity);
77 public void update(ServiceArtifact entity) {
78 ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
79 mapper.save(vspServiceArtifactEntity);
83 public ServiceArtifact get(String vspId, Version version) {
84 return mapper.get(getKeys(vspId, version)).getServiceArtifact();
88 public void delete(String vspId, Version version) {
89 accessor.delete(vspId, versionMapper.toUDT(version));
93 // public void deleteArtifacts(String vspId, Version version){
94 // accessor.delete(vspId, versionMapper.toUDT(version));
98 public Object[] getKeys(String vspId, Version version) {
99 return new Object[]{vspId, versionMapper.toUDT(version)};
103 public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
104 ServiceArtifactEntity serviceArtifactEntity =
105 accessor.getArtifactInfo(vspId, versionMapper.toUDT(version),
107 if (serviceArtifactEntity == null) {
111 return serviceArtifactEntity.getServiceArtifact();
115 public void deleteAll(String vspId, Version version) {
116 accessor.deleteAll(vspId, versionMapper.toUDT(version));
120 interface VspServiceArtifactAccessor {
122 @Query("SELECT vsp_id, version, name ,content_data FROM vsp_service_artifact")
123 Result<ServiceArtifactEntity> listAll();
126 "SELECT vsp_id, version, name ,content_data "
127 + "FROM vsp_service_artifact where vsp_id=? and version=? ")
128 Result<ServiceArtifactEntity> list(String vspId, UDTValue version);
131 "SELECT vsp_id,version,name,content_data FROM"
132 + " vsp_service_artifact where vsp_id=? and version=? and name=?")
133 Result<ServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version, String name);
135 @Query("DELETE from vsp_service_artifact where vsp_id=? and version=?")
136 ResultSet delete(String vspId, UDTValue version);
138 @Query("DELETE FROM vsp_service_artifact where vsp_id=? and version=?")
139 ResultSet deleteAll(String vspId, UDTValue version);