4e85ff61c3a79cf19271c279609394cf4aa25bde
[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.ComputeDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
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 ComputeDaoCassandraImpl extends CassandraBaseDao<ComputeEntity> implements
41     ComputeDao {
42
43   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
44   private static final Mapper<ComputeEntity> mapper =
45       noSqlDb.getMappingManager().mapper(ComputeEntity.class);
46   private static final ComputeAccessor accessor =
47       noSqlDb.getMappingManager().createAccessor(ComputeDaoCassandraImpl.ComputeAccessor.class);
48   private static final UDTMapper<Version> versionMapper =
49       noSqlDb.getMappingManager().udtMapper(Version.class);
50
51   @Override
52   public void registerVersioning(String versionableEntityType) {
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.COMPUTE_NAME,
61         Arrays.asList("vsp_id", "version", "component_id", "name"))));
62
63     ActionVersioningManagerFactory.getInstance().createInterface()
64         .register(versionableEntityType, metadata);
65   }
66
67   @Override
68   public Collection<ComputeEntity> list(ComputeEntity entity) {
69     return accessor.listByComponentId(entity.getVspId(),
70         versionMapper.toUDT(entity.getVersion()), entity.getComponentId()).all();
71   }
72
73   @Override
74   protected Mapper<ComputeEntity> getMapper() {
75     return mapper;
76   }
77
78   @Override
79   protected Object[] getKeys(ComputeEntity entity) {
80     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
81         entity.getComponentId(), entity.getId()};
82   }
83
84   @Override
85   public Collection<ComputeEntity> listByVsp(String vspId, Version version) {
86     return accessor.listByVspId(vspId, versionMapper.toUDT(version)).all();
87   }
88
89   @Override
90   public void update(ComputeEntity entity) {
91     accessor.updateCompositionData(entity.getCompositionData(), entity.getVspId(), versionMapper
92             .toUDT(entity.getVersion()), entity.getComponentId(), entity.getId());
93   }
94
95   @Override
96   public void updateQuestionnaireData(String vspId, Version version, String componentId,
97                                       String computeId, String questionnaireData) {
98     accessor.updateQuestionnaireData(questionnaireData, vspId, versionMapper.toUDT(version),
99         componentId, computeId);
100   }
101
102   @Override
103   public void deleteAll(String vspId, Version version) {
104     accessor.deleteAll(vspId, version);
105   }
106
107   @Override
108   public ComputeEntity getQuestionnaireData(String vspId, Version version, String componentId,
109                                             String computeId) {
110     return null;
111   }
112
113   @Accessor
114   interface ComputeAccessor {
115
116     @Query("select * from vsp_component_compute where vsp_id=? and version=? and component_id=?")
117     Result<ComputeEntity> listByComponentId(String vspId, UDTValue version, String componentId);
118
119     @Query("select * from vsp_component_compute where vsp_id=? and version=?")
120     Result<ComputeEntity> listByVspId(String vspId, UDTValue version);
121
122     @Query("update vsp_component_compute set composition_data=? where vsp_id=? and version=?"
123         + " and component_id=? and compute_id=?")
124     ResultSet updateCompositionData(String compositionData, String vspId, UDTValue version,
125         String componentId, String computeId);
126
127     @Query("update vsp_component_compute set questionnaire_data=? where vsp_id=? and version=?"
128             + " and component_id=? and compute_id=?")
129     ResultSet updateQuestionnaireData(String questionnaireData, String vspId, UDTValue version,
130         String componentId, String computeId);
131
132     @Query("delete from vsp_component_compute where vsp_id=? and version=?")
133     ResultSet deleteAll(String vspId, Version version);
134   }
135 }