Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / migration / dao / MigrationTasksDao.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.asdctool.migration.dao;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Row;
25 import com.datastax.driver.core.Session;
26 import com.datastax.driver.mapping.Mapper;
27 import com.datastax.driver.mapping.MappingManager;
28 import fj.data.Either;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.openecomp.sdc.be.dao.cassandra.CassandraClient;
31 import org.openecomp.sdc.be.dao.cassandra.CassandraDao;
32 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
33 import org.openecomp.sdc.be.resources.data.MigrationTaskEntry;
34 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
38
39 import javax.annotation.PostConstruct;
40 import java.math.BigInteger;
41
42 @Service
43 public class MigrationTasksDao extends CassandraDao {
44
45     private static Logger logger = Logger.getLogger(MigrationTasksDao.class.getName());
46     private MigrationTasksAccessor migrationTasksAccessor;
47     private Mapper<MigrationTaskEntry> migrationTaskMapper;
48
49     @Autowired
50     public MigrationTasksDao(CassandraClient cassandraClient){
51         super(cassandraClient);
52     }
53
54     @PostConstruct
55     public void init() {
56         String keyspace = AuditingTypesConstants.REPO_KEYSPACE;
57         if (client.isConnected()) {
58             Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
59             if (result.isLeft()) {
60                 session = result.left().value().left;
61                 manager = result.left().value().right;
62                 migrationTasksAccessor = manager.createAccessor(MigrationTasksAccessor.class);
63                 migrationTaskMapper =  manager.mapper(MigrationTaskEntry.class);
64                 logger.info("** migrationTasksAccessor created");
65             } else {
66                 logger.info("** migrationTasksAccessor failed");
67                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
68                         + result.right().value());
69             }
70         } else {
71             logger.info("** Cassandra client isn't connected");
72             logger.info("** migrationTasksAccessor created, but not connected");
73         }
74     }
75
76     public BigInteger getLatestMinorVersion(BigInteger majorVersion) {
77         try {
78             ResultSet latestMinorVersion = migrationTasksAccessor.getLatestMinorVersion(majorVersion.longValue());
79             Row minorVersionRow = latestMinorVersion.one();
80             return minorVersionRow == null ? BigInteger.valueOf(Long.MIN_VALUE) : BigInteger.valueOf(minorVersionRow.getLong(0));
81         } catch (RuntimeException e) {
82             logger.error("failed to get latest minor version for major version {}", majorVersion,  e);
83             throw e;
84         }
85     }
86
87     public void deleteAllTasksForVersion(BigInteger majorVersion) {
88         try {
89             migrationTasksAccessor.deleteTasksForMajorVersion(majorVersion.longValue());
90         } catch (RuntimeException e) {
91             logger.error("failed to delete tasks for major version {}", majorVersion,  e);
92             throw e;
93         }
94     }
95
96     public void createMigrationTask(MigrationTaskEntry migrationTask) {
97         migrationTaskMapper.save(migrationTask);
98     }
99
100
101 }