[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 / ServiceArtifactDaoCassandraImpl.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.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;
38
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.stream.Collectors;
42
43 public class ServiceArtifactDaoCassandraImpl implements ServiceArtifactDao {
44
45   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
46   private static final Mapper<ServiceArtifactEntity> mapper = noSqlDb.getMappingManager().mapper(
47       ServiceArtifactEntity.class);
48   private static final VspServiceArtifactAccessor accessor =
49       noSqlDb.getMappingManager().createAccessor(
50           VspServiceArtifactAccessor.class);
51   private static final UDTMapper<Version> versionMapper =
52       noSqlDb.getMappingManager().udtMapper(Version.class);
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     VersioningManagerFactory.getInstance().createInterface().register(versionableEntityType,
57         new VersionableEntityMetadata(mapper.getTableMetadata().getName(),
58             mapper.getTableMetadata().getPartitionKey().get(0).getName(),
59             mapper.getTableMetadata().getPartitionKey().get(1).getName()));
60   }
61
62   @Override
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();
67     } else {
68       entityList = accessor.listAll().all();
69     }
70
71     return entityList.stream().map(entity -> entity.getServiceArtifact())
72         .collect(Collectors.toList());
73   }
74
75   @Override
76   public void create(ServiceArtifact entity) {
77     ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
78     mapper.save(vspServiceArtifactEntity);
79   }
80
81   @Override
82   public void update(ServiceArtifact entity) {
83     ServiceArtifactEntity vspServiceArtifactEntity = new ServiceArtifactEntity(entity);
84     mapper.save(vspServiceArtifactEntity);
85   }
86
87   @Override
88   public ServiceArtifact get(String vspId, Version version) {
89     return mapper.get(getKeys(vspId, version)).getServiceArtifact();
90   }
91
92   @Override
93   public void delete(String vspId, Version version) {
94     accessor.delete(vspId, versionMapper.toUDT(version));
95   }
96
97   // @Override
98   // public void deleteArtifacts(String vspId, Version version){
99   // accessor.delete(vspId, versionMapper.toUDT(version));
100   // }
101
102   @Override
103   public Object[] getKeys(String vspId, Version version) {
104     return new Object[]{vspId, versionMapper.toUDT(version)};
105   }
106
107   @Override
108   public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
109     ServiceArtifactEntity serviceArtifactEntity =
110         accessor.getArtifactInfo(vspId, versionMapper.toUDT(version),
111             name).one();
112     if (serviceArtifactEntity == null) {
113       return null;
114     }
115
116     return serviceArtifactEntity.getServiceArtifact();
117   }
118
119   @Override
120   public void deleteAll(String vspId, Version version) {
121     accessor.deleteAll(vspId, versionMapper.toUDT(version));
122   }
123
124   @Accessor
125   interface VspServiceArtifactAccessor {
126
127     @Query("SELECT vsp_id, version, name ,content_data FROM vsp_service_artifact")
128     Result<ServiceArtifactEntity> listAll();
129
130     @Query(
131         "SELECT vsp_id, version, name ,content_data "
132             + "FROM vsp_service_artifact where vsp_id=? and version=? ")
133     Result<ServiceArtifactEntity> list(String vspId, UDTValue version);
134
135     @Query(
136         "SELECT vsp_id,version,name,content_data FROM"
137             + " vsp_service_artifact where vsp_id=? and version=? and name=?")
138     Result<ServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version, String name);
139
140     @Query("DELETE from vsp_service_artifact where vsp_id=? and version=?")
141     ResultSet delete(String vspId, UDTValue version);
142
143     @Query("DELETE FROM vsp_service_artifact where vsp_id=? and version=?")
144     ResultSet deleteAll(String vspId, UDTValue version);
145   }
146
147 }