[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 / NicDaoCassandraImpl.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.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;
40
41 import java.util.Arrays;
42 import java.util.Collection;
43 import java.util.Collections;
44
45 public class NicDaoCassandraImpl extends CassandraBaseDao<NicEntity> implements NicDao {
46
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);
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
63     metadata.setUniqueValuesMetadata(Collections.singletonList(new UniqueValueMetadata(
64         VendorSoftwareProductConstants.UniqueValues.NIC_NAME,
65         Arrays.asList("vsp_id", "version", "component_id", "name"))));
66
67     VersioningManagerFactory.getInstance().createInterface()
68         .register(versionableEntityType, metadata);
69   }
70
71   @Override
72   protected Mapper<NicEntity> getMapper() {
73     return mapper;
74   }
75
76   @Override
77   protected Object[] getKeys(NicEntity entity) {
78     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
79         entity.getComponentId(), entity.getId()};
80   }
81
82   @Override
83   public void create(NicEntity entity) {
84     super.create(entity);
85   }
86
87   @Override
88   public void update(NicEntity entity) {
89     accessor.updateCompositionData(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
90         entity.getComponentId(), entity.getId(), entity.getCompositionData());
91   }
92
93   @Override
94   public NicEntity getQuestionnaireData(String vspId, Version version, String componentId,
95                                         String nicId) {
96     return null; // TODO: 3/20/2017  
97   }
98
99   @Override
100   public void updateQuestionnaireData(String vspId, Version version, String id, String componentId,
101                                       String questionnaireData) {
102     accessor.updateQuestionnaireData(questionnaireData, vspId, versionMapper.toUDT(version), id,
103         componentId);
104   }
105
106   @Override
107   public Collection<NicEntity> listByVsp(String vspId, Version version) {
108     return accessor.listByVspId(vspId, versionMapper.toUDT(version)).all();
109   }
110
111   public void deleteByComponentId(String vspId, Version version, String componentId) {
112     accessor.deleteByComponentId(vspId, version, componentId);
113   }
114
115   public void deleteByVspId(String vspId, Version version) {
116     accessor.deleteByVspId(vspId, versionMapper.toUDT(version));
117   }
118
119   @Override
120   public Collection<NicEntity> list(NicEntity entity) {
121     return accessor.listByComponentId(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
122         entity.getComponentId()).all();
123   }
124
125   @Accessor
126   interface NicAccessor {
127
128     @Query(
129         "select vsp_id, version, component_id, nic_id, composition_data"
130             + " from vsp_component_nic where vsp_id=? and version=? and component_id=?")
131     Result<NicEntity> listByComponentId(String vspId, UDTValue version, String componentId);
132
133     @Query("select * from vsp_component_nic where vsp_id=? and version=?")
134     Result<NicEntity> listByVspId(String vspId, UDTValue version);
135
136     @Query(
137         "insert into vsp_component_nic (vsp_id, version, component_id, nic_id, composition_data)"
138             + " values (?,?,?,?,?)")
139     ResultSet updateCompositionData(String vspId, UDTValue version, String componentId, String id,
140                                     String compositionData);
141
142     @Query(
143         "update vsp_component_nic set questionnaire_data=? where vsp_id=? and version=?"
144             + " and component_id=? and nic_id=?")
145     ResultSet updateQuestionnaireData(String questionnaireData, String vspId, UDTValue version,
146                                       String componentId, String id);
147
148     @Query("delete from vsp_component_nic where vsp_id=? and version=? and component_id=?")
149     ResultSet deleteByComponentId(String vspId, Version version, String componentId);
150
151     @Query("delete from vsp_component_nic where vsp_id=? and version=?")
152     ResultSet deleteByVspId(String vspId, UDTValue version);
153   }
154 }