From f8959f5c51e6338b62e23ea503eb86d9c65d7c74 Mon Sep 17 00:00:00 2001 From: ramverma Date: Wed, 25 Jul 2018 17:26:52 +0100 Subject: Renaming examples in apex-pdp Renaming the examples as per what the documentation expects them. Otherwise the documents won't work. Change-Id: Ib9e30bf5a4cec0fec981372e1d9f3a0ee5d60f2f Issue-ID: POLICY-861 Signed-off-by: ramverma --- .../adaptive/TestAnomalyDetectionDBWrite.java | 60 +++++++ .../adaptive/TestAnomalyDetectionModel.java | 81 +++++++++ .../adaptive/TestAnomalyDetectionModelCreator.java | 56 ++++++ .../adaptive/TestAnomalyDetectionTSLUseCase.java | 164 ++++++++++++++++++ .../examples/adaptive/TestApexActionListener.java | 91 ++++++++++ .../examples/adaptive/TestAutoLearnDBWrite.java | 60 +++++++ .../apex/examples/adaptive/TestAutoLearnModel.java | 81 +++++++++ .../adaptive/TestAutoLearnModelCreator.java | 56 ++++++ .../examples/adaptive/TestAutoLearnTSLUseCase.java | 187 +++++++++++++++++++++ .../apex/examples/adaptive/package-info.java | 27 +++ .../src/test/resources/META-INF/persistence.xml | 69 ++++++++ 11 files changed, 932 insertions(+) create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDBWrite.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModelCreator.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTSLUseCase.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestApexActionListener.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDBWrite.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModelCreator.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTSLUseCase.java create mode 100644 examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/package-info.java create mode 100644 examples/examples-adaptive/src/test/resources/META-INF/persistence.xml (limited to 'examples/examples-adaptive/src/test') diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDBWrite.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDBWrite.java new file mode 100644 index 000000000..898531c32 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionDBWrite.java @@ -0,0 +1,60 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.dao.DAOParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class TestAnomalyDetectionDBWrite { + private Connection connection; + TestApexModel testApexModel; + + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelWriteReadJPA() throws Exception { + final DAOParameters daoParameters = new DAOParameters(); + daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + daoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJPA(daoParameters); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java new file mode 100644 index 000000000..7e2fb0eb7 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModel.java @@ -0,0 +1,81 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.dao.DAOParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class TestAnomalyDetectionModel { + private Connection connection; + TestApexModel testApexModel; + + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAnomalyDetectionModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelValid() throws Exception { + final AxValidationResult result = testApexModel.testApexModelValid(); + assertTrue(result.toString().equals(VALID_MODEL_STRING)); + } + + @Test + public void testModelWriteReadXML() throws Exception { + testApexModel.testApexModelWriteReadXML(); + } + + @Test + public void testModelWriteReadJSON() throws Exception { + testApexModel.testApexModelWriteReadJSON(); + } + + @Test + public void testModelWriteReadJPA() throws Exception { + final DAOParameters daoParameters = new DAOParameters(); + daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + daoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJPA(daoParameters); + } + + private static final String VALID_MODEL_STRING = "***validation of model successful***"; +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModelCreator.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModelCreator.java new file mode 100644 index 000000000..2b50d69ab --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionModelCreator.java @@ -0,0 +1,56 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestAnomalyDetectionModelCreator implements TestApexModelCreator { + + @Override + public AxPolicyModel getModel() { + return new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); + } + + @Override + public AxPolicyModel getMalstructuredModel() { + return null; + } + + @Override + public AxPolicyModel getObservationModel() { + return null; + } + + @Override + public AxPolicyModel getWarningModel() { + return getModel(); + } + + @Override + public AxPolicyModel getInvalidModel() { + return null; + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTSLUseCase.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTSLUseCase.java new file mode 100644 index 000000000..7d9791d46 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAnomalyDetectionTSLUseCase.java @@ -0,0 +1,164 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Random; + +import org.junit.Test; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.core.engine.engine.ApexEngine; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; +import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This policy passes, and recieves a Double event context filed called "EVCDouble".
+ * The policy tries to detect anomalies in the pattern of values for EVCDouble
+ * See the 2 test cases below (1 short, 1 long) + * + * @author John Keeney (John.Keeney@ericsson.com) + */ +public class TestAnomalyDetectionTSLUseCase { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestAnomalyDetectionTSLUseCase.class); + + private static final int MAXITERATIONS = 3660; + private static final Random RAND = new Random(System.currentTimeMillis()); + + @Test + // once through the long running test below + public void TestAnomalyDetectionTSL() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + final EnEvent triggerEvent = + apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); + final double rval = RAND.nextGaussian(); + triggerEvent.put("Iteration", 0); + triggerEvent.put("MonitoredValue", rval); + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + assertEquals("ExecutionIDs are different", triggerEvent.getExecutionID(), result.getExecutionID()); + triggerEvent.clear(); + result.clear(); + Thread.sleep(1); + apexEngine1.stop(); + } + + /** + * This policy passes, and recieves a Double event context filed called "EVCDouble"
+ * The policy tries to detect anomalies in the pattern of values for EVCDouble
+ * This test case generates a SineWave-like pattern for the parameter, repeating every 360 iterations. (These Period + * should probably be set using TaskParameters!) Every 361st value is a random number!, so should be identified as + * an Anomaly. The policy has 3 Decide Tasks, and the Decide TaskSelectionLogic picks one depending on the + * 'Anomaliness' of the input data.
+ * To plot the results grep debug results for the string "************", paste into excel and delete non-relevant + * columns
+ * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + // Test is disabled by default. uncomment below, or execute using the main() method + // @Test + // EG Dos command: apex-core.engine> mvn + // -Dtest=org.onap.policy.apex.core.engine.ml.TestAnomalyDetectionTSLUseCase test | findstr /L /C:"Apex [main] DEBUG + // c.e.a.e.TaskSelectionExecutionLogging - + // TestAnomalyDetectionTSL_Policy0000DecideStateTaskSelectionLogic.getTask():" + public void TestAnomalyDetectionTSL_main() throws ApexException, InterruptedException, IOException { + + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAnomalyDetectionPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AnomalyTSLApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener1", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + + final EnEvent triggerEvent = + apexEngine1.createEvent(new AxArtifactKey("AnomalyDetectionTriggerEvent", "0.0.1")); + assertNotNull(triggerEvent); + + for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { + // Trigger the policy in engine 1 + + double value = (Math.sin(Math.toRadians(iteration))) + (RAND.nextGaussian() / 25.0); + // lets make every 361st number a random value to perhaps flag as an anomaly + if (((iteration + 45) % 361) == 0) { + value = (RAND.nextGaussian() * 2.0); + } + triggerEvent.put("Iteration", iteration); + triggerEvent.put("MonitoredValue", value); + LOGGER.info("Iteration " + iteration + ":\tTriggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Iteration " + iteration + ":\tReceiving action event {} ", result); + triggerEvent.clear(); + result.clear(); + } + apexEngine1.stop(); + Thread.sleep(1000); + } + + public static void main(final String[] args) throws ApexException, InterruptedException, IOException { + new TestAnomalyDetectionTSLUseCase().TestAnomalyDetectionTSL_main(); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestApexActionListener.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestApexActionListener.java new file mode 100644 index 000000000..ec7c197b7 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestApexActionListener.java @@ -0,0 +1,91 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.policy.apex.core.engine.engine.EnEventListener; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; + +/** + * The listener interface for receiving testApexAction events. The class that is interested in processing a + * testApexAction event implements this interface, and the object created with that class is registered with a component + * using the component's addTestApexActionListener method. When the testApexAction event occurs, that + * object's appropriate method is invoked. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestApexActionListener implements EnEventListener { + private List resultEvents = new ArrayList<>(); + + private final String id; + + /** + * Instantiates a new test apex action listener. + * + * @param id the id + */ + public TestApexActionListener(final String id) { + this.id = id; + } + + /** + * Gets the result. + * + * @return the result + */ + public EnEvent getResult() { + while (resultEvents.isEmpty()) { + ThreadUtilities.sleep(100); + } + return resultEvents.remove(0); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.core.engine.engine.EnEventListener#onEnEvent(org.onap.policy.apex.core.engine.event.EnEvent) + */ + @Override + public void onEnEvent(final EnEvent actionEvent) { + try { + Thread.sleep(100); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + if (actionEvent != null) { + System.out.println("Action event from engine:" + actionEvent.getName()); + resultEvents.add(actionEvent); + } + } + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + return id; + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDBWrite.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDBWrite.java new file mode 100644 index 000000000..64efa6c35 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnDBWrite.java @@ -0,0 +1,60 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.dao.DAOParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class TestAutoLearnDBWrite { + private Connection connection; + TestApexModel testApexModel; + + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelWriteReadJPA() throws Exception { + final DAOParameters daoParameters = new DAOParameters(); + daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + daoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJPA(daoParameters); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java new file mode 100644 index 000000000..aa7f621c9 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModel.java @@ -0,0 +1,81 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.dao.DAOParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class TestAutoLearnModel { + private Connection connection; + TestApexModel testApexModel; + + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel<>(AxPolicyModel.class, new TestAutoLearnModelCreator()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelValid() throws Exception { + final AxValidationResult result = testApexModel.testApexModelValid(); + assertTrue(result.toString().equals(VALID_MODEL_STRING)); + } + + @Test + public void testModelWriteReadXML() throws Exception { + testApexModel.testApexModelWriteReadXML(); + } + + @Test + public void testModelWriteReadJSON() throws Exception { + testApexModel.testApexModelWriteReadJSON(); + } + + @Test + public void testModelWriteReadJPA() throws Exception { + final DAOParameters daoParameters = new DAOParameters(); + daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + daoParameters.setPersistenceUnit("AdaptiveModelsTest"); + + testApexModel.testApexModelWriteReadJPA(daoParameters); + } + + private static final String VALID_MODEL_STRING = "***validation of model successful***"; +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModelCreator.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModelCreator.java new file mode 100644 index 000000000..11f1991bf --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnModelCreator.java @@ -0,0 +1,56 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestAutoLearnModelCreator implements TestApexModelCreator { + + @Override + public AxPolicyModel getModel() { + return new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); + } + + @Override + public AxPolicyModel getMalstructuredModel() { + return null; + } + + @Override + public AxPolicyModel getObservationModel() { + return null; + } + + @Override + public AxPolicyModel getWarningModel() { + return getModel(); + } + + @Override + public AxPolicyModel getInvalidModel() { + return null; + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTSLUseCase.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTSLUseCase.java new file mode 100644 index 000000000..88b504cef --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestAutoLearnTSLUseCase.java @@ -0,0 +1,187 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.examples.adaptive; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Random; + +import org.junit.Test; +import org.onap.policy.apex.core.engine.EngineParameters; +import org.onap.policy.apex.core.engine.engine.ApexEngine; +import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters; +import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * Test Auto learning in TSL. + * + * @author John Keeney (John.Keeney@ericsson.com) + */ +public class TestAutoLearnTSLUseCase { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(TestAutoLearnTSLUseCase.class); + + private static final int MAXITERATIONS = 1000; + private static final Random rand = new Random(System.currentTimeMillis()); + + @Test + // once through the long running test below + public void TestAutoLearnTSL() throws ApexException, InterruptedException, IOException { + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); + final double rval = rand.nextGaussian(); + triggerEvent.put("MonitoredValue", rval); + triggerEvent.put("LastMonitoredValue", 0D); + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + assertEquals("ExecutionIDs are different", triggerEvent.getExecutionID(), result.getExecutionID()); + triggerEvent.clear(); + result.clear(); + Thread.sleep(1); + apexEngine1.stop(); + } + + /** + * This policy passes, and receives a Double event context filed called "EVCDouble"
+ * The policy tries to keep the value at 50, with a Min -100, Max 100 (These should probably be set using + * TaskParameters!)
+ * The policy has 7 Decide Tasks that manipulate the value of this field in unknown ways.
+ * The Decide TSL learns the effect of each task, and then selects the appropriate task to get the value back to + * 50
+ * After the value settles close to 50 for a while, the test Rests the value to to random number and then + * continues
+ * To plot the results grep stdout debug results for the string "*******", paste into excel and delete non-relevant + * columns
+ * + * @throws ApexException the apex exception + * @throws InterruptedException the interrupted exception + * @throws IOException Signals that an I/O exception has occurred. + */ + // @Test + public void TestAutoLearnTSL_main() throws ApexException, InterruptedException, IOException { + + final double WANT = 50.0; + final double toleranceTileJump = 3.0; + + final AxPolicyModel apexPolicyModel = new AdaptiveDomainModelFactory().getAutoLearnPolicyModel(); + assertNotNull(apexPolicyModel); + + final AxValidationResult validationResult = new AxValidationResult(); + apexPolicyModel.validate(validationResult); + assertTrue(validationResult.isValid()); + + final AxArtifactKey key = new AxArtifactKey("AADMApexEngine", "0.0.1"); + final EngineParameters parameters = new EngineParameters(); + parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters()); + parameters.getExecutorParameterMap().put("JAVA", new JavaExecutorParameters()); + + final ApexEngine apexEngine1 = new ApexEngineFactory().createApexEngine(key); + + final TestApexActionListener listener1 = new TestApexActionListener("TestListener1"); + apexEngine1.addEventListener("listener1", listener1); + apexEngine1.updateModel(apexPolicyModel); + apexEngine1.start(); + + final EnEvent triggerEvent = apexEngine1.createEvent(new AxArtifactKey("AutoLearnTriggerEvent", "0.0.1")); + assertNotNull(triggerEvent); + final double MIN = -100; + final double MAX = 100; + + double rval = (((rand.nextGaussian() + 1) / 2) * (MAX - MIN)) + MIN; + triggerEvent.put("MonitoredValue", rval); + triggerEvent.put("LastMonitoredValue", 0); + + double avval = 0; + double distance; + double avcount = 0; + + for (int iteration = 0; iteration < MAXITERATIONS; iteration++) { + // Trigger the policy in engine 1 + LOGGER.info("Triggering policy in Engine 1 with " + triggerEvent); + apexEngine1.handleEvent(triggerEvent); + final EnEvent result = listener1.getResult(); + LOGGER.info("Receiving action event {} ", result); + triggerEvent.clear(); + + double val = (Double) result.get("MonitoredValue"); + final double prevval = (Double) result.get("LastMonitoredValue"); + + triggerEvent.put("MonitoredValue", prevval); + triggerEvent.put("LastMonitoredValue", val); + + avcount = Math.min((avcount + 1), 20); // maintain average of only the last 20 values + avval = ((avval * (avcount - 1)) + val) / (avcount); + + distance = Math.abs(WANT - avval); + if (distance < toleranceTileJump) { + rval = (((rand.nextGaussian() + 1) / 2) * (MAX - MIN)) + MIN; + val = rval; + triggerEvent.put("MonitoredValue", val); + LOGGER.info("Iteration " + iteration + ": Average " + avval + " has become closer (" + distance + + ") than " + toleranceTileJump + " to " + WANT + " so reseting val:\t\t\t\t\t\t\t\t" + val); + avval = 0; + avcount = 0; + } + LOGGER.info("Iteration " + iteration + ": \tpreval\t" + prevval + "\tval\t" + val + "\tavval\t" + avval); + + result.clear(); + Thread.sleep(1); + } + + apexEngine1.stop(); + Thread.sleep(1000); + + } + + public static void main(final String[] args) throws ApexException, InterruptedException, IOException { + new TestAutoLearnTSLUseCase().TestAutoLearnTSL_main(); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/package-info.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/package-info.java new file mode 100644 index 000000000..0e004fe8d --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/package-info.java @@ -0,0 +1,27 @@ +/*- + * ============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========================================================= + */ + +/** + * Defines domain and policies demonstrating adaptive policies. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.examples.adaptive; diff --git a/examples/examples-adaptive/src/test/resources/META-INF/persistence.xml b/examples/examples-adaptive/src/test/resources/META-INF/persistence.xml new file mode 100644 index 000000000..7282bb562 --- /dev/null +++ b/examples/examples-adaptive/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,69 @@ + + + + + + org.eclipse.persistence.jpa.PersistenceProvider + + org.onap.policy.apex.model.basicmodel.dao.converters.CDATAConditioner + org.onap.policy.apex.model.basicmodel.dao.converters.UUID2String + org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey + org.onap.policy.apex.model.basicmodel.concepts.AxConcept + org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo + org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation + org.onap.policy.apex.model.basicmodel.concepts.AxModel + org.onap.policy.apex.model.basicmodel.concepts.TestEntity + org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema + org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas + org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum + org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums + org.onap.policy.apex.model.contextmodel.concepts.AxContextModel + org.onap.policy.apex.model.eventmodel.concepts.AxField + org.onap.policy.apex.model.eventmodel.concepts.AxInputField + org.onap.policy.apex.model.eventmodel.concepts.AxOutputField + org.onap.policy.apex.model.eventmodel.concepts.AxEvent + org.onap.policy.apex.model.eventmodel.concepts.AxEvents + org.onap.policy.apex.model.eventmodel.concepts.AxEventModel + org.onap.policy.apex.model.policymodel.concepts.AxLogic + org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter + org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic + org.onap.policy.apex.model.policymodel.concepts.AxTask + org.onap.policy.apex.model.policymodel.concepts.AxTasks + org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic + org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic + org.onap.policy.apex.model.policymodel.concepts.AxStateOutput + org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference + org.onap.policy.apex.model.policymodel.concepts.AxState + org.onap.policy.apex.model.policymodel.concepts.AxPolicy + org.onap.policy.apex.model.policymodel.concepts.AxPolicies + org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel + + + + + + + + + + + + -- cgit 1.2.3-korg