diff options
author | liamfallon <liam.fallon@est.tech> | 2020-03-05 10:50:48 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-03-09 18:30:13 +0000 |
commit | 6bda5d7e4e80d1aefb4f19203361b7199448e70f (patch) | |
tree | b28a68625cec91c57f0383d185f73fdf165ab558 /model/utilities/src | |
parent | f876b34ec297495df20394c248b899d610e1bcce (diff) |
Use Rhino javascript executor in apex-pdp
Integation unit tests resultd in StackOverflowException errors in the
Graal Javascript interpreter. Following extensive testing and
trouobleshooting, it proved very difficult to fix these issues in Graal
as the stck overflow errors were occurring in native class instantiation
methods being invoked by Grall on the JVM.
The alternative Rhino Javascript engine is developed by mozilla, and was
incorporated into the Java 6 JVM and evolved into Nashorn in the Java 8
JVM. Oracle dropped Nashorn in Java 11. However, in parallel, Rhino
development has continued.
This review brings in the Rhino javascript engine into apex-pdp as the
replacement for Nashorn and instead of Graal. Graal seems to be pretty
unstable as yet so we may bring it in in future releases but for now
Rhino is a more stable and reliable alternative.
Issue-ID: POLICY-2106
Change-Id: I0edeff3b0bee404b38e3ebe22001a6e3375a44dc
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'model/utilities/src')
-rw-r--r-- | model/utilities/src/main/antlr4/org/onap/policy/apex/model/utilities/typeutils/ParametrizedType.g4 | 64 | ||||
-rw-r--r-- | model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/ClassBuilder.java | 103 | ||||
-rw-r--r-- | model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/TypeBuilder.java | 115 | ||||
-rw-r--r-- | model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/package-info.java | 26 | ||||
-rw-r--r-- | model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java (renamed from model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyComparerTest.java) | 14 | ||||
-rw-r--r-- | model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java (renamed from model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyedMapComparerTest.java) | 19 | ||||
-rw-r--r-- | model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java | 101 |
7 files changed, 18 insertions, 424 deletions
diff --git a/model/utilities/src/main/antlr4/org/onap/policy/apex/model/utilities/typeutils/ParametrizedType.g4 b/model/utilities/src/main/antlr4/org/onap/policy/apex/model/utilities/typeutils/ParametrizedType.g4 deleted file mode 100644 index 384c37b0a..000000000 --- a/model/utilities/src/main/antlr4/org/onap/policy/apex/model/utilities/typeutils/ParametrizedType.g4 +++ /dev/null @@ -1,64 +0,0 @@ -//------------------------------------------------------------------------------- -// ============LICENSE_START======================================================= -// Copyright (C) 2016-2018 Ericsson. 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. -// 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. -// -// SPDX-License-Identifier: Apache-2.0 -// ============LICENSE_END========================================================= -//------------------------------------------------------------------------------- - -grammar ParametrizedType; - -@header { -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ -} - -type returns[ClassBuilder value] - : cls=CLASS { $value = ClassBuilder.parse($cls.text); } - | cls=CLASS { $value = ClassBuilder.parse($cls.text); } - LT head=type { $value.add($head.value); } - (COMMA tail=type { $value.add($tail.value); })* GT - ; - -GT : '>' - ; - -LT : '<' - ; - -COMMA - : ',' - ; - -CLASS - : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'$'|'.'|'_')* - ; diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/ClassBuilder.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/ClassBuilder.java deleted file mode 100644 index 8d1ed9497..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/ClassBuilder.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.model.utilities.typeutils; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This class is a utility class that builds a class with a set of user defined fields. It is used to get the Type of - * fields in Java schemas<br> For more information see:<br> <a - * href="http://stackoverflow.com/questions/39401083/class-forname-equivalent-for-creating-parameterizedtypes-from-string"> - * http://stackoverflow.com/questions/39401083/class-forname-equivalent-for-creating-parameterizedtypes-from-string</a><br> - * <a href="https://github.com/KetothXupack/stackoverflow-answers/tree/master/q39401083"> - * https://github.com/KetothXupack/stackoverflow-answers/tree/master/q39401083</a><br> - */ -public class ClassBuilder { - // Logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ClassBuilder.class); - - private final Class<?> clazz; - private final List<ClassBuilder> parameters = new ArrayList<>(); - - /** - * Constructor that sets the class for the class builder. - * - * @param clazz the class of the class builder - */ - public ClassBuilder(final Class<?> clazz) { - this.clazz = clazz; - } - - /** - * Creates a {@link ClassBuilder} instance for a class with the given class name. - * - * @param className the class name of the class builder to create - * @return the class builder that is created - */ - public static ClassBuilder parse(final String className) { - try { - return new ClassBuilder(Class.forName(className)); - } catch (ClassNotFoundException e) { - try { - return new ClassBuilder(Class.forName("java.lang." + className)); - } catch (Exception classFindException) { - LOGGER.warn("class not found", classFindException); - throw new IllegalArgumentException("Class '" + className - + "' not found. Also looked for a class called 'java.lang." + className + "'", e); - } - } - } - - /** - * Adds a field to the class builder. Each field is itself a class builder. - * - * @param fieldBuilder the class builder for the field - */ - public void add(final ClassBuilder fieldBuilder) { - parameters.add(fieldBuilder); - } - - /** - * Builds the {@link Type} of the class. - * - * @return the {@link Type} of the class - */ - public Type build() { - // class is not parameterized - if (parameters.isEmpty()) { - return clazz; - } - Type[] paramTypes = new Type[parameters.size()]; - int paramTypeIndex = 0; - for (ClassBuilder classBuilder : parameters) { - paramTypes[paramTypeIndex++] = classBuilder.build(); - } - // TODO: Fix this for parameterized types if needed or adapt to work with generic types only - // return ParameterizedTypeImpl.make(clazz, paramTypes, null); - return null; - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/TypeBuilder.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/TypeBuilder.java deleted file mode 100644 index 5f75bca1c..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/TypeBuilder.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.model.utilities.typeutils; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import org.antlr.v4.runtime.BailErrorStrategy; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CharStreams; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.TokenStream; - -/** - * This class builds a type from a grammar using ANTLR. - */ -public final class TypeBuilder { - /** - * Private constructor used to prevent sub class instantiation. - */ - private TypeBuilder() { - } - - /** - * Builds the Type of the Type string that was input. - * - * @param type the java Type as a string - * @return the Type of the string that was input - */ - public static Type build(final String type) { - if (type == null || type.length() == 0) { - throw new IllegalArgumentException( - "Blank type string passed to " + TypeBuilder.class.getName() + ".build(String type)"); - } - - try { - final CharStream stream = CharStreams.fromString(type); - final TokenStream tokenStream = new CommonTokenStream(new ParametrizedTypeLexer(stream)); - - final ParametrizedTypeParser parser = new ParametrizedTypeParser(tokenStream); - parser.removeErrorListeners(); - parser.setErrorHandler(new BailErrorStrategy()); - parser.setBuildParseTree(true); - return parser.type().value.build(); - } catch (final Exception e) { - throw new IllegalArgumentException("Failed to build type '" + type + "': " + e, e); - } - } - - /** - * Gets the class of Java Type. - * - * @param type the java Type as a string - * @return the java Type - */ - public static Class<?> getJavaTypeClass(final String type) { - return getJavaTypeClass(build(type)); - } - - /** - * Gets the class of Java Type. - * - * @param type the java Type as a Type - * @return the java Type - */ - public static Class<?> getJavaTypeClass(final Type type) { - if (type instanceof Class<?>) { - return (Class<?>) type; - } else if (type instanceof ParameterizedType) { - final Type raw = ((ParameterizedType) type).getRawType(); - if (!(raw instanceof Class<?>)) { - throw new IllegalArgumentException("The Parameterised javatype " + type + " with base type " + raw - + " is not a Java 'Class' that can be instantiated"); - } - return (Class<?>) raw; - } - throw new IllegalArgumentException( - "The Parameterised javatype " + type + " is not a Java 'Type' that has a 'Class'"); - } - - /** - * Gets the parameters of a Java Type. - * - * @param type the Java Type - * @return the parameters of the java Type - */ - public static Type[] getJavaTypeParameters(final Type type) { - if (type instanceof Class<?>) { - return new Type[0]; - } else if (type instanceof ParameterizedType) { - return ((ParameterizedType) type).getActualTypeArguments(); - } - throw new IllegalArgumentException( - "\"The Parameterised javatype \" + type + \" is not a Java 'Type' that has parameter types"); - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/package-info.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/package-info.java deleted file mode 100644 index ed4b079bf..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/typeutils/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -/** - * Provides utility classes that operate on and work with Java Types. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -package org.onap.policy.apex.model.utilities.typeutils; diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyComparerTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java index 45afc2088..4cf91bd39 100644 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyComparerTest.java +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java @@ -1,24 +1,25 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.model.utilities.typeutils; +package org.onap.policy.apex.model.utilities; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -29,6 +30,7 @@ import org.onap.policy.apex.model.utilities.comparison.KeyDifference; /** * Test key comparisons. + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class KeyComparerTest { @@ -36,14 +38,14 @@ public class KeyComparerTest { @Test public void test() { KeyDifference<String> keyDifference = new KeyComparer<String>().compareKeys("Hello", "Goodbye"); - + assertFalse(keyDifference.isEqual()); assertTrue("Hello".equals(keyDifference.getLeftKey().toString())); assertTrue("Goodbye".equals(keyDifference.getRightKey().toString())); assertTrue("left key Hello and right key Goodbye differ\n".equals(keyDifference.asString(true))); assertTrue("left key Hello and right key Goodbye differ\n".equals(keyDifference.asString(false))); - + KeyDifference<String> keyDifference2 = new KeyComparer<String>().compareKeys("Here", "Here"); assertTrue("".equals(keyDifference2.asString(true))); assertTrue("left key Here equals right key Here\n".equals(keyDifference2.asString(false))); diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyedMapComparerTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java index 7fb3a5bb7..fb32d9383 100644 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/KeyedMapComparerTest.java +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java @@ -1,24 +1,25 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.model.utilities.typeutils; +package org.onap.policy.apex.model.utilities; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -32,7 +33,7 @@ import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference; /** * Test key map comparisons. - * + * * @author Liam Fallon (liam.fallon@ericsson.com) */ public class KeyedMapComparerTest { @@ -53,10 +54,10 @@ public class KeyedMapComparerTest { rightMap.put("F", "FFFFF"); rightMap.put("G", "G"); - KeyedMapDifference<String, String> kmComparedSame = new KeyedMapComparer<String, String>().compareMaps(leftMap, - leftMap); - KeyedMapDifference<String, String> kmComparedDiff = new KeyedMapComparer<String, String>().compareMaps(leftMap, - rightMap); + KeyedMapDifference<String, String> kmComparedSame = + new KeyedMapComparer<String, String>().compareMaps(leftMap, leftMap); + KeyedMapDifference<String, String> kmComparedDiff = + new KeyedMapComparer<String, String>().compareMaps(leftMap, rightMap); assertTrue(kmComparedSame.getIdenticalValues().equals(leftMap)); assertEquals(1, kmComparedDiff.getLeftOnly().size()); diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java deleted file mode 100644 index 2faa8753f..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.model.utilities.typeutils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.lang.reflect.Type; - -import org.antlr.v4.runtime.BailErrorStrategy; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CharStreams; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.TokenStream; -import org.junit.Ignore; -import org.junit.Test; - -/** - * Test Java parsing. - */ -public class ParserTest { - // TODO: Fix this test based on the decision around prameterized vs generic types - @Ignore - @Test - public void testParser() { - final CharStream stream = CharStreams - .fromString("java.util.Map<java.util.List<java.lang.Integer>,java.util.Set<java.lang.String>>"); - final TokenStream tokenStream = new CommonTokenStream(new ParametrizedTypeLexer(stream)); - - final ParametrizedTypeParser parser = new ParametrizedTypeParser(tokenStream); - parser.removeErrorListeners(); - parser.setErrorHandler(new BailErrorStrategy()); - parser.setBuildParseTree(true); - assertEquals("java.util.Map<java.util.List<java.lang.Integer>, java.util.Set<java.lang.String>>", - parser.type().value.build().getTypeName()); - - } - - // TODO: Fix this test based on the decision around prameterized vs generic types - @Ignore - @Test - public void testBuilder() throws IllegalArgumentException { - String typeString = "java.lang.Integer"; - Type ret = TypeBuilder.build(typeString); - assertEquals(java.lang.Integer.class, TypeBuilder.getJavaTypeClass(ret)); - - typeString = "java.util.Map<java.util.List<java.lang.Integer>,java.util.Set<java.lang.String>>"; - ret = TypeBuilder.build(typeString); - assertEquals("java.util.Map<java.util.List<java.lang.Integer>, java.util.Set<java.lang.String>>", - ret.getTypeName()); - assertEquals(java.util.Map.class, TypeBuilder.getJavaTypeClass(ret)); - - final Type[] args = TypeBuilder.getJavaTypeParameters(ret); - assertEquals("java.util.List<java.lang.Integer>", args[0].getTypeName()); - assertEquals("java.util.Set<java.lang.String>", args[1].getTypeName()); - } - - @Test - public void testBoundaryConditions() { - try { - TypeBuilder.build(null); - fail("Test should throw exception"); - } catch (final IllegalArgumentException e) { - assertEquals( - "Blank type string passed to " - + "org.onap.policy.apex.model.utilities.typeutils.TypeBuilder.build(String type)", - e.getMessage()); - } - - try { - TypeBuilder.build("org.zooby.Wooby"); - fail("Test should throw exception"); - } catch (final IllegalArgumentException e) { - assertEquals(e.getMessage(), - "Failed to build type 'org.zooby.Wooby': java.lang.IllegalArgumentException: " - + "Class 'org.zooby.Wooby' not found. " - + "Also looked for a class called 'java.lang.org.zooby.Wooby'"); - } - - assertEquals(TypeBuilder.getJavaTypeClass("java.lang.String"), String.class); - } -} |