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 / RenameDeleteLeafMigrator.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.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import org.apache.commons.lang3.StringUtils;
26 import java.util.Map;
27 import java.util.Set;
28
29 public abstract class RenameDeleteLeafMigrator extends Migrator {
30
31     protected static Map<String,String> renamedFields ;
32     protected static Set<String> deletedFields ;
33
34     @Override
35     protected String convertData(JsonObject sourceData) {
36         JsonObject target =  convert(sourceData,"");
37         return  target.toString();
38     }
39
40     protected JsonObject convert(JsonObject source,String parent) {
41         JsonObject target = new JsonObject();
42         for (String key : source.keySet()){
43             String prefixKey = StringUtils.isNotEmpty(parent) ? parent + "."+key : key;
44             if(!deletedFields.contains(prefixKey)) {
45                 JsonElement value = source.get(key);
46                 if (value.isJsonPrimitive()) {
47                     target.add(renamedFields.getOrDefault(prefixKey,key), value);
48                 } else if(value.isJsonArray()){
49                     JsonArray targetList = new JsonArray();
50                     JsonArray sourceArray = value.getAsJsonArray();
51                     for(JsonElement  e : sourceArray){
52                          targetList.add(convert(e.getAsJsonObject(),prefixKey));
53                     }
54                     target.add(renamedFields.getOrDefault(prefixKey,key), targetList);
55                 } else{
56                     target.add(renamedFields.getOrDefault(prefixKey,key), convert(value.getAsJsonObject(),prefixKey));
57                 }
58             }
59         }
60         return target;
61     }
62 }