11524ed05f6dbc0b5210829f94a40c2936b73d51
[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.vendorsoftwareproduct.dao.impl;
18
19 import com.datastax.driver.core.ResultSet;
20 import com.datastax.driver.core.UDTValue;
21 import com.datastax.driver.mapping.Mapper;
22 import com.datastax.driver.mapping.Result;
23 import com.datastax.driver.mapping.UDTMapper;
24 import com.datastax.driver.mapping.annotations.Accessor;
25 import com.datastax.driver.mapping.annotations.Query;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import org.openecomp.core.dao.impl.CassandraBaseDao;
30 import org.openecomp.core.nosqldb.api.NoSqlDb;
31 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
35 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
38 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
39
40 public class ComponentDependencyModelDaoCassandraImpl extends
41     CassandraBaseDao<ComponentDependencyModelEntity> implements ComponentDependencyModelDao {
42
43   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
44   private static final Mapper<ComponentDependencyModelEntity> mapper =
45       noSqlDb.getMappingManager().mapper(ComponentDependencyModelEntity.class);
46   private static final ComponentDependencyModelDaoCassandraImpl.ComponentDependencyModelAccessor
47       accessor = noSqlDb.getMappingManager().createAccessor(
48       ComponentDependencyModelDaoCassandraImpl.ComponentDependencyModelAccessor.class);
49   private static final UDTMapper<Version> versionMapper =
50       noSqlDb.getMappingManager().udtMapper(Version.class);
51
52   @Override
53   protected Mapper<ComponentDependencyModelEntity> getMapper() {
54     return mapper;
55   }
56
57   @Override
58   protected Object[] getKeys(ComponentDependencyModelEntity entity) {
59     return new Object[]{entity.getVspId(), versionMapper.toUDT(entity.getVersion()),
60         entity.getId()};
61   }
62
63   @Override
64   public Collection<ComponentDependencyModelEntity> list(ComponentDependencyModelEntity entity) {
65     return accessor
66         .list(entity.getVspId(), versionMapper.toUDT(entity.getVersion())).all();
67   }
68
69   @Override
70   public void registerVersioning(String versionableEntityType) {
71     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
72         mapper.getTableMetadata().getName(),
73         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
74         mapper.getTableMetadata().getPartitionKey().get(1).getName());
75
76     metadata.setUniqueValuesMetadata(Collections.singletonList(new UniqueValueMetadata(
77         VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
78         Arrays.asList("vsp_id", "version", "component_id", "name"))));
79
80     ActionVersioningManagerFactory.getInstance().createInterface()
81         .register(versionableEntityType, metadata);
82
83   }
84
85   @Accessor
86   interface ComponentDependencyModelAccessor {
87     @Query("delete from vsp_component_dependency_model where vsp_id=? and version=?")
88     ResultSet deleteAll(String vspId, UDTValue version);
89
90     @Query(
91         "select * from vsp_component_dependency_model where vsp_id=? and version=?")
92     Result<ComponentDependencyModelEntity> list(String vspId, UDTValue version);
93   }
94 }