2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl;
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;
40 import java.nio.ByteBuffer;
41 import java.util.Collection;
43 public class ComponentArtifactDaoCassandraImpl extends CassandraBaseDao<ComponentArtifactEntity>
44 implements ComponentArtifactDao {
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);
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());
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"))));
67 VersioningManagerFactory.getInstance().createInterface()
68 .register(versionableEntityType, metadata);
72 public ComponentArtifactEntity getArtifactByType(ComponentArtifactEntity entity) {
73 return accessor.listByType(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
74 entity.getComponentId(), entity.getType().toString()).one();
78 protected Mapper<ComponentArtifactEntity> getMapper() {
83 protected Object[] getKeys(ComponentArtifactEntity entity) {
84 return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
85 entity.getComponentId(), entity.getType().toString(), entity.getId()};
89 public Collection<ComponentArtifactEntity> list(ComponentArtifactEntity entity) {
90 return accessor.listByType(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
91 entity.getComponentId(), entity.getType().toString()).all();
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());
103 public void delete(ComponentArtifactEntity entity) {
104 accessor.delete(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
105 entity.getComponentId(), entity.getType().toString());
109 public Collection<ComponentArtifactEntity> getArtifactNamesAndTypesForComponent(
110 ComponentArtifactEntity entity) {
111 return accessor.getArtifactNamesAndTypesForComponent(entity.getVspId(),
112 versionMapper.toUDT(entity.getVersion()), entity.getComponentId()).all();
116 public void deleteAll(ComponentArtifactEntity entity) {
117 accessor.deleteAll(entity.getVspId(), versionMapper.toUDT(entity.getVersion()));
122 interface ComponentArtifactAccessor {
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);
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,
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,
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);
148 @Query("delete from vsp_component_artifact where vsp_id=? and version=?")
149 ResultSet deleteAll(String vspId, UDTValue version);
152 "select name, artifact_type from vsp_component_artifact where vsp_id=? and version=? "
153 + "and component_id=?")
154 Result<ComponentArtifactEntity> getArtifactNamesAndTypesForComponent(String vspId,