Skip to content
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

ConfigConverter and ConfigSource implementations for the service layer #6107

Merged
merged 13 commits into from
Apr 28, 2023
Merged
1 change: 1 addition & 0 deletions hedera-node/hedera-app-spi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
api(project(":hedera-node:hapi"))
compileOnlyApi(libs.spotbugs.annotations)

testRuntimeOnly(libs.swirlds.config.impl)
testImplementation(testLibs.bundles.testing)
testCompileOnly(libs.spotbugs.annotations)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;

/**
* Abstract class to support {@link ConfigConverter} for {@link Enum} types.
*
* @param <E> type of the enum
*/
public abstract class AbstractEnumConfigConverter<E extends Enum<E>> implements ConfigConverter<E> {

@Override
public E convert(final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
final Class<E> enumType = getEnumType();
Objects.requireNonNull(enumType, "enumType");
return Enum.valueOf(enumType, value);
}

/**
* Returns the {@link Class} of the {@link Enum} type.
*
* @return the {@link Class} of the {@link Enum} type
*/
@NonNull
protected abstract Class<E> getEnumType();
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.hapi.node.base.AccountID;
import com.swirlds.config.api.converter.ConfigConverter;
import java.util.stream.Stream;

/**
* Config api {@link ConfigConverter} implementation for the type {@link AccountID}.
*/
public class AccountIDConverter implements ConfigConverter<AccountID> {

@Override
public AccountID convert(final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
try {
final long[] nums =
Stream.of(value.split("[.]")).mapToLong(Long::parseLong).toArray();
if (nums.length != 3) {
throw new IllegalArgumentException("Does not match pattern 'A.B.C'");
}
if (nums[0] < 0) {
throw new IllegalArgumentException("Shared num of value is negative");
}
if (nums[1] < 0) {
throw new IllegalArgumentException("Realm num of value is negative");
}
if (nums[2] < 0) {
throw new IllegalArgumentException("Account num of value is negative");
}
return AccountID.newBuilder()
.shardNum(nums[0])
.realmNum(nums[1])
.accountNum(nums[2])
.build();
} catch (final Exception e) {
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
throw new IllegalArgumentException("'" + value + "' can not be parsed to " + AccountID.class.getName(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.hapi.node.base.ContractID;
import com.swirlds.config.api.converter.ConfigConverter;
import java.util.stream.Stream;

/**
* Config api {@link ConfigConverter} implementation for the type {@link ContractID}.
*/
public class ContractIDConverter implements ConfigConverter<ContractID> {

@Override
public ContractID convert(final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
try {
final long[] nums =
Stream.of(value.split("[.]")).mapToLong(Long::valueOf).toArray();
if (nums.length != 3) {
throw new IllegalArgumentException("Does not match pattern 'A.B.C'");
}
if (nums[0] < 0) {
throw new IllegalArgumentException("Shared num of value is negative");
}
if (nums[1] < 0) {
throw new IllegalArgumentException("Realm num of value is negative");
}
if (nums[2] < 0) {
throw new IllegalArgumentException("Account num of value is negative");
}
return ContractID.newBuilder()
.shardNum(nums[0])
.realmNum(nums[1])
.contractNum(nums[2])
.build();
} catch (final Exception e) {
throw new IllegalArgumentException("'" + value + "' can not be parsed to " + ContractID.class.getName(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.hapi.node.base.FileID;
import com.swirlds.config.api.converter.ConfigConverter;
import java.util.stream.Stream;

/**
* Config api {@link ConfigConverter} implementation for the type {@link FileID}.
*/
public class FileIDConverter implements ConfigConverter<FileID> {

@Override
public FileID convert(final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}
try {
final long[] nums =
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
Stream.of(value.split("[.]")).mapToLong(Long::parseLong).toArray();
if (nums.length != 3) {
throw new IllegalArgumentException("Does not match pattern 'A.B.C'");
}
if (nums[0] < 0) {
throw new IllegalArgumentException("Shared num of value is negative");
}
if (nums[1] < 0) {
throw new IllegalArgumentException("Realm num of value is negative");
}
if (nums[2] < 0) {
throw new IllegalArgumentException("File num of value is negative");
}
return FileID.newBuilder()
.shardNum(nums[0])
.realmNum(nums[1])
.fileNum(nums[2])
.build();
} catch (final Exception e) {
throw new IllegalArgumentException("'" + value + "' can not be parsed to " + FileID.class.getName(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.hapi.node.base.HederaFunctionality;
import com.swirlds.config.api.converter.ConfigConverter;

/**
* Config api {@link ConfigConverter} implementation for the type {@link HederaFunctionality}. Based on
* https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the
* class for now.
*/
public class HederaFunctionalityConverter extends AbstractEnumConfigConverter<HederaFunctionality>
implements ConfigConverter<HederaFunctionality> {
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved

@Override
protected Class<HederaFunctionality> getEnumType() {
return HederaFunctionality.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.node.app.spi.config.Profile;
import com.swirlds.config.api.converter.ConfigConverter;

/**
* Config api {@link ConfigConverter} implementation for the type {@link Profile}.
*/
public class ProfileConverter implements ConfigConverter<Profile> {

@Override
public Profile convert(final String value) throws IllegalArgumentException, NullPointerException {
if (value == null) {
throw new NullPointerException("null can not be converted");
}

try {
final int i = Integer.parseInt(value);
if (i == 0) {
hendrikebbers marked this conversation as resolved.
Show resolved Hide resolved
return Profile.DEV;
} else if (i == 1) {
return Profile.PROD;
} else if (i == 2) {
return Profile.TEST;
}
} catch (final Exception e) {
// ignore
}

return Profile.valueOf(value.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed 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.hedera.node.app.spi.config.converter;

import com.hedera.hapi.streams.SidecarType;
import com.swirlds.config.api.converter.ConfigConverter;
import edu.umd.cs.findbugs.annotations.NonNull;

/**
* Config api {@link ConfigConverter} implementation for the type {@link SidecarType}. Based on
* https://github.com/hashgraph/hedera-services/issues/6106 we need to add {@code implements ConfigConverter} to the
* class for now.
*/
public class SidecarTypeConverter extends AbstractEnumConfigConverter<SidecarType>
implements ConfigConverter<SidecarType> {

@NonNull
@Override
protected Class<SidecarType> getEnumType() {
return SidecarType.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
exports com.hedera.node.app.spi.validation;
exports com.hedera.node.app.spi.accounts;
exports com.hedera.node.app.spi.info;
exports com.hedera.node.app.spi.config.converter;
}