84972342f86a0cace4411cc3e5ae13f8ce354015
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.model.impl;
18
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;
37
38 public class ServiceArtifactDaoCassandraImpl implements ServiceArtifactDao {
39
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);
48
49   @Override
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()));
55   }
56
57   @Override
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();
62     } else {
63       entityList = accessor.listAll().all();
64     }
65
66     return entityList.stream().map(entity -> entity.getServiceArtifact())
67         .collect(Collectors.toList());
68   }
69
70   @Override
71   public void create(ServiceArtifact entity) {
72     ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
73     mapper.save(vspServiceArtifactEntity);
74   }
75
76   @Override
77   public void update(ServiceArtifact entity) {
78     ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
79     mapper.save(vspServiceArtifactEntity);
80   }
81
82   @Override
83   public ServiceArtifact get(String vspId, Version version) {
84     return mapper.get(getKeys(vspId, version)).getServiceArtifact();
85   }
86
87   @Override
88   public void delete(String vspId, Version version) {
89     accessor.delete(vspId, versionMapper.toUDT(version));
90   }
91
92   // @Override
93   // public void deleteArtifacts(String vspId, Version version){
94   // accessor.delete(vspId, versionMapper.toUDT(version));
95   // }
96
97   @Override
98   public Object[] getKeys(String vspId, Version version) {
99     return new Object[]{vspId, versionMapper.toUDT(version)};
100   }
101
102   @Override
103   public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
104     ServiceArtifactEntity serviceArtifactEntity =
105         accessor.getArtifactInfo(vspId, versionMapper.toUDT(version),
106             name).one();
107     if (serviceArtifactEntity == null) {
108       return null;
109     }
110
111     return serviceArtifactEntity.getServiceArtifact();
112   }
113
114   @Override
115   public void deleteAll(String vspId, Version version) {
116     accessor.deleteAll(vspId, versionMapper.toUDT(version));
117   }
118
119   @Accessor
120   interface VspServiceArtifactAccessor {
121
122     @Query("SELECT vsp_id, version, name ,content_data FROM vsp_service_artifact")
123     Result<ServiceArtifactEntity> listAll();
124
125     @Query(
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);
129
130     @Query(
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);
134
135     @Query("DELETE from vsp_service_artifact where vsp_id=? and version=?")
136     ResultSet delete(String vspId, UDTValue version);
137
138     @Query("DELETE FROM vsp_service_artifact where vsp_id=? and version=?")
139     ResultSet deleteAll(String vspId, UDTValue version);
140   }
141
142 }