Skip to content

[Default Configuration Part1]: Add defaults mode configuration #2781

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

Merged
merged 1 commit into from
Oct 21, 2021
Merged
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,65 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.lite.maven.plugin;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import software.amazon.awssdk.codegen.lite.CodeGenerator;
import software.amazon.awssdk.codegen.lite.defaultsmode.DefaultConfiguration;
import software.amazon.awssdk.codegen.lite.defaultsmode.DefaultsLoader;
import software.amazon.awssdk.codegen.lite.defaultsmode.DefaultsModeGenerator;

/**
* The Maven mojo to generate defaults mode related classes.
*/
@Mojo(name = "generate-defaults-mode")
public class DefaultsModeGenerationMojo extends AbstractMojo {

private static final String DEFAULTS_MODE_BASE = "software.amazon.awssdk.defaultsmode";

@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
private String outputDirectory;

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;

@Parameter(property = "defaultConfigurationFile", defaultValue =
"${basedir}/src/main/resources/software/amazon/awssdk/internal/defaults/sdk-default-configuration.json")
private File defaultConfigurationFile;

public void execute() {
Path baseSourcesDirectory = Paths.get(outputDirectory).resolve("generated-sources").resolve("sdk");
Path testsDirectory = Paths.get(outputDirectory).resolve("generated-test-sources").resolve("sdk-tests");

DefaultConfiguration configuration = DefaultsLoader.load(defaultConfigurationFile);

generateDefaultsModeClass(baseSourcesDirectory, configuration);

project.addCompileSourceRoot(baseSourcesDirectory.toFile().getAbsolutePath());
project.addTestCompileSourceRoot(testsDirectory.toFile().getAbsolutePath());
}

public void generateDefaultsModeClass(Path baseSourcesDirectory, DefaultConfiguration configuration) {
Path sourcesDirectory = baseSourcesDirectory.resolve(DEFAULTS_MODE_BASE.replace(".", "/"));
new CodeGenerator(sourcesDirectory.toString(), new DefaultsModeGenerator(DEFAULTS_MODE_BASE, configuration)).generate();
}

}
5 changes: 5 additions & 0 deletions codegen-lite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<artifactId>utils</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>json-utils</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.lite.defaultsmode;

import java.util.Map;

/**
* Container for default configuration
*/
public class DefaultConfiguration {
/**
* The transformed configuration values for each mode
*/
private Map<String, Map<String, String>> modeDefaults;

/**
* The documentation for each mode
*/
private Map<String, String> modesDocumentation;

/*
* The documentation for each configuration option
*/
private Map<String, String> configurationDocumentation;

public Map<String, Map<String, String>> modeDefaults() {
return modeDefaults;
}

public DefaultConfiguration modeDefaults(Map<String, Map<String, String>> modeDefaults) {
this.modeDefaults = modeDefaults;
return this;
}

public Map<String, String> modesDocumentation() {
return modesDocumentation;
}

public DefaultConfiguration modesDocumentation(Map<String, String> documentation) {
this.modesDocumentation = documentation;
return this;
}

public Map<String, String> configurationDocumentation() {
return configurationDocumentation;
}

public DefaultConfiguration configurationDocumentation(Map<String, String> configurationDocumentation) {
this.configurationDocumentation = configurationDocumentation;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.lite.defaultsmode;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeVisitor;
import software.amazon.awssdk.utils.Logger;

/**
* Loads sdk-default-configuration.json into memory. It filters out unsupported configuration options from the file
*/
@SdkInternalApi
public final class DefaultsLoader {
private static final Logger log = Logger.loggerFor(DefaultsLoader.class);

private static final Set<String> UNSUPPORTED_OPTIONS = new HashSet<>();

static {
UNSUPPORTED_OPTIONS.add("stsRegionalEndpoints");
UNSUPPORTED_OPTIONS.add("tlsNegotiationTimeoutInMillis");
}

private DefaultsLoader() {
}

public static DefaultConfiguration load(File path) {
return loadDefaultsFromFile(path);
}

private static DefaultConfiguration loadDefaultsFromFile(File path) {
DefaultConfiguration defaultsResolution = new DefaultConfiguration();
Map<String, Map<String, String>> resolvedDefaults = new HashMap<>();

try (FileInputStream fileInputStream = new FileInputStream(path)) {
JsonNodeParser jsonNodeParser = JsonNodeParser.builder().build();

Map<String, JsonNode> sdkDefaultConfiguration = jsonNodeParser.parse(fileInputStream)
.asObject();

Map<String, JsonNode> base = sdkDefaultConfiguration.get("base").asObject();
Map<String, JsonNode> modes = sdkDefaultConfiguration.get("modes").asObject();

modes.forEach((mode, modifiers) -> applyModificationToOneMode(resolvedDefaults, base, mode, modifiers));

Map<String, JsonNode> documentation = sdkDefaultConfiguration.get("documentation").asObject();
Map<String, JsonNode> modesDocumentation = documentation.get("modes").asObject();
Map<String, JsonNode> configDocumentation = documentation.get("configuration").asObject();

defaultsResolution.modesDocumentation(
modesDocumentation.entrySet()
.stream()
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue().asString()), Map::putAll));
defaultsResolution.configurationDocumentation(
configDocumentation.entrySet()
.stream()
.filter(e -> !UNSUPPORTED_OPTIONS.contains(e.getKey()))
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue().asString()), Map::putAll));

} catch (IOException e) {
throw new RuntimeException(e);
}

defaultsResolution.modeDefaults(resolvedDefaults);

return defaultsResolution;
}

private static void applyModificationToOneConfigurationOption(Map<String, String> resolvedDefaultsForCurrentMode,
String option,
JsonNode modifier) {
String resolvedValue;
String baseValue = resolvedDefaultsForCurrentMode.get(option);

if (UNSUPPORTED_OPTIONS.contains(option)) {
return;
}

Map<String, JsonNode> modifierMap = modifier.asObject();

if (modifierMap.size() != 1) {
throw new IllegalStateException("More than one modifier exists for option " + option);
}

String modifierString = modifierMap.keySet().iterator().next();

switch (modifierString) {
case "override":
resolvedValue = modifierMap.get("override").visit(new StringJsonNodeVisitor());
break;
case "multiply":
resolvedValue = processMultiply(baseValue, modifierMap);
break;
case "add":
resolvedValue = processAdd(baseValue, modifierMap);
break;
default:
throw new UnsupportedOperationException("Unsupported modifier: " + modifierString);
}

resolvedDefaultsForCurrentMode.put(option, resolvedValue);
}

private static void applyModificationToOneMode(Map<String, Map<String, String>> resolvedDefaults,
Map<String, JsonNode> base,
String mode,
JsonNode modifiers) {

log.info(() -> "Apply modification for mode: " + mode);
Map<String, String> resolvedDefaultsForCurrentMode =
base.entrySet().stream().filter(e -> !UNSUPPORTED_OPTIONS.contains(e.getKey()))
.collect(HashMap::new, (m, e) -> m.put(e.getKey(),
e.getValue().visit(new StringJsonNodeVisitor())), Map::putAll);


// Iterate the configuration options and apply modification.
modifiers.asObject().forEach((option, modifier) -> applyModificationToOneConfigurationOption(
resolvedDefaultsForCurrentMode, option, modifier));

resolvedDefaults.put(mode, resolvedDefaultsForCurrentMode);
}

private static String processAdd(String baseValue, Map<String, JsonNode> modifierMap) {
String resolvedValue;
String add = modifierMap.get("add").asNumber();
int parsedAdd = Integer.parseInt(add);
int number = Math.addExact(Integer.parseInt(baseValue), parsedAdd);
resolvedValue = String.valueOf(number);
return resolvedValue;
}

private static String processMultiply(String baseValue, Map<String, JsonNode> modifierMap) {
String resolvedValue;
String multiply = modifierMap.get("multiply").asNumber();
double parsedValue = Double.parseDouble(multiply);

double resolvedNumber = Integer.parseInt(baseValue) * parsedValue;
int castValue = (int) resolvedNumber;

if (castValue != resolvedNumber) {
throw new IllegalStateException("The transformed value must be be a float number: " + castValue);
}

resolvedValue = String.valueOf(castValue);
return resolvedValue;
}

private static final class StringJsonNodeVisitor implements JsonNodeVisitor<String> {
@Override
public String visitNull() {
throw new IllegalStateException("Invalid type encountered");
}

@Override
public String visitBoolean(boolean b) {
throw new IllegalStateException("Invalid type (boolean) encountered " + b);
}

@Override
public String visitNumber(String s) {
return s;
}

@Override
public String visitString(String s) {
return s;
}

@Override
public String visitArray(List<JsonNode> list) {
throw new IllegalStateException("Invalid type (list) encountered: " + list);
}

@Override
public String visitObject(Map<String, JsonNode> map) {
throw new IllegalStateException("Invalid type (map) encountered: " + map);
}

@Override
public String visitEmbeddedObject(Object o) {
throw new IllegalStateException("Invalid type (embedded) encountered: " + o);
}
}
}
Loading