Merge "DG changes for the closed loop and async support in MDONS"
[sdnc/oam.git] / data-migrator / src / main / java / org / onap / sdnc / oam / datamigrator / migrators / Migrator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SDNC
4  * ================================================================================
5  * Copyright 2019 AMDOCS
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 package org.onap.sdnc.oam.datamigrator.migrators;
21
22 import com.google.gson.Gson;
23 import com.google.gson.JsonObject;
24 import org.onap.sdnc.oam.datamigrator.common.MigratorConfiguration;
25 import org.onap.sdnc.oam.datamigrator.common.Operation;
26 import org.onap.sdnc.oam.datamigrator.common.RestconfClient;
27 import org.onap.sdnc.oam.datamigrator.exceptions.RestconfException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.FileReader;
33 import java.io.FileWriter;
34 import java.io.IOException;
35
36 public abstract class Migrator {
37
38     protected RestconfClient sourceClient;
39     protected RestconfClient targetClient;
40     protected boolean success = true;
41     private MigratorConfiguration config;
42     private final Logger log = LoggerFactory.getLogger(PreloadInformationMigrator.class);
43
44
45     public void run(Operation operation){
46         {
47             JsonObject sourceData;
48             if(operation != Operation.RESTORE) {
49
50                 try {
51                     sourceData = sourceClient.get(getYangModuleName()+":"+ getSourcePath());
52                     if(operation == Operation.BACKUP){
53                         String fileName = getFileName();
54                         try {
55                             BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
56                             writer.write(sourceData.toString());
57                             writer.close();
58                         } catch (IOException e) {
59                             log.error("Error writing data to file : " + fileName, e);
60                             success = false;
61                             return;
62                         }
63                         return;
64                     }
65                 } catch (RestconfException e) {
66                     if(e.getErrorCode() == 404){
67                         log.error("No data available for migration. Returning silent success.", e);
68                         success = true;
69                     }else {
70                         log.error("Error retrieving data from MD-SAL store. Error code: " + e.getErrorCode() + ". Error message:" + e.getErrorMessage(), e);
71                         success = false;
72                     }
73                     return;
74                 }
75             }else {
76                 String fileName = getFileName();
77                 try {
78                     Gson gson = new Gson();
79                     sourceData = gson.fromJson(new BufferedReader(new FileReader(fileName)),JsonObject.class);
80                 } catch (IOException e) {
81                     log.error("Error Reading data from file : " + fileName, e);
82                     success = false;
83                     return;
84                 }
85             }
86             try {
87                 String targetData = convertData(sourceData);
88                 targetClient.put(getYangModuleName()+":"+ getTargetPath(),targetData);
89             } catch (RestconfException e) {
90                 log.error("Error loading data to MD-SAL store. Error code: "+e.getErrorCode()+". Error message:"+e.getErrorMessage(),e);
91                 success=false;
92             }
93         }
94     }
95
96     private String getFileName() {
97         return config.getDataPath()+ "/" + getYangModuleName()+ "_"+ getSourcePath()+"_"+ getTargetPath() + ".json";
98     }
99
100     protected abstract String convertData(JsonObject sourceData);
101
102     public abstract String getYangModuleName();
103     public abstract String getSourcePath();
104     public abstract String getTargetPath();
105
106     public void init(MigratorConfiguration config){
107         this.config = config;
108         sourceClient = new RestconfClient(config.getSourceHost(),config.getSourceUser(),config.getSourcePassword());
109         targetClient = new RestconfClient(config.getTargetHost(),config.getTargetUser(),config.getTargetPassword());
110     }
111
112     public RestconfClient getSourceClient() {
113         return sourceClient;
114     }
115
116     public void setSourceClient(RestconfClient sourceClient) {
117         this.sourceClient = sourceClient;
118     }
119
120     public RestconfClient getTargetClient() {
121         return targetClient;
122     }
123
124     public void setTargetClient(RestconfClient targetClient) {
125         this.targetClient = targetClient;
126     }
127
128     public boolean isSuccess() {
129         return success;
130     }
131
132     public void setSuccess(boolean success) {
133         this.success = success;
134     }
135 }
136