Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / store / HealingHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 - 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.core.tools.store;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.mapping.annotations.Accessor;
25 import com.datastax.driver.mapping.annotations.Query;
26 import org.openecomp.core.nosqldb.api.NoSqlDb;
27 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
28 import org.openecomp.core.tools.store.zusammen.datatypes.HealingEntity;
29
30 import java.util.List;
31
32 /**
33  * Created by ayalaben on 10/15/2017
34  */
35 public class HealingHandler {
36
37   private static NoSqlDb nnoSqlDb = NoSqlDbFactory.getInstance().createInterface();
38   private static HealingAccessor accessor =
39       nnoSqlDb.getMappingManager().createAccessor(HealingAccessor.class);
40
41
42   public void populateHealingTable(List<HealingEntity> healingEntities) {
43     healingEntities.forEach(healingEntity -> {
44       if (isHealingRecordExist(healingEntity)) {
45         accessor.updateFlag(healingEntity.getHealingFlag(), healingEntity.getSpace(),
46             healingEntity.getItemId(), healingEntity.getVersionId());
47       } else {
48         accessor.create(healingEntity.getSpace(), healingEntity.getItemId(),
49             healingEntity.getVersionId(), healingEntity.getHealingFlag(),
50             healingEntity.getOldVersion());
51       }
52     });
53   }
54
55   private boolean isHealingRecordExist(HealingEntity healingEntity) {
56     return accessor.getFlag(healingEntity.getSpace(), healingEntity.getItemId(),
57         healingEntity.getVersionId()).getAvailableWithoutFetching() == 1;
58   }
59
60   @Accessor
61   interface HealingAccessor {
62
63     @Query("SELECT healing_needed FROM healing WHERE space=? AND item_id=? AND version_id=?")
64     ResultSet getFlag(String space, String itemId, String versionId);
65
66     @Query("Insert into healing (space, item_id, version_id, healing_needed, old_version) " +
67         "values (?,?,?,?,?)")
68     void create(String space, String itemId, String versionId, boolean flag, String oldVersion);
69
70     @Query("UPDATE healing SET healing_needed=? WHERE space=? AND item_id=? AND version_id=?")
71     void updateFlag(boolean flag, String space, String itemId, String versionId);
72   }
73
74 }