441f8fbbf957119582e42b706eeb2be69c745b23
[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.vendorsoftwareproduct.dao.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.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import org.openecomp.core.dao.impl.CassandraBaseDao;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
35 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
38 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
39
40 public class ImageDaoImpl extends CassandraBaseDao<ImageEntity> implements ImageDao {
41
42   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
43   private static final Mapper<ImageEntity> mapper =
44       noSqlDb.getMappingManager().mapper(ImageEntity.class);
45   private static final ImageAccessor accessor =
46       noSqlDb.getMappingManager().createAccessor(ImageAccessor.class);
47   private static final UDTMapper<Version> versionMapper =
48       noSqlDb.getMappingManager().udtMapper(Version.class);
49
50   @Override
51   public void registerVersioning(String versionableEntityType) {
52
53     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
54         mapper.getTableMetadata().getName(),
55         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
56         mapper.getTableMetadata().getPartitionKey().get(1).getName());
57
58
59     metadata.setUniqueValuesMetadata(Collections.singletonList(new UniqueValueMetadata(
60         VendorSoftwareProductConstants.UniqueValues.IMAGE_NAME,
61         Arrays.asList("vsp_id", "version", "component_id", "name"))));
62
63     ActionVersioningManagerFactory.getInstance().createInterface()
64         .register(versionableEntityType, metadata);
65   }
66
67   @Override
68   protected Mapper<ImageEntity> getMapper() {
69     return mapper;
70   }
71
72   @Override
73   protected Object[] getKeys(ImageEntity entity) {
74     return new Object[]{entity.getVspId(),
75         versionMapper.toUDT(entity.getVersion()), entity.getComponentId(), entity.getId() };
76   }
77
78   @Override
79   public Collection<ImageEntity> list(ImageEntity entity) {
80     return accessor.list(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
81         entity.getComponentId()).all();
82   }
83
84   @Override
85   public void update(ImageEntity entity) {
86     accessor.updateCompositionData(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
87         entity.getComponentId(), entity.getId(), entity.getCompositionData());
88   }
89
90   @Override
91   public void updateQuestionnaireData(String vspId, Version version, String componentId,
92                                       String imageId, String questionnaireData) {
93     accessor.updateQuestionnaireData(questionnaireData, vspId, versionMapper.toUDT(version),
94         componentId, imageId);
95   }
96
97   @Override
98   public void delete(ImageEntity entity) {
99     super.delete(entity);
100   }
101
102   @Override
103   public void deleteByVspId(String vspId, Version version) {
104     accessor.deleteByVspId(vspId, versionMapper.toUDT(version));
105   }
106
107   @Override
108   public Collection<ImageEntity> listByVsp(String vspId, Version version) {
109     return accessor.listByVspId(vspId, versionMapper.toUDT(version)).all();
110   }
111
112   @Override
113   public ImageEntity getQuestionnaireData(String vspId, Version version, String componentId,
114                                           String computeId) {
115     return null;
116   }
117
118   @Accessor
119   interface ImageAccessor {
120
121     @Query("select vsp_id, version, component_id, image_id, composition_data from "
122         + "vsp_component_image where vsp_id=? and version=? and component_id=?")
123     Result<ImageEntity> list(String vspId, UDTValue version, String componentId);
124
125     @Query(
126         "insert into vsp_component_image (vsp_id, version, component_id, image_id, "
127             + "composition_data) values (?,?,?,?,?)")
128     ResultSet updateCompositionData(String vspId, UDTValue version, String componentId, String id,
129         String compositionData);
130
131     @Query("update vsp_component_image set questionnaire_data=? where vsp_id=? and version=?"
132         + " and component_id=? and image_id=?")
133     ResultSet updateQuestionnaireData(String questionnaireData, String vspId, UDTValue version,
134         String componentId, String computeId);
135
136     @Query("delete from vsp_component_image where vsp_id=? and version=?")
137     ResultSet deleteByVspId(String vspId, UDTValue version);
138
139     @Query("select * from vsp_component_image where vsp_id=? and version=?")
140     Result<ImageEntity> listByVspId(String vspId, UDTValue version);
141
142   }
143
144 }