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.ServiceArtifactDao;
31 import org.openecomp.core.model.types.ServiceArtifact;
32 import org.openecomp.core.model.types.ServiceArtifactEntity;
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 ServiceArtifactDaoCassandraImpl implements ServiceArtifactDao {
45 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
46 private static final Mapper<ServiceArtifactEntity> mapper =
47 noSqlDb.getMappingManager().mapper(ServiceArtifactEntity.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<ServiceArtifactEntity> 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 ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
78 mapper.save(vspServiceArtifactEntity);
82 public void update(ServiceArtifact entity) {
83 ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
84 mapper.save(vspServiceArtifactEntity);
88 public ServiceArtifact get(String vspId, Version version) {
89 return mapper.get(getKeys(vspId, version)).getServiceArtifact();
93 public void delete(String vspId, Version version) {
94 accessor.delete(vspId, versionMapper.toUDT(version));
98 // public void deleteArtifacts(String vspId, Version version){
99 // accessor.delete(vspId, versionMapper.toUDT(version));
103 public Object[] getKeys(String vspId, Version version) {
104 return new Object[]{vspId, versionMapper.toUDT(version)};
108 public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
109 ServiceArtifactEntity serviceArtifactEntity =
110 accessor.getArtifactInfo(vspId, versionMapper.toUDT(version), name).one();
111 if (serviceArtifactEntity == null) {
115 return serviceArtifactEntity.getServiceArtifact();
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);