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.EnrichedServiceArtifactDao;
30 import org.openecomp.core.model.types.EnrichedServiceArtifactEntity;
31 import org.openecomp.core.model.types.ServiceArtifact;
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 EnrichedServiceArtifactDaoCassandraImpl implements EnrichedServiceArtifactDao {
40 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
41 private static final Mapper<EnrichedServiceArtifactEntity> mapper =
42 noSqlDb.getMappingManager().mapper(
43 EnrichedServiceArtifactEntity.class);
44 private static final VspServiceArtifactAccessor accessor =
45 noSqlDb.getMappingManager().createAccessor(
46 VspServiceArtifactAccessor.class);
47 private static final UDTMapper<Version> versionMapper =
48 noSqlDb.getMappingManager().udtMapper(Version.class);
51 public void registerVersioning(String versionableEntityType) {
52 ActionVersioningManagerFactory.getInstance().createInterface().register(versionableEntityType,
53 new VersionableEntityMetadata(mapper.getTableMetadata().getName(),
54 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
55 mapper.getTableMetadata().getPartitionKey().get(1).getName()));
59 public Collection<ServiceArtifact> list(String vspId, Version version) {
60 List<EnrichedServiceArtifactEntity> entityList;
61 if (vspId != null && version != null) {
62 entityList = accessor.list(vspId, versionMapper.toUDT(version)).all();
64 entityList = accessor.listAll().all();
67 return entityList.stream().map(entity -> entity.getServiceArtifact())
68 .collect(Collectors.toList());
72 public void create(ServiceArtifact entity) {
73 EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
74 new EnrichedServiceArtifactEntity(entity);
75 mapper.save(vspEnrichedServiceArtifactEntity);
79 public void update(ServiceArtifact entity) {
80 EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
81 new EnrichedServiceArtifactEntity(entity);
82 mapper.save(vspEnrichedServiceArtifactEntity);
86 public ServiceArtifact get(String vspId, Version version) {
87 return mapper.get(getKeys(vspId, version)).getServiceArtifact();
91 public void delete(String vspId, Version version) {
92 accessor.delete(vspId, versionMapper.toUDT(version));
96 public Object[] getKeys(String vspId, Version version) {
97 return new Object[]{vspId, versionMapper.toUDT(version)};
101 public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
102 EnrichedServiceArtifactEntity enrichedServiceArtifactEntity = accessor.getArtifactInfo(vspId,
103 versionMapper.toUDT(version), name).one();
104 if (enrichedServiceArtifactEntity == null) {
108 return enrichedServiceArtifactEntity.getServiceArtifact();
112 public void deleteAll(String vspId, Version version) {
113 accessor.deleteAll(vspId, versionMapper.toUDT(version));
117 interface VspServiceArtifactAccessor {
119 @Query("SELECT vsp_id, version, name ,content_data FROM vsp_enriched_service_artifact")
120 Result<EnrichedServiceArtifactEntity> listAll();
123 "SELECT vsp_id, version, name ,content_data FROM "
124 + "vsp_enriched_service_artifact where vsp_id=? and version=? ")
125 Result<EnrichedServiceArtifactEntity> list(String vspId, UDTValue version);
128 "SELECT vsp_id,version,name,content_data FROM "
129 + "vsp_enriched_service_artifact where vsp_id=? and version=? and name=?")
130 Result<EnrichedServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version,
133 @Query("DELETE from vsp_enriched_service_artifact where vsp_id=? and version=?")
134 ResultSet delete(String vspId, UDTValue version);
136 @Query("DELETE FROM vsp_enriched_service_artifact where vsp_id=? and version=?")
137 ResultSet deleteAll(String vspId, UDTValue version);