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.VendorSoftwareProductConstants;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
36 import org.openecomp.sdc.versioning.VersioningManagerFactory;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
39 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
41 import java.util.Arrays;
42 import java.util.Collection;
43 import java.util.Collections;
45 public class NicDaoCassandraImpl extends CassandraBaseDao<NicEntity> implements NicDao {
47 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
48 private static final Mapper<NicEntity> mapper =
49 noSqlDb.getMappingManager().mapper(NicEntity.class);
50 private static final NicAccessor accessor =
51 noSqlDb.getMappingManager().createAccessor(NicAccessor.class);
52 private static final UDTMapper<Version> versionMapper =
53 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());
63 metadata.setUniqueValuesMetadata(Collections
64 .singletonList(new UniqueValueMetadata(VendorSoftwareProductConstants.UniqueValues.NIC_NAME,
65 Arrays.asList("vsp_id", "version", "component_id", "name"))));
67 VersioningManagerFactory.getInstance().createInterface()
68 .register(versionableEntityType, metadata);
72 protected Mapper<NicEntity> getMapper() {
77 protected Object[] getKeys(NicEntity entity) {
78 return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
79 entity.getComponentId(), entity.getId()};
83 public void create(NicEntity entity) {
88 public void update(NicEntity entity) {
89 accessor.updateCompositionData(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
90 entity.getComponentId(), entity.getId(), entity.getCompositionData());
94 public void updateQuestionnaireData(String vspId, Version version, String id, String componentId,
95 String questionnaireData) {
96 accessor.updateQuestionnaireData(questionnaireData, vspId, versionMapper.toUDT(version), id,
101 public Collection<NicEntity> listByVsp(String vspId, Version version) {
102 return accessor.listByVspId(vspId, versionMapper.toUDT(version)).all();
106 public Collection<NicEntity> list(NicEntity entity) {
107 return accessor.listByComponentId(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
108 entity.getComponentId()).all();
111 public void deleteByComponentId(String vspId, Version version, String componentId) {
112 accessor.deleteByComponentId(vspId, version, componentId);
115 public void deleteByVspId(String vspId, Version version) {
116 accessor.deleteByVspId(vspId, versionMapper.toUDT(version));
120 interface NicAccessor {
123 "select vsp_id, version, component_id, nic_id, composition_data "
124 + "from vsp_component_nic where vsp_id=? and version=? and component_id=?")
125 Result<NicEntity> listByComponentId(String vspId, UDTValue version, String componentId);
127 @Query("select * from vsp_component_nic where vsp_id=? and version=?")
128 Result<NicEntity> listByVspId(String vspId, UDTValue version);
131 "insert into vsp_component_nic (vsp_id, version, component_id, nic_id, composition_data) "
132 + "values (?,?,?,?,?)")
133 ResultSet updateCompositionData(String vspId, UDTValue version, String componentId, String id,
134 String compositionData);
137 "update vsp_component_nic set questionnaire_data=? where vsp_id=? and version=? "
138 + "and component_id=? and nic_id=?")
139 ResultSet updateQuestionnaireData(String questionnaireData, String vspId, UDTValue version,
140 String componentId, String id);
142 @Query("delete from vsp_component_nic where vsp_id=? and version=? and component_id=?")
143 ResultSet deleteByComponentId(String vspId, Version version, String componentId);
145 @Query("delete from vsp_component_nic where vsp_id=? and version=?")
146 ResultSet deleteByVspId(String vspId, UDTValue version);