-
Notifications
You must be signed in to change notification settings - Fork 1.3k
core: introduce a set of type adapters #13624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.22
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // 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.Answer; | ||
| import com.cloud.agent.api.Command; | ||
| import com.cloud.agent.api.SecStorageFirewallCfgCommand; | ||
| import com.cloud.agent.api.to.DataStoreTO; | ||
| import com.cloud.agent.api.to.DataTO; | ||
| import com.cloud.agent.transport.ArrayTypeAdaptor; | ||
| import com.cloud.agent.transport.InterfaceTypeAdaptor; | ||
| import com.cloud.agent.transport.LoggingExclusionStrategy; | ||
| import com.cloud.agent.transport.Request; | ||
| import com.cloud.agent.transport.StoragePoolTypeAdaptor; | ||
| import com.cloud.hypervisor.Hypervisor; | ||
| import com.cloud.storage.Storage; | ||
| import com.cloud.utils.Pair; | ||
| import com.cloud.utils.StringUtils; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonSerializationContext; | ||
| import com.google.gson.JsonSerializer; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import org.apache.cloudstack.transport.HypervisorTypeAdaptor; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.logging.log4j.LogManager; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| 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) | ||
| */ | ||
| public class AbstractTOAdaptor<T> implements JsonSerializer<T> { | ||
| private static final Logger LOGGER = LogManager.getLogger(AbstractTOAdaptor.class); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small one: the blank line between the javadoc and the class declaration means it is not picked up as javadoc. Can we remove it? |
||
| private static final Gson gson; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this class be declared |
||
|
|
||
| static { | ||
| GsonBuilder gsonBuilder = new GsonBuilder(); | ||
| gson = setDefaultGsonConfig(gsonBuilder); | ||
| GsonBuilder loggerBuilder = new GsonBuilder(); | ||
| loggerBuilder.disableHtmlEscaping(); | ||
| loggerBuilder.setExclusionStrategies(new LoggingExclusionStrategy(LOGGER)); | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like |
||
|
|
||
| private Map<String, String> fieldMappings; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be |
||
|
|
||
| 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"); | ||
| } | ||
| } | ||
|
|
||
| private static Gson setDefaultGsonConfig(GsonBuilder builder) { | ||
| builder.setVersion(1.5); | ||
| InterfaceTypeAdaptor<DataStoreTO> dsAdaptor = new InterfaceTypeAdaptor<DataStoreTO>(); | ||
| builder.registerTypeAdapter(DataStoreTO.class, dsAdaptor); | ||
| InterfaceTypeAdaptor<DataTO> dtAdaptor = new InterfaceTypeAdaptor<DataTO>(); | ||
| builder.registerTypeAdapter(DataTO.class, dtAdaptor); | ||
| ArrayTypeAdaptor<Command> cmdAdaptor = new ArrayTypeAdaptor<Command>(); | ||
| builder.registerTypeAdapter(Command[].class, cmdAdaptor); | ||
| ArrayTypeAdaptor<Answer> ansAdaptor = new ArrayTypeAdaptor<Answer>(); | ||
| builder.registerTypeAdapter(Answer[].class, ansAdaptor); | ||
| builder.registerTypeAdapter(new TypeToken<List<SecStorageFirewallCfgCommand.PortConfig>>() { | ||
| }.getType(), new Request.PortConfigListTypeAdaptor()); | ||
| builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() { | ||
| }.getType(), new Request.NwGroupsCommandTypeAdaptor()); | ||
| builder.registerTypeAdapter(Storage.StoragePoolType.class, new StoragePoolTypeAdaptor()); | ||
| builder.registerTypeAdapter(Hypervisor.HypervisorType.class, new HypervisorTypeAdaptor()); | ||
|
|
||
| Gson gson = builder.create(); | ||
| dsAdaptor.initGson(gson); | ||
| dtAdaptor.initGson(gson); | ||
| cmdAdaptor.initGson(gson); | ||
| ansAdaptor.initGson(gson); | ||
| return gson; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { | ||
| if (src == null) { | ||
| return null; | ||
| } | ||
| JsonObject obj = gson.toJsonTree(src).getAsJsonObject(); | ||
| if (obj != null) { | ||
| 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 obj; | ||
| } | ||
| } | ||
| 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"); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.