Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.transport.compat;

import com.cloud.utils.StringUtils;
import com.cloud.utils.exception.CloudRuntimeException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* JSON serializer adapter for transport classes (com.cloud.agent.api.to.*) that ensures backward compatibility
* with older Agent versions due to rename of the fields
* (see https://github.com/apache/cloudstack/pull/10514)
*
* This class does not build its own Gson instance: doing so would silently drop whichever exclusion
* strategy (e.g. log redaction) and sibling compat adaptors (for nested TOs) the enclosing Gson was
* configured with. Instead, whoever registers an instance of this class into a GsonBuilder is
* responsible for also calling {@link #initGson(Gson)} with a Gson that (a) carries that same
* exclusion strategy and (b) has adapters registered for any nested TO types that also need field
* renaming, but not for this adaptor's own type (to avoid infinite recursion). See
* {@link com.cloud.serializer.GsonHelper#setDefaultGsonConfig(com.google.gson.GsonBuilder)}.
*/
public class AbstractTOAdaptor<T> implements JsonSerializer<T> {
Comment thread
weizhouapache marked this conversation as resolved.
private Gson gson;
private Map<String, String> fieldMappings;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be final?


protected AbstractTOAdaptor(String... fields) {
this.fieldMappings = new LinkedHashMap<>();
for (int i = 0; i + 1 < fields.length; i += 2) {
String sourceField = fields[i];
String destinationField = fields[i + 1];
// skip empty fields
if (StringUtils.isBlank(sourceField) || StringUtils.isBlank(destinationField)) {
continue;
}
this.fieldMappings.put(sourceField, destinationField);
}
if (this.fieldMappings.isEmpty()) {
throw new CloudRuntimeException("Field mappings must not be empty");
}
}

public void initGson(Gson gson) {
this.gson = gson;
}

@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return null;
}
JsonElement tree = gson.toJsonTree(src);
if (tree.isJsonObject()) {
JsonObject obj = tree.getAsJsonObject();
for (Map.Entry<String, String> field : fieldMappings.entrySet()) {
String sourceField = field.getKey();
String destinationField = field.getValue();
if (obj.has(sourceField)) {
obj.add(destinationField, obj.get(sourceField));
}
}
}
return tree;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.transport.compat;

import com.cloud.agent.api.to.DiskTO;

/**
* See {@link AbstractTOAdaptor}.
*/
public class DiskTOAdaptor extends AbstractTOAdaptor<DiskTO> {
public DiskTOAdaptor() {
super("details", "_details");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.transport.compat;

import com.cloud.agent.api.MigrateCommand;

/**
* See {@link AbstractTOAdaptor}.
*/
public class MigrateCommandAdaptor extends AbstractTOAdaptor<MigrateCommand> {
public MigrateCommandAdaptor() {
super("destinationIp", "destIp", "windows", "isWindows", "virtualMachine", "vmTO");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.transport.compat;

import com.cloud.agent.api.to.NetworkTO;

/**
* See {@link AbstractTOAdaptor}.
*/
public class NetworkTOAdaptor extends AbstractTOAdaptor<NetworkTO> {
public NetworkTOAdaptor() {
super("securityGroupEnabled", "isSecurityGroupEnabled");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.agent.transport.compat;

import com.cloud.agent.api.to.VirtualMachineTO;

/**
* See {@link AbstractTOAdaptor}.
*/
public class VirtualMachineTOAdaptor extends AbstractTOAdaptor<VirtualMachineTO> {

public VirtualMachineTOAdaptor() {
super("details", "params");
}
}
44 changes: 44 additions & 0 deletions core/src/main/java/com/cloud/serializer/GsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

import java.util.List;

import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.to.DiskTO;
import com.cloud.agent.api.to.NetworkTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.agent.transport.compat.DiskTOAdaptor;
import com.cloud.agent.transport.compat.MigrateCommandAdaptor;
import com.cloud.agent.transport.compat.NetworkTOAdaptor;
import com.cloud.agent.transport.compat.VirtualMachineTOAdaptor;
import com.cloud.hypervisor.Hypervisor;
import org.apache.cloudstack.transport.HypervisorTypeAdaptor;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -78,6 +86,42 @@ public static Gson setDefaultGsonConfig(GsonBuilder builder) {
}.getType(), new NwGroupsCommandTypeAdaptor());
builder.registerTypeAdapter(Storage.StoragePoolType.class, new StoragePoolTypeAdaptor());
builder.registerTypeAdapter(Hypervisor.HypervisorType.class, new HypervisorTypeAdaptor());

// added for compatibility purposes, remove after all Agents migrate to the new version
//
// Each compat adaptor below needs a "base" Gson to run its own reflective (pre-rename)
// serialization through, so that nested TOs are renamed too and the exclusion strategy set
// on `builder` (e.g. log redaction) is honoured consistently at every nesting level. That base
// Gson is built incrementally off the same builder, snapshotted (via builder.create()) just
// before each adaptor's own type is registered on it, so it carries every sibling adaptor it
// can nest without ever routing back into itself and recursing forever.
DiskTOAdaptor diskAdaptor = new DiskTOAdaptor();
NetworkTOAdaptor netAdaptor = new NetworkTOAdaptor();
VirtualMachineTOAdaptor vmAdaptor = new VirtualMachineTOAdaptor();
MigrateCommandAdaptor migrateAdaptor = new MigrateCommandAdaptor();

// DiskTO and NetworkTO don't nest any other compat TO, so the plain config built so far is
// already the correct base Gson for them.
Gson leafDelegateGson = builder.create();
diskAdaptor.initGson(leafDelegateGson);
netAdaptor.initGson(leafDelegateGson);

// VirtualMachineTO nests DiskTO[] and NicTO[] (NicTO extends NetworkTO), so its base Gson needs
// Disk/Network adapters too. registerTypeHierarchyAdapter is used for NetworkTO so that the
// NicTO[]-declared "nics" field is matched via its supertype.
builder.registerTypeAdapter(DiskTO.class, diskAdaptor);
builder.registerTypeHierarchyAdapter(NetworkTO.class, netAdaptor);
Gson vmDelegateGson = builder.create();
vmAdaptor.initGson(vmDelegateGson);

// MigrateCommand nests a VirtualMachineTO, so its base Gson needs the VirtualMachineTO adapter
// (which already renames the nested disks/nics above).
builder.registerTypeAdapter(VirtualMachineTO.class, vmAdaptor);
Gson migrateDelegateGson = builder.create();
migrateAdaptor.initGson(migrateDelegateGson);

builder.registerTypeAdapter(MigrateCommand.class, migrateAdaptor);

Gson gson = builder.create();
dsAdaptor.initGson(gson);
dtAdaptor.initGson(gson);
Expand Down
Loading
Loading