9574dac3d547e0cb5d283d9fbc84c3299a554591
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.impl;
18
19 import com.datastax.driver.mapping.Mapper;
20 import com.datastax.driver.mapping.Result;
21 import com.datastax.driver.mapping.UDTMapper;
22 import com.datastax.driver.mapping.annotations.Accessor;
23 import com.datastax.driver.mapping.annotations.Query;
24 import java.util.Collection;
25 import org.openecomp.core.dao.impl.CassandraBaseDao;
26 import org.openecomp.core.nosqldb.api.NoSqlDb;
27 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
28 import org.openecomp.sdc.vendorlicense.dao.VendorLicenseModelDao;
29 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
30 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
33
34 public class VendorLicenseModelCassandraDaoImpl extends CassandraBaseDao<VendorLicenseModelEntity>
35     implements
36     VendorLicenseModelDao {
37
38   private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
39   private static Mapper<VendorLicenseModelEntity> mapper =
40       noSqlDb.getMappingManager().mapper(VendorLicenseModelEntity.class);
41   private static VendorLicenseModelAccessor accessor =
42       noSqlDb.getMappingManager().createAccessor(VendorLicenseModelAccessor.class);
43   private static UDTMapper<Version> versionMapper =
44       noSqlDb.getMappingManager().udtMapper(Version.class);
45
46   @Override
47   public void registerVersioning(String versionableEntityType) {
48     ActionVersioningManagerFactory.getInstance().createInterface()
49         .register(versionableEntityType, new VersionableEntityMetadata(
50             mapper.getTableMetadata().getName(),
51             mapper.getTableMetadata().getPartitionKey().get(0).getName(),
52             mapper.getTableMetadata().getPartitionKey().get(1).getName()));
53   }
54
55   @Override
56   protected Mapper<VendorLicenseModelEntity> getMapper() {
57     return mapper;
58   }
59
60   @Override
61   protected Object[] getKeys(VendorLicenseModelEntity entity) {
62     return new Object[]{entity.getId(), versionMapper.toUDT(entity.getVersion())};
63   }
64
65   @Override
66   public Collection<VendorLicenseModelEntity> list(VendorLicenseModelEntity vendorLicenseModel) {
67     return accessor.getAll().all();
68   }
69
70   @Accessor
71   interface VendorLicenseModelAccessor {
72
73     @Query("SELECT * FROM vendor_license_model")
74     Result<VendorLicenseModelEntity> getAll();
75
76   }
77 }