[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 / NetworkDaoCassandraImpl.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.NetworkDao;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
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 NetworkDaoCassandraImpl extends CassandraBaseDao<NetworkEntity> implements NetworkDao {
46
47   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
48   private static final Mapper<NetworkEntity> mapper =
49       noSqlDb.getMappingManager().mapper(NetworkEntity.class);
50   private static final NetworkAccessor accessor =
51       noSqlDb.getMappingManager().createAccessor(NetworkAccessor.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.NETWORK_NAME,
65         Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
66             mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
67
68     VersioningManagerFactory.getInstance().createInterface()
69         .register(versionableEntityType, metadata);
70   }
71
72   @Override
73   protected Mapper<NetworkEntity> getMapper() {
74     return mapper;
75   }
76
77   @Override
78   protected Object[] getKeys(NetworkEntity entity) {
79     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
80         entity.getId()};
81   }
82
83   @Override
84   public void update(NetworkEntity entity) {
85     accessor.updateCompositionData(entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
86         entity.getId(), entity.getCompositionData());
87   }
88
89   /*@Override
90   public void updateQuestionnaireData(String vspId, Version version, String id,
91                                       String questionnaireData) {
92     accessor.updateQuestionnaireData(questionnaireData, vspId, versionMapper.toUDT(version), id);
93   }*/
94
95   @Override
96   public void deleteAll(String vspId, Version version) {
97     accessor.deleteAll(vspId, version);
98   }
99
100
101
102   @Override
103   public Collection<NetworkEntity> list(NetworkEntity entity) {
104     return accessor.list(entity.getVspId(), versionMapper.toUDT(entity.getVersion())).all();
105   }
106
107   @Accessor
108   interface NetworkAccessor {
109
110     @Query(
111         "select vsp_id, version, network_id, composition_data from vsp_network where vsp_id=?"
112             + " and version=?")
113     Result<NetworkEntity> list(String vspId, UDTValue version);
114
115     @Query(
116         "insert into vsp_network (vsp_id, version, network_id, composition_data) values (?,?,?,?)")
117     ResultSet updateCompositionData(String vspId, UDTValue version, String id,
118                                     String compositionData);
119
120     @Query(
121         "update vsp_network set questionnaire_data=? where vsp_id=? and version=? and network_id=?")
122     ResultSet updateQuestionnaireData(String questionnaireData, String vspId, UDTValue version,
123                                       String id);
124
125     @Query("delete from vsp_network where vsp_id=? and version=?")
126     ResultSet deleteAll(String vspId, Version version);
127
128   }
129 }