4962ae37a22357536aac5037ba7d83b7be9c0d34
[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.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;
37
38 public class EnrichedServiceArtifactDaoCassandraImpl implements EnrichedServiceArtifactDao {
39
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);
49
50   @Override
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()));
56   }
57
58   @Override
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();
63     } else {
64       entityList = accessor.listAll().all();
65     }
66
67     return entityList.stream().map(entity -> entity.getServiceArtifact())
68         .collect(Collectors.toList());
69   }
70
71   @Override
72   public void create(ServiceArtifact entity) {
73     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
74         new EnrichedServiceArtifactEntity(entity);
75     mapper.save(vspEnrichedServiceArtifactEntity);
76   }
77
78   @Override
79   public void update(ServiceArtifact entity) {
80     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
81         new EnrichedServiceArtifactEntity(entity);
82     mapper.save(vspEnrichedServiceArtifactEntity);
83   }
84
85   @Override
86   public ServiceArtifact get(String vspId, Version version) {
87     return mapper.get(getKeys(vspId, version)).getServiceArtifact();
88   }
89
90   @Override
91   public void delete(String vspId, Version version) {
92     accessor.delete(vspId, versionMapper.toUDT(version));
93   }
94
95   @Override
96   public Object[] getKeys(String vspId, Version version) {
97     return new Object[]{vspId, versionMapper.toUDT(version)};
98   }
99
100   @Override
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) {
105       return null;
106     }
107
108     return enrichedServiceArtifactEntity.getServiceArtifact();
109   }
110
111   @Override
112   public void deleteAll(String vspId, Version version) {
113     accessor.deleteAll(vspId, versionMapper.toUDT(version));
114   }
115
116   @Accessor
117   interface VspServiceArtifactAccessor {
118
119     @Query("SELECT vsp_id, version, name ,content_data FROM vsp_enriched_service_artifact")
120     Result<EnrichedServiceArtifactEntity> listAll();
121
122     @Query(
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);
126
127     @Query(
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,
131         String name);
132
133     @Query("DELETE from vsp_enriched_service_artifact where vsp_id=? and version=?")
134     ResultSet delete(String vspId, UDTValue version);
135
136     @Query("DELETE FROM vsp_enriched_service_artifact where vsp_id=? and version=?")
137     ResultSet deleteAll(String vspId, UDTValue version);
138   }
139
140 }