[sdc] docker file fix for cassandra
[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 / ComponentArtifactDaoCassandraImpl.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.dao.impl.CassandraBaseDao;
31 import org.openecomp.core.nosqldb.api.NoSqlDb;
32 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentArtifactEntity;
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
40 import java.nio.ByteBuffer;
41 import java.util.Collection;
42
43 public class ComponentArtifactDaoCassandraImpl extends CassandraBaseDao<ComponentArtifactEntity>
44     implements ComponentArtifactDao {
45
46   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
47   private static final ComponentArtifactAccessor accessor =
48       noSqlDb.getMappingManager().createAccessor(ComponentArtifactAccessor.class);
49   private static final Mapper<ComponentArtifactEntity> mapper =
50       noSqlDb.getMappingManager().mapper(ComponentArtifactEntity.class);
51   private static final UDTMapper<Version> versionMapper =
52       noSqlDb.getMappingManager().udtMapper(Version.class);
53
54
55   @Override
56   public void registerVersioning(String versionableEntityType) {
57     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
58         mapper.getTableMetadata().getName(),
59         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
60         mapper.getTableMetadata().getPartitionKey().get(1).getName());
61
62     // metadata.setUniqueValuesMetadata(Collections.singletonList(new UniqueValueMetadata
63     // (VendorSoftwareProductConstants.UniqueValues.COMPONENT_ARTIFACT_NAME,
64     // Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(), mapper
65     // .getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
66
67     VersioningManagerFactory.getInstance().createInterface()
68         .register(versionableEntityType, metadata);
69   }
70
71   @Override
72   public ComponentArtifactEntity getArtifactByType(ComponentArtifactEntity entity) {
73     return accessor.listByType(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
74         entity.getComponentId(), entity.getType().toString()).one();
75   }
76
77   @Override
78   protected Mapper<ComponentArtifactEntity> getMapper() {
79     return mapper;
80   }
81
82   @Override
83   protected Object[] getKeys(ComponentArtifactEntity entity) {
84     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
85         entity.getComponentId(), entity.getType().toString(), entity.getId()};
86   }
87
88   @Override
89   public Collection<ComponentArtifactEntity> list(ComponentArtifactEntity entity) {
90     return accessor.listByType(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
91         entity.getComponentId(), entity.getType().toString()).all();
92   }
93
94   @Override
95   public void update(ComponentArtifactEntity entity) {
96     accessor.update(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
97         entity.getComponentId(), entity.getId(), entity.getArtifactName(), entity.getArtifact(),
98         entity.getType().toString());
99
100   }
101
102   @Override
103   public void delete(ComponentArtifactEntity entity) {
104     accessor.delete(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
105         entity.getComponentId(), entity.getType().toString());
106   }
107
108   @Override
109   public Collection<ComponentArtifactEntity> getArtifactNamesAndTypesForComponent(
110       ComponentArtifactEntity entity) {
111     return accessor.getArtifactNamesAndTypesForComponent(entity.getVspId(),
112         versionMapper.toUDT(entity.getVersion()), entity.getComponentId()).all();
113   }
114
115   @Override
116   public void deleteAll(ComponentArtifactEntity entity) {
117     accessor.deleteAll(entity.getVspId(), versionMapper.toUDT(entity.getVersion()));
118   }
119
120
121   @Accessor
122   interface ComponentArtifactAccessor {
123
124     @Query(
125         "insert into vsp_component_artifact (vsp_id, version, component_id, artifact_id, name, "
126             + "artifact, artifact_type) values (?,?,?,?,?,?,?)")
127     ResultSet update(String vspId, UDTValue version, String componentId, String id,
128                      String artifactName, ByteBuffer artifact, String type);
129
130     @Query(
131         "select vsp_id, version, component_id, artifact_type, artifact_id, name, artifact from "
132             + "vsp_component_artifact where vsp_id=? and version=? and component_id=? and "
133             + "artifact_type =?")
134     Result<ComponentArtifactEntity> listByType(String vspId, UDTValue version, String componentId,
135                                                String type);
136
137     @Query(
138         "delete name, artifact from vsp_component_artifact where vsp_id=? and version=? and "
139             + "component_id=? and artifact_type=? and artifact_id=?")
140     ResultSet delete(String vspId, UDTValue version, String componentId, String type,
141                      String artifactId);
142
143     @Query(
144         "delete from vsp_component_artifact where vsp_id=? and version=? and component_id=? "
145             + "and artifact_type=?")
146     ResultSet delete(String vspId, UDTValue version, String componentId, String artifactType);
147
148     @Query("delete from vsp_component_artifact where vsp_id=? and version=?")
149     ResultSet deleteAll(String vspId, UDTValue version);
150
151     @Query(
152         "select name, artifact_type from vsp_component_artifact where vsp_id=? and version=? "
153             + "and component_id=?")
154     Result<ComponentArtifactEntity> getArtifactNamesAndTypesForComponent(String vspId,
155                                                                          UDTValue version,
156                                                                          String componentId);
157
158   }
159 }