2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.sdnc.oam.datamigrator.migrators;
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;
36 public abstract class Migrator {
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);
45 public void run(Operation operation){
47 JsonObject sourceData;
48 if(operation != Operation.RESTORE) {
51 sourceData = sourceClient.get(getYangModuleName()+":"+ getSourcePath());
52 if(operation == Operation.BACKUP){
53 String fileName = getFileName();
55 BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
56 writer.write(sourceData.toString());
58 } catch (IOException e) {
59 log.error("Error writing data to file : " + fileName, e);
65 } catch (RestconfException e) {
66 if(e.getErrorCode() == 404){
67 log.error("No data available for migration. Returning silent success.", e);
70 log.error("Error retrieving data from MD-SAL store. Error code: " + e.getErrorCode() + ". Error message:" + e.getErrorMessage(), e);
76 String fileName = getFileName();
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);
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);
96 private String getFileName() {
97 return config.getDataPath()+ "/" + getYangModuleName()+ "_"+ getSourcePath()+"_"+ getTargetPath() + ".json";
100 protected abstract String convertData(JsonObject sourceData);
102 public abstract String getYangModuleName();
103 public abstract String getSourcePath();
104 public abstract String getTargetPath();
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());
112 public RestconfClient getSourceClient() {
116 public void setSourceClient(RestconfClient sourceClient) {
117 this.sourceClient = sourceClient;
120 public RestconfClient getTargetClient() {
124 public void setTargetClient(RestconfClient targetClient) {
125 this.targetClient = targetClient;
128 public boolean isSuccess() {
132 public void setSuccess(boolean success) {
133 this.success = success;