push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / dao / impl / ProcessDaoCassandraImpl.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.vendorsoftwareproduct.dao.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.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.ProcessDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
35 import org.openecomp.sdc.versioning.VersioningManagerFactory;
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 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.Collections;
43
44 public class ProcessDaoCassandraImpl implements ProcessDao {
45
46   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
47   private static final Mapper<ProcessEntity> mapper =
48       noSqlDb.getMappingManager().mapper(ProcessEntity.class);
49   private static final ProcessAccessor accessor =
50       noSqlDb.getMappingManager().createAccessor(ProcessAccessor.class);
51   private static final UDTMapper<Version> versionMapper =
52       noSqlDb.getMappingManager().udtMapper(Version.class);
53
54   @Override
55   public void registerVersioning(String versionableEntityType) {
56     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
57         mapper.getTableMetadata().getName(),
58         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
59         mapper.getTableMetadata().getPartitionKey().get(1).getName());
60
61     metadata.setUniqueValuesMetadata(Collections.singletonList(
62         new UniqueValueMetadata(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
63             Arrays.asList("vsp_id", "version", "component_id", "name"))));
64
65     VersioningManagerFactory.getInstance().createInterface()
66         .register(versionableEntityType, metadata);
67   }
68
69   @Override
70   public Collection<ProcessEntity> list(ProcessEntity entity) {
71     return accessor
72         .list(entity.getVspId(), versionMapper.toUDT(entity.getVersion()), entity.getComponentId())
73         .all();
74   }
75
76   @Override
77   public void create(ProcessEntity entity) {
78     accessor.update(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
79         entity.getComponentId(), entity.getId(), entity.getName(), entity.getDescription());
80   }
81
82   @Override
83   public void update(ProcessEntity entity) {
84     accessor.update(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
85         entity.getComponentId(), entity.getId(), entity.getName(), entity.getDescription());
86   }
87
88   @Override
89   public ProcessEntity get(ProcessEntity entity) {
90     return accessor
91         .get(entity.getVspId(), versionMapper.toUDT(entity.getVersion()), entity.getComponentId(),
92             entity.getId());
93   }
94
95   @Override
96   public void delete(ProcessEntity entity) {
97     if (entity.getId() == null) {
98       accessor.deleteAll(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
99           entity.getComponentId());
100     } else {
101       accessor.delete(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
102           entity.getComponentId(), entity.getId());
103     }
104   }
105
106   public void deleteAll(ProcessEntity entity) {
107     accessor.deleteAll(entity.getVspId(), versionMapper.toUDT(entity.getVersion()));
108   }
109
110   @Accessor
111   interface ProcessAccessor {
112
113     @Query(
114         "insert into vsp_process (vsp_id, version, component_id, process_id, name, description) "
115             + "values (?,?,?,?,?,?)")
116     ResultSet update(String vspId, UDTValue version, String componentId, String id, String name,
117                      String description);
118
119     @Query(
120         "select vsp_id, version, component_id, process_id, name, description, artifact_name "
121             + "from vsp_process where vsp_id=? and version=? and component_id=? and process_id=?")
122     ProcessEntity get(String vspId, UDTValue version, String componentId, String id);
123
124     @Query(
125         "select vsp_id, version, component_id, process_id, name, description, artifact_name "
126             + "from vsp_process where vsp_id=? and version=? and component_id=?")
127     Result<ProcessEntity> list(String vspId, UDTValue version, String componentId);
128
129     @Query(
130         "delete from vsp_process where vsp_id=? and version=? and component_id=? and process_id=?")
131     ResultSet delete(String vspId, UDTValue version, String componentId, String id);
132
133     @Query("delete from vsp_process where vsp_id=? and version=? and component_id=?")
134     ResultSet deleteAll(String vspId, UDTValue version, String componentId);
135
136     @Query("delete from vsp_process where vsp_id=? and version=?")
137     ResultSet deleteAll(String vspId, UDTValue version);
138   }
139 }