2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.model.impl;
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.UDTValue;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.Result;
27 import com.datastax.driver.mapping.UDTMapper;
28 import com.datastax.driver.mapping.annotations.Accessor;
29 import com.datastax.driver.mapping.annotations.Query;
30 import org.openecomp.core.model.dao.EnrichedServiceArtifactDao;
31 import org.openecomp.core.model.types.EnrichedServiceArtifactEntity;
32 import org.openecomp.core.model.types.ServiceArtifact;
33 import org.openecomp.core.nosqldb.api.NoSqlDb;
34 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
35 import org.openecomp.sdc.versioning.VersioningManagerFactory;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.stream.Collectors;
43 public class EnrichedServiceArtifactDaoCassandraImpl implements EnrichedServiceArtifactDao {
45 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
46 private static final Mapper<EnrichedServiceArtifactEntity> mapper =
47 noSqlDb.getMappingManager().mapper(EnrichedServiceArtifactEntity.class);
48 private static final VspServiceArtifactAccessor accessor =
49 noSqlDb.getMappingManager().createAccessor(VspServiceArtifactAccessor.class);
50 private static final UDTMapper<Version> versionMapper =
51 noSqlDb.getMappingManager().udtMapper(Version.class);
54 public void registerVersioning(String versionableEntityType) {
55 VersioningManagerFactory.getInstance().createInterface()
56 .register(versionableEntityType, new VersionableEntityMetadata(
57 mapper.getTableMetadata().getName(),
58 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
59 mapper.getTableMetadata().getPartitionKey().get(1).getName()));
63 public Collection<ServiceArtifact> list(String vspId, Version version) {
64 List<EnrichedServiceArtifactEntity> entityList;
65 if (vspId != null && version != null) {
66 entityList = accessor.list(vspId, versionMapper.toUDT(version)).all();
68 entityList = accessor.listAll().all();
71 return entityList.stream().map(entity -> entity.getServiceArtifact())
72 .collect(Collectors.toList());
76 public void create(ServiceArtifact entity) {
77 EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
78 new EnrichedServiceArtifactEntity(entity);
79 mapper.save(vspEnrichedServiceArtifactEntity);
83 public void update(ServiceArtifact entity) {
84 EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
85 new EnrichedServiceArtifactEntity(entity);
86 mapper.save(vspEnrichedServiceArtifactEntity);
90 public ServiceArtifact get(String vspId, Version version) {
91 return mapper.get(getKeys(vspId, version)).getServiceArtifact();
95 public void delete(String vspId, Version version) {
96 accessor.delete(vspId, versionMapper.toUDT(version));
100 public Object[] getKeys(String vspId, Version version) {
101 return new Object[]{vspId, versionMapper.toUDT(version)};
105 public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
106 EnrichedServiceArtifactEntity enrichedServiceArtifactEntity =
107 accessor.getArtifactInfo(vspId, versionMapper.toUDT(version), name).one();
108 if (enrichedServiceArtifactEntity == null) {
112 return enrichedServiceArtifactEntity.getServiceArtifact();
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 vsp_enriched_service_artifact "
124 + "where vsp_id=? and version=? ")
125 Result<EnrichedServiceArtifactEntity> list(String vspId, UDTValue version);
129 "SELECT vsp_id,version,name,content_data FROM vsp_enriched_service_artifact "
130 + "where vsp_id=? and version=? and name=?")
131 Result<EnrichedServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version,
134 @Query("DELETE from vsp_enriched_service_artifact where vsp_id=? and version=?")
135 ResultSet delete(String vspId, UDTValue version);