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.JsonArray;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import org.apache.commons.lang3.StringUtils;
29 public abstract class RenameDeleteLeafMigrator extends Migrator {
31 protected static Map<String,String> renamedFields ;
32 protected static Set<String> deletedFields ;
35 protected String convertData(JsonObject sourceData) {
36 JsonObject target = convert(sourceData,"");
37 return target.toString();
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));
54 target.add(renamedFields.getOrDefault(prefixKey,key), targetList);
56 target.add(renamedFields.getOrDefault(prefixKey,key), convert(value.getAsJsonObject(),prefixKey));