push addional code
[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(EnrichedServiceArtifactEntity.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);
52
53   @Override
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()));
60   }
61
62   @Override
63   public Collection<ServiceArtifact> list(String vspId, Version version) {
64     List<EnrichedServiceArtifactEntity> 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     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
78         new EnrichedServiceArtifactEntity(entity);
79     mapper.save(vspEnrichedServiceArtifactEntity);
80   }
81
82   @Override
83   public void update(ServiceArtifact entity) {
84     EnrichedServiceArtifactEntity vspEnrichedServiceArtifactEntity =
85         new EnrichedServiceArtifactEntity(entity);
86     mapper.save(vspEnrichedServiceArtifactEntity);
87   }
88
89   @Override
90   public ServiceArtifact get(String vspId, Version version) {
91     return mapper.get(getKeys(vspId, version)).getServiceArtifact();
92   }
93
94   @Override
95   public void delete(String vspId, Version version) {
96     accessor.delete(vspId, versionMapper.toUDT(version));
97   }
98
99   @Override
100   public Object[] getKeys(String vspId, Version version) {
101     return new Object[]{vspId, versionMapper.toUDT(version)};
102   }
103
104   @Override
105   public ServiceArtifact getArtifactInfo(String vspId, Version version, String name) {
106     EnrichedServiceArtifactEntity enrichedServiceArtifactEntity =
107         accessor.getArtifactInfo(vspId, versionMapper.toUDT(version), name).one();
108     if (enrichedServiceArtifactEntity == null) {
109       return null;
110     }
111
112     return enrichedServiceArtifactEntity.getServiceArtifact();
113   }
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 vsp_enriched_service_artifact "
124             + "where vsp_id=? and version=? ")
125     Result<EnrichedServiceArtifactEntity> list(String vspId, UDTValue version);
126
127
128     @Query(
129         "SELECT vsp_id,version,name,content_data FROM vsp_enriched_service_artifact "
130             + "where vsp_id=? and version=? and name=?")
131     Result<EnrichedServiceArtifactEntity> getArtifactInfo(String vspId, UDTValue version,
132                                                           String name);
133
134     @Query("DELETE from vsp_enriched_service_artifact where vsp_id=? and version=?")
135     ResultSet delete(String vspId, UDTValue version);
136   }
137 }