2a19082005ca965ac4c4485d8084ee3b328f27d9
[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.DeploymentFlavorDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
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
41 public class DeploymentFlavorDaoCassandraImpl extends CassandraBaseDao<DeploymentFlavorEntity>
42     implements DeploymentFlavorDao {
43   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
44   private static final Mapper<DeploymentFlavorEntity> mapper =
45       noSqlDb.getMappingManager().mapper(DeploymentFlavorEntity.class);
46   private static final DeploymentFlavorAccessor accessor =
47       noSqlDb.getMappingManager().createAccessor(DeploymentFlavorAccessor.class);
48   private static final UDTMapper<Version> versionMapper =
49       noSqlDb.getMappingManager().udtMapper(Version.class);
50
51   @Override
52   protected Mapper<DeploymentFlavorEntity> getMapper() {
53     return mapper;
54   }
55
56   @Override
57   protected Object[] getKeys(DeploymentFlavorEntity entity) {
58     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
59         entity.getId()};
60   }
61
62   @Override
63   public Collection<DeploymentFlavorEntity> list(DeploymentFlavorEntity entity) {
64     return accessor.list(entity.getVspId(), versionMapper.toUDT(entity.getVersion())).all();
65   }
66
67   @Override
68   public void update(DeploymentFlavorEntity entity) {
69     accessor.updateCompositionData(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
70         entity.getId(), entity.getCompositionData());
71   }
72
73   @Override
74   public void registerVersioning(String versionableEntityType) {
75     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
76         mapper.getTableMetadata().getName(),
77         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
78         mapper.getTableMetadata().getPartitionKey().get(1).getName());
79
80
81     metadata.setUniqueValuesMetadata(Collections.singletonList(new UniqueValueMetadata(
82         VendorSoftwareProductConstants.UniqueValues.DEPLOYMENT_FLAVOR_NAME,
83         Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
84             mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
85
86     ActionVersioningManagerFactory.getInstance().createInterface()
87         .register(versionableEntityType, metadata);
88   }
89
90   @Override
91   public void deleteAll(String vspId, Version version) {
92     accessor.deleteAll(vspId, version);
93   }
94
95   @Accessor
96   interface DeploymentFlavorAccessor {
97     @Query(
98         "select vsp_id, version, deployment_flavor_id, composition_data from vsp_deployment_flavor where vsp_id=?"
99             + " and version=?")
100     Result<DeploymentFlavorEntity> list(String vspId, UDTValue version);
101
102     @Query(
103         "insert into vsp_deployment_flavor (vsp_id, version, deployment_flavor_id, composition_data) values (?,?,?,?)")
104     ResultSet updateCompositionData(String vspId, UDTValue version, String id,
105         String compositionData);
106
107     @Query("delete from vsp_deployment_flavor where vsp_id=? and version=?")
108     ResultSet deleteAll(String vspId, Version version);
109
110   }
111 }