[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-model-lib / openecomp-sdc-model-impl / src / main / java / org / openecomp / sdc / model / impl / EnrichedServiceArtifactDaoCassandraImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.model.impl;
22
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;
38
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.stream.Collectors;
42
43 public class EnrichedServiceArtifactDaoCassandraImpl implements EnrichedServiceArtifactDao {
44
45   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
46   private static final Mapper<EnrichedServiceArtifactEntity> mapper =
47       noSqlDb.getMappingManager().mapper(
48           EnrichedServiceArtifactEntity.class);
49   private static final VspServiceArtifactAccessor accessor =
50       noSqlDb.getMappingManager().createAccessor(
51           VspServiceArtifactAccessor.class);
52   private static final UDTMapper<Version> versionMapper =
53       noSqlDb.getMappingManager().udtMapper(Version.class);
54
55   @Override
56   public void registerVersioning(String versionableEntityType) {
57     VersioningManagerFactory.getInstance().createInterface().register(versionableEntityType,
58         new VersionableEntityMetadata(mapper.getTableMetadata().getName(),
59             mapper.getTableMetadata().getPartitionKey().get(0).getName(),
60             mapper.getTableMetadata().getPartitionKey().get(1).getName()));
61   }
62
63   @Override
64   public Collection<ServiceArtifact> list(String vspId, Version version) {
65     List<EnrichedServiceArtifactEntity> entityList;
66     if (vspId != null && version != null) {
67       entityList = accessor.list(vspId, versionMapper.toUDT(version)).all();
68     } else {
69       entityList = accessor.listAll().all();
70     }
71
72     return entityList.stream().map(entity -> entity.getServiceArtifact())
73         .collect(Collectors.toList());
74   }
75
76   @Override
77   public void create(ServiceArtifact entity) {
78     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
79         new EnrichedServiceArtifactEntity(entity);
80     mapper.save(vspEnrichedServiceArtifactEntity);
81   }
82
83   @Override
84   public void update(ServiceArtifact entity) {
85     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
86         new EnrichedServiceArtifactEntity(entity);
87     mapper.save(vspEnrichedServiceArtifactEntity);
88   }
89
90   @Override
91   public ServiceArtifact get(String vspId, Version version) {
92     return mapper.get(getKeys(vspId, version)).getServiceArtifact();
93   }
94
95   @Override
96   public void delete(String vspId, Version version) {
97     accessor.delete(vspId, versionMapper.toUDT(version));
98   }
99
100   @Override
101   public Object[] getKeys(String vspId, Version version) {
102     return new Object[]{vspId, versionMapper.toUDT(version)};
103   }
104
105   @Override
106   public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
107     EnrichedServiceArtifactEntity enrichedServiceArtifactEntity = accessor.getArtifactInfo(vspId,
108         versionMapper.toUDT(version), name).one();
109     if (enrichedServiceArtifactEntity == null) {
110       return null;
111     }
112
113     return enrichedServiceArtifactEntity.getServiceArtifact();
114   }
115
116   @Override
117   public void deleteAll(String vspId, Version version) {
118     accessor.deleteAll(vspId, versionMapper.toUDT(version));
119   }
120
121   @Accessor
122   interface VspServiceArtifactAccessor {
123
124     @Query("SELECT vsp_id, version, name ,content_data FROM vsp_enriched_service_artifact")
125     Result<EnrichedServiceArtifactEntity> listAll();
126
127     @Query(
128         "SELECT vsp_id, version, name ,content_data FROM "
129             + "vsp_enriched_service_artifact where vsp_id=? and version=? ")
130     Result<EnrichedServiceArtifactEntity> list(String vspId, UDTValue version);
131
132     @Query(
133         "SELECT vsp_id,version,name,content_data FROM "
134             + "vsp_enriched_service_artifact where vsp_id=? and version=? and name=?")
135     Result<EnrichedServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version,
136                                                           String name);
137
138     @Query("DELETE from vsp_enriched_service_artifact where vsp_id=? and version=?")
139     ResultSet delete(String vspId, UDTValue version);
140
141     @Query("DELETE FROM vsp_enriched_service_artifact where vsp_id=? and version=?")
142     ResultSet deleteAll(String vspId, UDTValue version);
143   }
144
145 }