From 10d58cde1f75ea0ddf0cdc0746d858e0a077980d Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 29 May 2018 15:25:49 +0100 Subject: Adding apex context module Change-Id: I1284c2ba4c41a9cf721e141e096d1021be599a2d Issue-ID: POLICY-857 Signed-off-by: ramverma --- context/context-management/pom.xml | 49 +++ .../org/onap/policy/apex/context/ContextAlbum.java | 127 ++++++ .../onap/policy/apex/context/ContextException.java | 51 +++ .../apex/context/ContextRuntimeException.java | 51 +++ .../org/onap/policy/apex/context/Distributor.java | 140 ++++++ .../org/onap/policy/apex/context/LockManager.java | 88 ++++ .../org/onap/policy/apex/context/Persistor.java | 79 ++++ .../org/onap/policy/apex/context/SchemaHelper.java | 119 +++++ .../policy/apex/context/impl/ContextAlbumImpl.java | 486 ++++++++++++++++++++ .../impl/distribution/AbstractDistributor.java | 326 ++++++++++++++ .../impl/distribution/DistributorFactory.java | 86 ++++ .../distribution/DistributorFlushTimerTask.java | 116 +++++ .../distribution/jvmlocal/JVMLocalDistributor.java | 70 +++ .../impl/distribution/jvmlocal/package-info.java | 27 ++ .../context/impl/distribution/package-info.java | 28 ++ .../context/impl/locking/AbstractLockManager.java | 225 ++++++++++ .../context/impl/locking/LockManagerFactory.java | 84 ++++ .../impl/locking/jvmlocal/JVMLocalLockManager.java | 56 +++ .../impl/locking/jvmlocal/package-info.java | 27 ++ .../apex/context/impl/locking/package-info.java | 26 ++ .../policy/apex/context/impl/package-info.java | 28 ++ .../context/impl/persistence/PersistorFactory.java | 83 ++++ .../persistence/ephemeral/EphemeralPersistor.java | 100 +++++ .../impl/persistence/ephemeral/package-info.java | 28 ++ .../context/impl/persistence/package-info.java | 28 ++ .../context/impl/schema/AbstractSchemaHelper.java | 169 +++++++ .../context/impl/schema/SchemaHelperFactory.java | 119 +++++ .../context/impl/schema/java/JavaSchemaHelper.java | 214 +++++++++ .../schema/java/JavaSchemaHelperParameters.java | 38 ++ .../context/impl/schema/java/package-info.java | 27 ++ .../apex/context/impl/schema/package-info.java | 29 ++ .../apex/context/monitoring/ContextMonitor.java | 210 +++++++++ .../apex/context/monitoring/package-info.java | 27 ++ .../org/onap/policy/apex/context/package-info.java | 29 ++ .../apex/context/parameters/ContextParameters.java | 146 ++++++ .../context/parameters/DistributorParameters.java | 85 ++++ .../context/parameters/LockManagerParameters.java | 85 ++++ .../context/parameters/PersistorParameters.java | 120 +++++ .../context/parameters/SchemaHelperParameters.java | 81 ++++ .../apex/context/parameters/SchemaParameters.java | 87 ++++ .../apex/context/parameters/package-info.java | 28 ++ .../impl/schema/java/TestInstanceCreation.java | 96 ++++ .../src/test/resources/logback-test.xml | 70 +++ context/context-test/pom.xml | 55 +++ .../context/test/concepts/TestContextItem000.java | 114 +++++ .../context/test/concepts/TestContextItem001.java | 112 +++++ .../context/test/concepts/TestContextItem002.java | 121 +++++ .../context/test/concepts/TestContextItem003.java | 113 +++++ .../context/test/concepts/TestContextItem004.java | 112 +++++ .../context/test/concepts/TestContextItem005.java | 115 +++++ .../context/test/concepts/TestContextItem006.java | 116 +++++ .../context/test/concepts/TestContextItem007.java | 116 +++++ .../context/test/concepts/TestContextItem008.java | 258 +++++++++++ .../context/test/concepts/TestContextItem009.java | 186 ++++++++ .../context/test/concepts/TestContextItem00A.java | 261 +++++++++++ .../context/test/concepts/TestContextItem00B.java | 128 ++++++ .../context/test/concepts/TestContextItem00C.java | 122 ++++++ .../test/concepts/TestExternalContextItem.java | 434 ++++++++++++++++++ .../test/concepts/TestGlobalContextItem.java | 434 ++++++++++++++++++ .../test/concepts/TestPolicyContextItem.java | 241 ++++++++++ .../apex/context/test/concepts/package-info.java | 27 ++ .../test/distribution/ContextAlbumUpdate.java | 121 +++++ .../test/distribution/ContextInstantiation.java | 487 +++++++++++++++++++++ .../context/test/distribution/ContextUpdate.java | 155 +++++++ .../SequentialContextInstantiation.java | 343 +++++++++++++++ .../context/test/distribution/package-info.java | 27 ++ .../test/factory/TestContextAlbumFactory.java | 189 ++++++++ .../apex/context/test/factory/package-info.java | 27 ++ .../context/test/locking/ConcurrentContext.java | 194 ++++++++ .../context/test/locking/ConcurrentContextJVM.java | 219 +++++++++ .../test/locking/ConcurrentContextJVMThread.java | 123 ++++++ .../test/locking/ConcurrentContextThread.java | 149 +++++++ .../apex/context/test/locking/package-info.java | 27 ++ .../test/distribution/TestContextAlbumUpdate.java | 65 +++ .../distribution/TestContextInstantiation.java | 75 ++++ .../test/distribution/TestContextUpdate.java | 62 +++ .../TestSequentialContextInstantiation.java | 73 +++ .../test/locking/TestConcurrentContext.java | 97 ++++ .../TestPersistentContextInstantiation.java | 184 ++++++++ .../src/test/resources/META-INF/persistence.xml | 50 +++ context/pom.xml | 40 ++ 81 files changed, 9960 insertions(+) create mode 100644 context/context-management/pom.xml create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/ContextException.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/ContextRuntimeException.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/Distributor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/LockManager.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/Persistor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/AbstractDistributor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFactory.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFlushTimerTask.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/JVMLocalDistributor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/AbstractLockManager.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/LockManagerFactory.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/JVMLocalLockManager.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/PersistorFactory.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/EphemeralPersistor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/ContextMonitor.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/package-info.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/ContextParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/DistributorParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/LockManagerParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/PersistorParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaHelperParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaParameters.java create mode 100644 context/context-management/src/main/java/org/onap/policy/apex/context/parameters/package-info.java create mode 100644 context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java create mode 100644 context/context-management/src/test/resources/logback-test.xml create mode 100644 context/context-test/pom.xml create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem000.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem001.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem002.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem003.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem004.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem005.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem006.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem007.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem008.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem009.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00A.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00B.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00C.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestExternalContextItem.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestGlobalContextItem.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestPolicyContextItem.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/package-info.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextAlbumUpdate.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/package-info.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/TestContextAlbumFactory.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/package-info.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java create mode 100644 context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/package-info.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextAlbumUpdate.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextInstantiation.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextUpdate.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestSequentialContextInstantiation.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/locking/TestConcurrentContext.java create mode 100644 context/context-test/src/test/java/org/onap/policy/apex/context/test/persistence/TestPersistentContextInstantiation.java create mode 100644 context/context-test/src/test/resources/META-INF/persistence.xml create mode 100644 context/pom.xml (limited to 'context') diff --git a/context/context-management/pom.xml b/context/context-management/pom.xml new file mode 100644 index 000000000..b05c76a71 --- /dev/null +++ b/context/context-management/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.context + context + 2.0.0-SNAPSHOT + + + context-management + ${project.artifactId} + Context management for Apex policy execution + + + + org.onap.policy.apex-pdp.model + context-model + ${project.version} + + + org.onap.policy.apex-pdp.model + utilities + ${project.version} + + + com.google.code.gson + gson + + + \ No newline at end of file diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java new file mode 100644 index 000000000..73ef668a0 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java @@ -0,0 +1,127 @@ +/*- + * ============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.context; + +import java.util.Map; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; + +/** + * The Interface ContextAlbum is implemented by all classes that manage context in Apex. Context albums may store + * context in memory, on disk, in a repository or in a mechanism such as a distributed map. + *

+ * A context album uses plugins to handle its context schemas, its distribution, its locking, and its persistence. + *

+ * The schema that defines the items in a context album is interpreted by a plugin that implements the + * {@link SchemaHelper} interface. The schema helper uses the schema definition to provide new instances for a context + * album. By default, context albums use Java schemas. + *

+ * Context albums may be shared across an arbitrary number of JVMs using a distribution mechanism. Apex context + * distributed context albums using plugins that implement the {@link Distributor} interface. By default, context albums + * use JVM local distribution, that is context albums are only available in a single JVM + *

+ * Items in a context album may be locked across all distributed instances of an album. Apex locks instances on context + * albums using the distributed locking mechanism in a plugin that implements the {@link LockManager} interface. By + * default, context albums use Java locking local to a single JVM on each context album instance. + *

+ * Context albums may be persisted to disk, database, or any other repository. Apex persists context albums using the + * persistence mechanism in a plugin that implements the {@link Persistor} interface. By default, context albums use a + * dummy persistor plugin that does not persist context albums. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public interface ContextAlbum extends Map { + /** + * Gets the key of the context album instance. + * + * @return the key + */ + AxArtifactKey getKey(); + + /** + * Gets the name of the context album instance. + * + * @return the name + */ + String getName(); + + /** + * Get the current context album with values. + * + * @return the current context runtime values + */ + AxContextAlbum getAlbumDefinition(); + + /** + * Get the schema helper for the technology that is handling the schema for this album. + * + * @return the schema helper + */ + SchemaHelper getSchemaHelper(); + + /** + * Place a read lock on a key in this album across the entire cluster. + * + * @param key The key to lock + * @throws ContextException on locking errors + */ + void lockForReading(String key) throws ContextException; + + /** + * Place a write lock on a key in this album across the entire cluster. + * + * @param key The key to lock + * @throws ContextException on locking errors + */ + void lockForWriting(String key) throws ContextException; + + /** + * Release the the read lock on a key in this album across the entire cluster. + * + * @param key The key to unlock + * @throws ContextException on locking errors + */ + void unlockForReading(String key) throws ContextException; + + /** + * Release the the write lock on a key in this album across the entire cluster. + * + * @param key The key to unlock + * @throws ContextException on locking errors + */ + void unlockForWriting(String key) throws ContextException; + + /** + * Set the stack of artifact keys currently using this context item. + * + * @param userArtifactStack the keys of the artifacts using the context album at the moment + */ + void setUserArtifactStack(AxConcept[] userArtifactStack); + + /** + * Flush the context album to the distribution and persistence mechanism. + * + * @throws ContextException On context flush errors + */ + void flush() throws ContextException; +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextException.java b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextException.java new file mode 100644 index 000000000..de5cec051 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextException.java @@ -0,0 +1,51 @@ +/*- + * ============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.context; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; + +/** + * This exception will be called if an error occurs in an Apex context item. + * + * @author Liam Fallon + */ +public class ContextException extends ApexException { + private static final long serialVersionUID = -8507246953751956974L; + + /** + * Instantiates a new apex context exception with a message. + * + * @param message the message + */ + public ContextException(final String message) { + super(message); + } + + /** + * Instantiates a new apex context exception with a message and a caused by exception. + * + * @param message the message + * @param e the exception that caused this exception to be thrown + */ + public ContextException(final String message, final Exception e) { + super(message, e); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextRuntimeException.java b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextRuntimeException.java new file mode 100644 index 000000000..b2aa017e5 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextRuntimeException.java @@ -0,0 +1,51 @@ +/*- + * ============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.context; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; + +/** + * This exception will be called if an error occurs in an Apex context item. + * + * @author Liam Fallon + */ +public class ContextRuntimeException extends ApexRuntimeException { + private static final long serialVersionUID = -8507246953751956974L; + + /** + * Instantiates a new apex context exception with a message. + * + * @param message the message + */ + public ContextRuntimeException(final String message) { + super(message); + } + + /** + * Instantiates a new apex context exception with a message and a caused by exception. + * + * @param message the message + * @param e the exception that caused this exception to be thrown + */ + public ContextRuntimeException(final String message, final Exception e) { + super(message, e); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/Distributor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/Distributor.java new file mode 100644 index 000000000..a17313882 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/Distributor.java @@ -0,0 +1,140 @@ +/*- + * ============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.context; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; + +/** + * This interface is implemented by plugin classes that distribute context albums in Apex. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public interface Distributor { + + /** + * Initialize the distributor with its properties. + * + * @param key The key that identifies this distributor + * @throws ContextException On errors initializing the distributor + */ + void init(AxArtifactKey key) throws ContextException; + + /** + * Shut down distributor. + * + * @throws ContextException On errors initializing the distributor + */ + void shutdown() throws ContextException; + + /** + * Get the key of the distributor. + * + * @return the contextSetKey + */ + AxArtifactKey getKey(); + + /** + * Register the context model and its sub models with the model service. + * + * @param contextModel the context model to register + * @throws ContextException on model registration errors + */ + void registerModel(AxContextModel contextModel) throws ContextException; + + /** + * Create a context album on a distributor, the distributor looks up the album and initialize it. The + * {@link AxContextAlbum} is used to check that the album in the distributor matches the album definition we expect + * to get. + * + * @param axContextAlbumKey the key of the model context album for this context album + * @return the context album + * @throws ContextException if the album cannot be initialised + */ + ContextAlbum createContextAlbum(AxArtifactKey axContextAlbumKey) throws ContextException; + + /** + * Remove a context album from a distributor. + * + * @param contextAlbum The album to remove + * @throws ContextException if the album cannot be removed + */ + void removeContextAlbum(AxContextAlbum contextAlbum) throws ContextException; + + /** + * Flush all context albums owned by the distributor to the distribution mechanism. + * + * @throws ContextException on context flushing errors + */ + void flush() throws ContextException; + + /** + * Flush a context album owned by the distributor to the distribution mechanism. + * + * @param contextAlbum the context album to flush + * @throws ContextException on errors in flushing the context album + */ + void flushContextAlbum(ContextAlbum contextAlbum) throws ContextException; + + /** + * Place a read lock on an item in an album across the entire cluster. + * + * @param albumKey The key of the album containing the item + * @param keyOnMap The key on the album to lock + * @throws ContextException on locking errors + */ + void lockForReading(AxArtifactKey albumKey, String keyOnMap) throws ContextException; + + /** + * Place a write lock on an album item across the entire cluster. + * + * @param albumKey The key of the album containing the item + * @param key The key on the album to lock + * @throws ContextException on locking errors + */ + void lockForWriting(AxArtifactKey albumKey, String key) throws ContextException; + + /** + * Release the read lock on a key across the entire cluster. + * + * @param albumKey The key of the album containing the item + * @param key The key on the album to unlock + * @throws ContextException on locking errors + */ + void unlockForReading(AxArtifactKey albumKey, String key) throws ContextException; + + /** + * Release the write lock on a key across the entire cluster. + * + * @param albumKey The key of the album containing the item + * @param key The key on the album to unlock + * @throws ContextException on locking errors + */ + void unlockForWriting(AxArtifactKey albumKey, String key) throws ContextException; + + /** + * Clear all the context from the context distributor. + * + * @throws ContextException on context clearing exceptions + */ + void clear() throws ContextException; +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/LockManager.java b/context/context-management/src/main/java/org/onap/policy/apex/context/LockManager.java new file mode 100644 index 000000000..8ccf54424 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/LockManager.java @@ -0,0 +1,88 @@ +/*- + * ============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.context; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * This interface provides a facade to hide implementation details of various lock managers that may be used to manage + * locking of context items. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public interface LockManager { + + /** + * Initialize the lock manager with its properties. + * + * @param key The key of this lock manager + * @throws ContextException On errors initializing the persistor + */ + void init(AxArtifactKey key) throws ContextException; + + /** + * Get the key of the lock manager. + * + * @return the managers key + */ + AxArtifactKey getKey(); + + /** + * Place a read lock on a lock type and key across the entire cluster. + * + * @param lockTypeKey The key of the map where the context item to lock is + * @param lockKey The key on the map to lock + * @throws ContextException on locking errors + */ + void lockForReading(String lockTypeKey, String lockKey) throws ContextException; + + /** + * Place a write lock on a lock type and key across the entire cluster. + * + * @param lockTypeKey The key of the map where the context item to lock is + * @param lockKey The key on the map to lock + * @throws ContextException on locking errors + */ + void lockForWriting(String lockTypeKey, String lockKey) throws ContextException; + + /** + * Release a read lock on a lock type and key across the entire cluster. + * + * @param lockTypeKey The key of the map where the context item to lock is + * @param lockKey The key on the map to lock + * @throws ContextException on locking errors + */ + void unlockForReading(String lockTypeKey, String lockKey) throws ContextException; + + /** + * Release a write lock on a lock type and key across the entire cluster. + * + * @param lockTypeKey The key of the map where the context item to lock is + * @param lockKey The key on the map to lock + * @throws ContextException on locking errors + */ + void unlockForWriting(String lockTypeKey, String lockKey) throws ContextException; + + /** + * Shut down the lock manager and clear any connections or data it is using. + */ + void shutdown(); +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/Persistor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/Persistor.java new file mode 100644 index 000000000..d3218a34b --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/Persistor.java @@ -0,0 +1,79 @@ +/*- + * ============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.context; + +import java.util.Set; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; + +/** + * This interface is implemented by plugin classes that persist Context Albums in Apex. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public interface Persistor { + + /** + * Initialize the persistor with its properties. + * + * @param key The key that identifies this persistor + * @throws ContextException On errors initializing the persistor + */ + void init(AxArtifactKey key) throws ContextException; + + /** + * Get the key of the persistor. + * + * @return the contextSetKey + */ + AxArtifactKey getKey(); + + /** + * Read a context item from the persistence mechanism. + * + * @param key the key of the context item + * @param contextItemClassName the name of the context item class, a subclass of {@link AxContextSchema} + * @return the context item that has been read + * @throws ContextException on persistence read errors + */ + AxContextSchema readContextItem(AxReferenceKey key, Class contextItemClassName) throws ContextException; + + /** + * Read all the values of a particular type from persistence. + * + * @param ownerKey the owner key + * @param contextItemClassName The class name of the objects to return + * @return the set of context item values read from persistence or null if none were found + * @throws ContextException On read errors + */ + Set readContextItems(AxArtifactKey ownerKey, Class contextItemClassName) + throws ContextException; + + /** + * Write a context item value to the persistence mechanism. + * + * @param contextItem the context item + * @return the context item that has been written + */ + Object writeContextItem(Object contextItem); +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java new file mode 100644 index 000000000..aa6ea9ffe --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/SchemaHelper.java @@ -0,0 +1,119 @@ +/*- + * ============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.context; + +import com.google.gson.JsonElement; + +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; + +/** + * This interface is implemented by plugin classes that use a particular schema to convert Apex context objects to an + * understandable form. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public interface SchemaHelper { + + /** + * Initialize the schema helper with its properties. + * + * @param userKey The key that identifies the user of the schema helper + * @param schema the schema + * @throws ContextRuntimeException the context runtime exception + */ + void init(AxKey userKey, AxContextSchema schema) throws ContextRuntimeException; + + /** + * Get the user key of the schema helper. + * + * @return the user key + */ + AxKey getUserKey(); + + /** + * Get the schema of the schema helper. + * + * @return the schema + */ + AxContextSchema getSchema(); + + /** + * The Java class that this schema produces on the Java side. + * + * @return the schema class + */ + Class getSchemaClass(); + + /** + * The Java class that handles the schema for the schema technology in use. + * + * @return the schema object + */ + Object getSchemaObject(); + + /** + * Create a new instance of the schema class using whatever schema technology is being used. + * + * @return the new instance + */ + Object createNewInstance(); + + /** + * Create a new instance of the schema class using whatever schema technology is being used. + * + * @param stringValue the string represents the value the new instance should have + * @return the new instance + */ + Object createNewInstance(String stringValue); + + /** + * Create a new instance of the schema class from a GSON JsonElement using whatever schema technology is being used. + * + * @param jsonElement the JSON element that holds the Json representation of the object + * @return the new instance + */ + Object createNewInstance(JsonElement jsonElement); + + /** + * Unmarshal an object in schema format into a Java object. + * + * @param object the object as a Java object + * @return the object in schema format + */ + Object unmarshal(Object object); + + /** + * Marshal a Java object into Json format. + * + * @param schemaObject the object in schema format + * @return the object as a Json string + */ + String marshal2Json(Object schemaObject); + + /** + * Marshal a Java object into a GSON json element. + * + * @param schemaObject the object in schema format + * @return the object as a GSON Json element + */ + JsonElement marshal2JsonElement(Object schemaObject); +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java new file mode 100644 index 000000000..a68271609 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java @@ -0,0 +1,486 @@ +/*- + * ============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.context.impl; + +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.monitoring.ContextMonitor; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.utilities.Assertions; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class ContextAlbumImpl implements the methods on the {@link ContextAlbum} interface. It implements the getters + * and setters on the {@link Map} and uses the {@link Distributor} to handle distribution and locking. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class ContextAlbumImpl implements ContextAlbum { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumImpl.class); + + // The definition of this context album + private final AxContextAlbum albumDefinition; + + /// The map holding the items and their values for this context album + private final Map albumMap; + + // The artifact stack of the artifacts currently using the context album + private AxConcept[] userArtifactStack = null; + + // The context distributor we are using + private final Distributor distributor; + + // The schema helper that handles translations of Java objects for this album + private SchemaHelper schemaHelper; + + // The context monitor for this context album + private ContextMonitor monitor = null; + + /** + * Constructor, instantiate the context album. + * + * @param albumDefinition The model definition of this context album + * @param distributor The context distributor passed to us to distribute context across ContextAlbum instances + * @param albumMap the album map + * @throws ContextException on errors creating context albums + */ + public ContextAlbumImpl(final AxContextAlbum albumDefinition, final Distributor distributor, + final Map albumMap) throws ContextException { + this.albumDefinition = albumDefinition; + + // Use the context distributor passed to us + this.distributor = distributor; + + // The map to use to store objects + this.albumMap = albumMap; + + try { + // Get a schema helper to manage the translations between objects on the album map for this album + schemaHelper = new SchemaHelperFactory().createSchemaHelper(albumDefinition.getKey(), + albumDefinition.getItemSchema()); + } catch (final ContextRuntimeException e) { + final String resultString = "could not initiate schema management for context album " + albumDefinition; + LOGGER.warn(resultString, e); + throw new ContextException(resultString, e); + } + + // Create the context monitor + monitor = new ContextMonitor(); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getKey() + */ + @Override + public AxArtifactKey getKey() { + return albumDefinition.getKey(); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getName() + */ + @Override + public String getName() { + return albumDefinition.getKey().getName(); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getAxContextAlbum() + */ + @Override + public AxContextAlbum getAlbumDefinition() { + return albumDefinition; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getSchemaHelper() + */ + @Override + public SchemaHelper getSchemaHelper() { + return schemaHelper; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#lockForReading(java.lang.String) + */ + @Override + public void lockForReading(final String keyOnAlbum) throws ContextException { + distributor.lockForReading(albumDefinition.getKey(), keyOnAlbum); + monitor.monitorReadLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, + userArtifactStack); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#lockForWriting(java.lang.String) + */ + @Override + public void lockForWriting(final String keyOnAlbum) throws ContextException { + distributor.lockForWriting(albumDefinition.getKey(), keyOnAlbum); + monitor.monitorWriteLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, + userArtifactStack); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#unlockForReading(java.lang.String) + */ + @Override + public void unlockForReading(final String keyOnAlbum) throws ContextException { + distributor.unlockForReading(albumDefinition.getKey(), keyOnAlbum); + monitor.monitorReadUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, + userArtifactStack); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#unlockForWriting(java.lang.String) + */ + @Override + public void unlockForWriting(final String keyOnAlbum) throws ContextException { + distributor.unlockForWriting(albumDefinition.getKey(), keyOnAlbum); + monitor.monitorWriteUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, + userArtifactStack); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.context.ContextAlbum#setUserArtifactStack(org.onap.policy.apex.model.basicmodel.concepts. + * AxConcept []) + */ + @Override + public void setUserArtifactStack(final AxConcept[] userArtifactStack) { + this.userArtifactStack = userArtifactStack; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#flush() + */ + @Override + public void flush() throws ContextException { + distributor.flushContextAlbum(this); + } + + /* + * The Map interface + */ + + /* + * (non-Javadoc) + * + * @see java.util.Map#size() + */ + @Override + public int size() { + return albumMap.size(); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#isEmpty() + */ + @Override + public boolean isEmpty() { + return albumMap.isEmpty(); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#containsKey(java.lang.Object) + */ + @Override + public boolean containsKey(final Object key) { + if (key == null) { + LOGGER.warn("null values are illegal on method parameter \"key\""); + throw new ContextRuntimeException("null values are illegal on method parameter \"key\""); + } + + return albumMap.containsKey(key); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#containsValue(java.lang.Object) + */ + @Override + public boolean containsValue(final Object value) { + if (value == null) { + LOGGER.warn("null values are illegal on method parameter \"value\""); + throw new ContextRuntimeException("null values are illegal on method parameter \"value\""); + } + + return albumMap.containsValue(value); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#get(java.lang.Object) + */ + @Override + public Object get(final Object key) { + if (key == null) { + final String returnString = + "album \"" + albumDefinition.getID() + "\" null keys are illegal on keys for get()"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + final Object item = albumMap.get(key); + if (item == null) { + return null; + } + + // Get the context value and monitor it + monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), item, + userArtifactStack); + return item; + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#keySet() + */ + @Override + public Set keySet() { + return albumMap.keySet(); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#values() + */ + @Override + public Collection values() { + // Build the key set and return it + final ArrayList valueList = new ArrayList<>(); + + for (final Entry contextAlbumEntry : albumMap.entrySet()) { + final Object item = contextAlbumEntry.getValue(); + monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), + item, userArtifactStack); + valueList.add(contextAlbumEntry.getValue()); + } + + return valueList; + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#entrySet() + */ + @Override + public Set> entrySet() { + // Build the entry set and return it + final Set> entrySet = new HashSet<>(); + + for (final Entry contextAlbumEntry : albumMap.entrySet()) { + final Object item = contextAlbumEntry.getValue(); + monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), + item, userArtifactStack); + entrySet.add(new SimpleEntry<>(contextAlbumEntry.getKey(), contextAlbumEntry.getValue())); + } + + return entrySet; + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#put(java.lang.Object, java.lang.Object) + */ + @Override + public Object put(final String key, final Object incomingValue) { + if (key == null) { + final String returnString = + "album \"" + albumDefinition.getID() + "\" null keys are illegal on keys for put()"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + if (incomingValue == null) { + final String returnString = "album \"" + albumDefinition.getID() + "\" null values are illegal on key \"" + + key + "\" for put()"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + if (!albumDefinition.isWritable()) { + final String returnString = "album \"" + albumDefinition.getID() + + "\" put() not allowed on read only albums for key=\"" + key + "\", value=\"" + incomingValue; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + try { + // Translate the object to a schema object + final Object valueToPut = schemaHelper.unmarshal(incomingValue); + + // Check if the key is already in the map + if (albumMap.containsKey(key)) { + // Update the value in the context item and in the context value map + monitor.monitorSet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue, + userArtifactStack); + } else { + // Update the value in the context item and in the context value map + monitor.monitorInit(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue, + userArtifactStack); + } + + // Put the translated value on the map and return the old map value + return albumMap.put(key, valueToPut); + } catch (final ContextRuntimeException e) { + final String returnString = "Failed to set context value for key \"" + key + "\" in album \"" + + albumDefinition.getID() + "\": " + e.getMessage(); + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString, e); + } + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#putAll(java.util.Map) + */ + @Override + public void putAll(final Map incomingContextAlbum) { + if (!albumDefinition.isWritable()) { + final String returnString = + "album \"" + albumDefinition.getID() + "\" putAll() not allowed on read only albums"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + // Sanity check on incoming context + Assertions.argumentNotNull(incomingContextAlbum, ContextRuntimeException.class, + "cannot update context, context album is null"); + + // Iterate over the incoming context + for (final Entry entry : albumMap.entrySet()) { + synchronized (albumDefinition) { + // Get the key for the incoming name + final Object incomingDataItem = incomingContextAlbum.get(entry.getKey()); + if (incomingDataItem != null) { + // Update the value the context album + put(entry.getKey(), incomingDataItem); + } + } + } + + // Put all the objects on the context album + for (final Entry incomingMapEntry : incomingContextAlbum.entrySet()) { + // Put the entry on the map + this.put(incomingMapEntry.getKey(), incomingMapEntry.getValue()); + } + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#remove(java.lang.Object) + */ + @Override + public Object remove(final Object key) { + if (!albumDefinition.isWritable()) { + final String returnString = "album \"" + albumDefinition.getID() + + "\" remove() not allowed on read only albums for key=\"" + key; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + if (key == null) { + LOGGER.warn("null values are illegal on method parameter \"key\""); + throw new ContextRuntimeException("null values are illegal on method parameter \"keyID\""); + } + + // Delete the item + final Object removedValue = albumMap.remove(key); + monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), removedValue, + userArtifactStack); + + // Return the value of the deleted item + return removedValue; + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#clear() + */ + @Override + public void clear() { + if (!albumDefinition.isWritable()) { + final String returnString = + "album \"" + albumDefinition.getID() + "\" clear() not allowed on read only albums"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + // Monitor deletion of each item + for (final Entry contextAlbumEntry : albumMap.entrySet()) { + final Object item = contextAlbumEntry.getValue(); + monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), + item, userArtifactStack); + } + + // Clear the map + albumMap.clear(); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/AbstractDistributor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/AbstractDistributor.java new file mode 100644 index 000000000..e5a45b203 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/AbstractDistributor.java @@ -0,0 +1,326 @@ +/*- + * ============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.context.impl.distribution; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.LockManager; +import org.onap.policy.apex.context.Persistor; +import org.onap.policy.apex.context.impl.ContextAlbumImpl; +import org.onap.policy.apex.context.impl.locking.LockManagerFactory; +import org.onap.policy.apex.context.impl.persistence.PersistorFactory; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This context distributor implements the mechanism-neutral parts of a context distributor. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public abstract class AbstractDistributor implements Distributor { + + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractDistributor.class); + + // The key of this distributor + private AxArtifactKey key = null; + + // The context albums for this context set indexed by their keys + private static Map albumMaps = + Collections.synchronizedMap(new HashMap()); + + // Lock manager for this distributor + private static LockManager lockManager = null; + + // Hold a persistor for this distributor + private Persistor persistor = null; + + // Hold a flush timer for this context distributor + private static DistributorFlushTimerTask flushTimer = null; + + /** + * Create an instance of an abstract Context Distributor. + */ + public AbstractDistributor() { + LOGGER.entry("AbstractContextDistributor()"); + LOGGER.exit("AbstractContextDistributor()"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextDistributor#init(org.onap.policy.apex.model.basicmodel.concepts. + * AxArtifactKey) + */ + @Override + public void init(final AxArtifactKey distributorKey) throws ContextException { + LOGGER.entry("init(" + distributorKey + ")"); + + // Record parameters and key + this.key = distributorKey; + + // Create the lock manager if it doesn't already exist + if (lockManager == null) { + lockManager = new LockManagerFactory().createLockManager(key); + } + + // Set up flushing on the context distributor if its not set up already + if (flushTimer == null) { + flushTimer = new DistributorFlushTimerTask(this); + } + + // Create a new persistor for this key + persistor = new PersistorFactory().createPersistor(key); + LOGGER.exit("init(" + key + ")"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextDistributor#shutdown() + */ + @Override + public abstract void shutdown(); + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextDistributor#getKey() + */ + @Override + public AxArtifactKey getKey() { + return key; + } + + /** + * Create a context album using whatever underlying mechanism we are using for albums. + * + * @param contextAlbumKey The key of the album + * @return The album as a string-object map + */ + public abstract Map getContextAlbumMap(AxArtifactKey contextAlbumKey); + + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.Distributor#registerModel(org.onap.policy.apex.model.contextmodel.concepts. + * AxContextModel) + */ + @Override + public void registerModel(final AxContextModel contextModel) throws ContextException { + ModelService.registerModel(AxKeyInformation.class, contextModel.getKeyInformation()); + ModelService.registerModel(AxContextSchemas.class, contextModel.getSchemas()); + ModelService.registerModel(AxContextAlbums.class, contextModel.getAlbums()); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.core.context.ContextDistributor#createContextAlbum(org.onap.policy.apex.core.basicmodel. + * concepts. AxArtifactKey) + */ + @Override + public synchronized ContextAlbum createContextAlbum(final AxArtifactKey axContextAlbumKey) throws ContextException { + // Get the context album definition + final AxContextAlbum album = ModelService.getModel(AxContextAlbums.class).get(axContextAlbumKey); + if (album == null) { + final String resultString = "context album " + axContextAlbumKey.getID() + " does not exist"; + LOGGER.warn(resultString); + throw new ContextException(resultString); + } + + // Check if the context album is valid + final AxValidationResult result = album.validate(new AxValidationResult()); + if (!result.isValid()) { + final String resultString = + "context album definition for " + album.getKey().getID() + " is invalid" + result; + LOGGER.warn(resultString); + throw new ContextException(resultString); + } + + // Get the schema of the context album + final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(album.getItemSchema()); + if (schema == null) { + final String resultString = "schema \"" + album.getItemSchema().getID() + "\" for context album " + + album.getKey().getID() + " does not exist"; + LOGGER.warn(resultString); + throw new ContextException(resultString); + } + + // Check if the map has already been instantiated + if (!albumMaps.containsKey(album.getKey())) { + // Instantiate the album map for this context album that we'll distribute using the distribution mechanism + final Map newContextAlbumMap = getContextAlbumMap(album.getKey()); + + // The distributed context album will have content from another process instance if the album exists in + // another process, + // if not, we have to try to read the content from persistence + if (newContextAlbumMap.isEmpty()) { + // Read entries from persistence + // TODO: READ ITEMS FROM PRESISTENCE!!!! + } + + // Create the context album and put the context album object onto the distributor + albumMaps.put(album.getKey(), new ContextAlbumImpl(album, this, newContextAlbumMap)); + } + + return albumMaps.get(album.getKey()); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.core.context.ContextDistributor#removeContextAlbum(org.onap.policy.apex.core.basicmodel. + * concepts. AxArtifactKey) + */ + @Override + public void removeContextAlbum(final AxContextAlbum contextAlbum) throws ContextException { + // Check if the map already exists, if not return + if (!albumMaps.containsKey(contextAlbum.getKey())) { + LOGGER.warn("map remove failed, supplied map is null"); + throw new ContextException("map update failed, supplied map is null"); + } + + // Remove the map from the distributor + albumMaps.remove(contextAlbum.getKey()); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#flush() + */ + @Override + public void flush() throws ContextException { + // Flush all the maps + for (final Entry distributorMapEntry : albumMaps.entrySet()) { + // Let the persistor write each of the entries + for (final Object contextItem : distributorMapEntry.getValue().values()) { + LOGGER.debug(contextItem.toString()); + // persistor.writeContextItem((AxContextSchema) contextItem); + } + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#flushContextAlbum(org.onap.policy.apex.core.context. + * ContextAlbum) + */ + @Override + public void flushContextAlbum(final ContextAlbum contextAlbum) throws ContextException { + // Check if the map already exists, if not return + if (!albumMaps.containsKey(contextAlbum.getKey())) { + LOGGER.warn("map flush failed, supplied map is null"); + throw new ContextException("map flush failed, supplied map is null"); + } + + // Let the persistor flush the items on the map + for (final Object contextItem : albumMaps.get(contextAlbum.getKey()).values()) { + persistor.writeContextItem(contextItem); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#lockForReading(java.lang.String) + */ + @Override + public synchronized void lockForReading(final AxArtifactKey mapKey, final String itemKey) throws ContextException { + // Lock using the lock manager + lockManager.lockForReading(mapKey.getID(), itemKey); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#lockForWriting(java.lang.String) + */ + @Override + public synchronized void lockForWriting(final AxArtifactKey mapKey, final String itemKey) throws ContextException { + // Lock using the lock manager + lockManager.lockForWriting(mapKey.getID(), itemKey); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#unlockForReading(java.lang.String) + */ + @Override + public void unlockForReading(final AxArtifactKey mapKey, final String itemKey) throws ContextException { + // Unlock using the lock manager + lockManager.unlockForReading(mapKey.getID(), itemKey); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#unlockForWriting(java.lang.String) + */ + @Override + public void unlockForWriting(final AxArtifactKey mapKey, final String itemKey) throws ContextException { + // Unlock using the lock manager + lockManager.unlockForWriting(mapKey.getID(), itemKey); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.ContextDistributor#clear() + */ + @Override + public void clear() { + // Shut down the lock manager + if (lockManager != null) { + lockManager.shutdown(); + lockManager = null; + } + + albumMaps.clear(); + + // Turn off the flush timer + flushTimer.cancel(); + + // Shut down the specialization of the context distributor + shutdown(); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFactory.java new file mode 100644 index 000000000..1af13cbea --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFactory.java @@ -0,0 +1,86 @@ +/*- + * ============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.context.impl.distribution; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.utilities.Assertions; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class returns a context distributor for the particular type of distribution mechanism configured for use. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class DistributorFactory { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFactory.class); + + /** + * Get a context distributor for a given context set key. + * + * @param key The key for the distributor + * @return a context distributor + * @throws ContextException on context distributor creation errors + */ + public Distributor getDistributor(final AxArtifactKey key) throws ContextException { + LOGGER.entry("Distributor factory, key=" + key); + + Assertions.argumentNotNull(key, ContextException.class, "Parameter \"key\" may not be null"); + + // Get the class for the distributor using reflection + final DistributorParameters distributorParameters = ParameterService.getParameters(DistributorParameters.class); + final String pluginClass = distributorParameters.getPluginClass(); + Object contextDistributorObject = null; + try { + contextDistributorObject = Class.forName(pluginClass).newInstance(); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + LOGGER.error( + "Apex context distributor class not found for context distributor plugin \"" + pluginClass + "\"", + e); + throw new ContextException( + "Apex context distributor class not found for context distributor plugin \"" + pluginClass + "\"", + e); + } + + // Check the class is a distributor + if (!(contextDistributorObject instanceof Distributor)) { + final String returnString = "Specified Apex context distributor plugin class \"" + pluginClass + + "\" does not implement the ContextDistributor interface"; + LOGGER.error(returnString); + throw new ContextException(returnString); + } + + // The context Distributor to return + final Distributor contextDistributor = (Distributor) contextDistributorObject; + + // Lock and load the context distributor + contextDistributor.init(key); + + LOGGER.exit( + "Distributor factory, key=" + key + ", selected distributor of class " + contextDistributor.getClass()); + return contextDistributor; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFlushTimerTask.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFlushTimerTask.java new file mode 100644 index 000000000..467d43f9d --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/DistributorFlushTimerTask.java @@ -0,0 +1,116 @@ +/*- + * ============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.context.impl.distribution; + +import java.util.Timer; +import java.util.TimerTask; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class is used to periodically flush a context distributor. + * + * @author eeilfn + */ +public class DistributorFlushTimerTask extends TimerTask { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFlushTimerTask.class); + + // The timer for flushing + private Timer timer = null; + + // The context distributor to flush + private final Distributor contextDistributor; + + // Timing information + private long period = 0; + private long flushCount = 0; + + /** + * Constructor, save a reference to the event stream handler. + * + * @param contextDistributor the distributor that this timer task is flushing + * @throws ContextException On flush setup errors + */ + public DistributorFlushTimerTask(final Distributor contextDistributor) throws ContextException { + // Save the context distributor and period + this.contextDistributor = contextDistributor; + + // Set the period for persistence flushing + final PersistorParameters persistorParameters = ParameterService.getParameters(PersistorParameters.class); + period = persistorParameters.getFlushPeriod(); + + // Set up the timer + timer = new Timer(DistributorFlushTimerTask.class.getSimpleName(), true); + timer.schedule(this, period, period); + + LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushing set up with interval: " + + period + "ms"); + } + + /** + * Flush the context distributor. + */ + @Override + public void run() { + // Increment the flush counter + flushCount++; + + LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushing: period=" + period + + ": count=" + flushCount); + try { + contextDistributor.flush(); + LOGGER.debug("context distributor " + contextDistributor.getKey().getID() + " flushed: period=" + period + + ": count=" + flushCount); + } catch (final ContextException e) { + LOGGER.error("flush error on context distributor " + contextDistributor.getKey().getID() + ": period=" + + period + ": count=" + flushCount, e); + } + } + + /** + * Cancel the timer. + * + * @return true, if cancel + */ + @Override + public boolean cancel() { + // Cancel the timer + if (timer != null) { + timer.cancel(); + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "ContextDistributorFlushTimerTask [period=" + period + ", flushCount=" + flushCount + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/JVMLocalDistributor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/JVMLocalDistributor.java new file mode 100644 index 000000000..dc6637227 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/JVMLocalDistributor.java @@ -0,0 +1,70 @@ +/*- + * ============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.context.impl.distribution.jvmlocal; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.onap.policy.apex.context.impl.distribution.AbstractDistributor; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This context distributor distributes context across threads in a single JVM. It holds context in memory in a map. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JVMLocalDistributor extends AbstractDistributor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JVMLocalDistributor.class); + + /** + * Create an instance of a JVM Local Context Distributor. + */ + public JVMLocalDistributor() { + super(); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.context.impl.distribution.AbstractDistributor#getContextAlbumMap(org.onap.policy.apex.model. + * basicmodel.concepts.AxArtifactKey) + */ + @Override + public Map getContextAlbumMap(final AxArtifactKey contextMapKey) { + LOGGER.debug("create map: " + contextMapKey.getID()); + return Collections.synchronizedMap(new HashMap()); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.impl.distribution.AbstractDistributor#shutdown() + */ + @Override + public void shutdown() { + // No specific shutdown for the JVMLocalContextDistributor + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/package-info.java new file mode 100644 index 000000000..d96420034 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/jvmlocal/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========================================================= + */ + +/** + * Provides the JVM local default context distribution mechanism for APEX. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.distribution.jvmlocal; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/package-info.java new file mode 100644 index 000000000..9b21eed51 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/distribution/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============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 an implementation of context album distribution that uses a distribution plugin to distribute items in APEX + * context albums. It also provides a default JVM local distribution plugin. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.distribution; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/AbstractLockManager.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/AbstractLockManager.java new file mode 100644 index 000000000..4f197e2d3 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/AbstractLockManager.java @@ -0,0 +1,225 @@ +/*- + * ============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.context.impl.locking; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.LockManager; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class implements the {@link LockManager} functionality that is common across all implementations. Lock managers + * for specific lock mechanisms specialize this class. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public abstract class AbstractLockManager implements LockManager { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractLockManager.class); + + // The key of this lock manager + private AxArtifactKey key = null; + + // Map of locks in use on this distributor for each context map + private final Map> lockMaps = + Collections.synchronizedMap(new HashMap>()); + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.LockManager#init(org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey) + */ + @Override + public void init(final AxArtifactKey lockManagerKey) throws ContextException { + this.key = lockManagerKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.LockManager#getKey() + */ + @Override + public AxArtifactKey getKey() { + return key; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#lockForReading(org.onap.policy.apex.core.model.concepts. + * AxArtifactKey, java.lang.String) + */ + @Override + public synchronized void lockForReading(final String lockTypeKey, final String lockKey) throws ContextException { + LOGGER.entry("lockForReading(" + lockTypeKey + "_" + lockKey + ")"); + + // Find the lock or create a new one + final ReadWriteLock lock = getLock(lockTypeKey, lockKey, true); + + try { + lock.readLock().lock(); + LOGGER.exit("lockForReading(" + lockTypeKey + "_" + lockKey + ")"); + } catch (final Exception e) { + LOGGER.warn("error acquiring read lock on context map " + lockTypeKey + " context item " + lockKey, e); + throw new ContextException( + "error acquiring read lock on context map " + lockTypeKey + " context item " + lockKey, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#lockForWriting(java.lang.String, java.lang.String) + */ + @Override + public synchronized void lockForWriting(final String lockTypeKey, final String lockKey) throws ContextException { + LOGGER.entry("lockForWriting(" + lockTypeKey + "_" + lockKey + ")"); + + // Find the lock or create a new one + final ReadWriteLock lock = getLock(lockTypeKey, lockKey, true); + + try { + lock.writeLock().lock(); + LOGGER.exit("lockForWriting(" + lockTypeKey + "_" + lockKey + ")"); + } catch (final Exception e) { + LOGGER.warn("error acquiring write lock on context map " + lockTypeKey + " context item " + lockKey, e); + throw new ContextException( + "error acquiring write lock on context map " + lockTypeKey + " context item " + lockKey, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#unlockForReading(java.lang.String, java.lang.String) + */ + @Override + public void unlockForReading(final String lockTypeKey, final String lockKey) throws ContextException { + LOGGER.entry("unlockForReading(" + lockTypeKey + "_" + lockKey + ")"); + + // Find the lock + final ReadWriteLock lock = getLock(lockTypeKey, lockKey, false); + + try { + lock.readLock().unlock(); + LOGGER.exit("unlockForReading(" + lockTypeKey + "_" + lockKey + ")"); + } catch (final Exception e) { + LOGGER.warn("error releasing read lock on context map " + lockTypeKey + " context item " + lockKey, e); + throw new ContextException( + "error releasing read lock on context map " + lockTypeKey + " context item " + lockKey, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#unlockForWriting(java.lang.String, java.lang.String) + */ + @Override + public void unlockForWriting(final String lockTypeKey, final String lockKey) throws ContextException { + LOGGER.entry("unlockForWriting(" + lockTypeKey + "_" + lockKey + ")"); + + // Find the lock + final ReadWriteLock lock = getLock(lockTypeKey, lockKey, false); + + try { + lock.writeLock().unlock(); + LOGGER.exit("unlockForWriting(" + lockTypeKey + "_" + lockKey + ")"); + } catch (final Exception e) { + LOGGER.warn("error releasing write lock on context map " + lockTypeKey + " context item " + lockKey, e); + throw new ContextException( + "error releasing write lock on context map " + lockTypeKey + " context item " + lockKey, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#shutdown() + */ + @Override + public abstract void shutdown(); + + /** + * Get a reentrant read write lock from whatever locking mechanism is in use. + * + * @param lockId The unique ID of the lock. + * @return The lock + * @throws ContextException On errors getting a lock + */ + protected abstract ReadWriteLock getReentrantReadWriteLock(String lockId) throws ContextException; + + /** + * Get a lock for a context item in a context map. + * + * @param lockTypeKey The key of the map where the context item to lock is + * @param lockKey The key on the map to lock + * @param createMode if true, create a lock if it does not exist + * @return The lock + * @throws ContextException On errors getting the lock + */ + private ReadWriteLock getLock(final String lockTypeKey, final String lockKey, final boolean createMode) + throws ContextException { + // Check if we have a lock type map for this lock type yet + if (!lockMaps.containsKey(lockTypeKey)) { + // Create a lock type map for the lock type + lockMaps.put(lockTypeKey, Collections.synchronizedMap(new HashMap())); + } + + // Find or create a lock in the lock map + ReadWriteLock lock = lockMaps.get(lockTypeKey).get(lockKey); + if (lock != null) { + return lock; + } + + // Should we create a lock? + if (!createMode) { + LOGGER.warn("error getting lock on context map " + lockTypeKey + " context item " + lockKey + + ", lock does not exist"); + throw new ContextException("error getting lock on context map " + lockTypeKey + " context item " + lockKey + + ", lock does not exist"); + } + + try { + // Create the lock using the specialization of this abstract class + lock = getReentrantReadWriteLock(lockTypeKey + "_" + lockKey); + + // Add the lock to the lock map + lockMaps.get(lockTypeKey).put(lockKey, lock); + + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("created lock " + lockTypeKey + "_" + lockKey); + } + return lock; + } catch (final Exception e) { + LOGGER.warn("error getting lock on context map " + lockTypeKey + " context item " + lockKey, e); + throw new ContextException("error getting lock on context map " + lockTypeKey + " context item " + lockKey, + e); + } + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/LockManagerFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/LockManagerFactory.java new file mode 100644 index 000000000..f3f4a6240 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/LockManagerFactory.java @@ -0,0 +1,84 @@ +/*- + * ============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.context.impl.locking; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.LockManager; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class returns a {@link LockManager} for the particular type of locking mechanism that has been configured for + * use. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class LockManagerFactory { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(LockManagerFactory.class); + + /** + * Return a {@link LockManager} for the particular type of locking mechanism configured for use. + * + * @param key The key for the lock manager + * @return a lock manager that can generate locks using some underlying mechanism + * @throws ContextException on errors in getting a lock manager + */ + public LockManager createLockManager(final AxArtifactKey key) throws ContextException { + LOGGER.entry("Lock Manager factory, key=" + key); + + final LockManagerParameters lockManagerParameters = ParameterService.getParameters(LockManagerParameters.class); + + // Get the class for the lock manager using reflection + Object lockManagerObject = null; + final String pluginClass = lockManagerParameters.getPluginClass(); + try { + lockManagerObject = Class.forName(pluginClass).newInstance(); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + LOGGER.error( + "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"", + e); + throw new ContextException( + "Apex context lock manager class not found for context lock manager plugin \"" + pluginClass + "\"", + e); + } + + // Check the class is a lock manager + if (!(lockManagerObject instanceof LockManager)) { + LOGGER.error("Specified Apex context lock manager plugin class \"" + pluginClass + + "\" does not implement the LockManager interface"); + throw new ContextException("Specified Apex context lock manager plugin class \"" + pluginClass + + "\" does not implement the LockManager interface"); + } + + // The context lock manager to return + final LockManager lockManager = (LockManager) lockManagerObject; + + // Lock and load (OK sorry!!!) the lock manager + lockManager.init(key); + + LOGGER.exit("Lock manager factory, key=" + key + ", selected lock manager of class " + lockManager.getClass()); + return lockManager; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/JVMLocalLockManager.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/JVMLocalLockManager.java new file mode 100644 index 000000000..5e71557b8 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/JVMLocalLockManager.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.context.impl.locking.jvmlocal; + +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.impl.locking.AbstractLockManager; + +/** + * A lock manager that returns locks that have a range of just the local JVM. The implementation uses a Jav + * {@link ReentrantReadWriteLock} as the lock for context album items. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JVMLocalLockManager extends AbstractLockManager { + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.core.context.impl.locking.AbstractLockManager#getReentrantReadWriteLock(java.lang.String) + */ + @Override + public ReadWriteLock getReentrantReadWriteLock(final String lockId) throws ContextException { + return new ReentrantReadWriteLock(); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.LockManager#shutdown() + */ + @Override + public void shutdown() { + // Nothing to do for this lock manager + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/package-info.java new file mode 100644 index 000000000..f1ac5cf19 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/jvmlocal/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========================================================= + */ + +/** + * Provides the JVM local default context lock management mechanism for apex. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.locking.jvmlocal; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/package-info.java new file mode 100644 index 000000000..1a467275d --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/locking/package-info.java @@ -0,0 +1,26 @@ +/*- + * ============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 an implementation of context album lock management that uses a lock management plugin to lock items items in + * Apex context albums. It also provides a default JVM local lock management plugin. + */ + +package org.onap.policy.apex.context.impl.locking; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/package-info.java new file mode 100644 index 000000000..989827bd3 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============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 an implementation of APEX context schemas and APEX context albums. It provides default implementations of + * context schema handling and context album distribution, locking, and persistence. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/PersistorFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/PersistorFactory.java new file mode 100644 index 000000000..af6f922d3 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/PersistorFactory.java @@ -0,0 +1,83 @@ +/*- + * ============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.context.impl.persistence; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Persistor; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.utilities.Assertions; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class returns a persistor for the particular type of persistor mechanism that has been configured for use. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PersistorFactory { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(PersistorFactory.class); + + /** + * Return a persistor for the persistence mechanism configured for use. + * + * @param key The key for the persistor + * @return a persistor + * @throws ContextException on invalid persistor types + */ + public Persistor createPersistor(final AxArtifactKey key) throws ContextException { + LOGGER.entry("persistor factory, key=" + key); + Assertions.argumentNotNull(key, ContextException.class, "Parameter \"key\" may not be null"); + + final PersistorParameters persistorParameters = ParameterService.getParameters(PersistorParameters.class); + + // Get the class for the persistor using reflection + Object persistorObject = null; + final String pluginClass = persistorParameters.getPluginClass(); + try { + persistorObject = Class.forName(pluginClass).newInstance(); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + LOGGER.error("Apex context persistor class not found for context persistor plugin \"" + pluginClass + "\"", + e); + throw new ContextException( + "Apex context persistor class not found for context persistor plugin \"" + pluginClass + "\"", e); + } + + // Check the class is a persistor + if (!(persistorObject instanceof Persistor)) { + LOGGER.error("Specified Apex context persistor plugin class \"" + pluginClass + + "\" does not implement the ContextDistributor interface"); + throw new ContextException("Specified Apex context persistor plugin class \"" + pluginClass + + "\" does not implement the ContextDistributor interface"); + } + + // The persistor to return + final Persistor persistor = (Persistor) persistorObject; + + // Lock and load the persistor + persistor.init(key); + + LOGGER.exit("Persistor factory, key=" + key + ", selected persistor of class " + persistor.getClass()); + return persistor; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/EphemeralPersistor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/EphemeralPersistor.java new file mode 100644 index 000000000..b875978be --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/EphemeralPersistor.java @@ -0,0 +1,100 @@ +/*- + * ============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.context.impl.persistence.ephemeral; + +import java.util.Set; +import java.util.TreeSet; + +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Persistor; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; + +/** + * This class acts as an "in memory" persistor for a single JVM. It just initiates stubs the Persistor interface and + * does not persist anything. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class EphemeralPersistor implements Persistor { + + // The key of this persistor + private AxArtifactKey key; + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.Persistor#init(org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey) + */ + @Override + public void init(final AxArtifactKey persistorKey) throws ContextException { + this.key = persistorKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.Persistor#getKey() + */ + @Override + public AxArtifactKey getKey() { + return key; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.Persistor#readContextItem(org.onap.policy.apex.core.basicmodel.concepts. + * AxReferenceKey, java.lang.Class) + */ + @Override + public AxContextSchema readContextItem(final AxReferenceKey itemKey, final Class contextItemClass) { + // Can't read from this persistor as nothing is persisted + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.core.context.Persistor#readContextItems(org.onap.policy.apex.core.basicmodel.concepts. + * AxArtifactKey, java.lang.Class) + */ + @Override + public Set readContextItems(final AxArtifactKey ownerKey, final Class contextItemClass) + throws ContextException { + // No reading from persistence on the Ephemeral persistor, return an empty set + return new TreeSet<>(); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.core.context.Persistor#writeContextItem(org.onap.policy.apex.core.contextmodel.concepts. + * AxContextItem) + */ + @Override + public Object writeContextItem(final Object contextItem) { + // No writing to persistence on the Ephemeral persistor + return contextItem; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/package-info.java new file mode 100644 index 000000000..2d31d21bd --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/ephemeral/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============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 the JVM local default context persistence mechanism for APEX, which is in fact a dummy persistor that just + * stubs the Persistor interface and does not persist anything. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.persistence.ephemeral; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/package-info.java new file mode 100644 index 000000000..d450dd80c --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/persistence/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============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 an implementation of context album persistence that uses a persistence plugin to lock items items in APEX + * context albums. It also provides a default JVM local persistence plugin. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.persistence; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java new file mode 100644 index 000000000..83d1b6ba5 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/AbstractSchemaHelper.java @@ -0,0 +1,169 @@ +/*- + * ============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.context.impl.schema; + +import java.lang.reflect.Constructor; + +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.utilities.Assertions; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class implements the {@link SchemaHelper} functionality that is common across all implementations. Schema + * helpers for specific schema mechanisms specialize this class. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public abstract class AbstractSchemaHelper implements SchemaHelper { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractSchemaHelper.class); + + // The key of the user of this schema helper + private AxKey userKey = null; + + // The schema of this schema helper + private AxContextSchema schema = null; + + // The class of objects for this schema + private Class schemaClass; + + /** + * Sets the schema class for the schema, designed jots to be called by sub classes. + * + * @param schemaClass the Java class that is used to hold items of this schema + */ + protected void setSchemaClass(final Class schemaClass) { + this.schemaClass = schemaClass; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#init(org.onap.policy.apex.model.basicmodel.concepts.AxKey, + * org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema) + */ + @Override + public void init(final AxKey incomingUserKey, final AxContextSchema incomingSchema) throws ContextRuntimeException { + Assertions.argumentNotNull(incomingUserKey, ContextRuntimeException.class, "incomingUserKey may not be null"); + Assertions.argumentNotNull(incomingSchema, ContextRuntimeException.class, "incomingSchema may not be null"); + + this.userKey = incomingUserKey; + this.schema = incomingSchema; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#getKey() + */ + @Override + public AxKey getUserKey() { + return userKey; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#getSchema() + */ + @Override + public AxContextSchema getSchema() { + return schema; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#getSchemaClass() + */ + @Override + public Class getSchemaClass() { + return schemaClass; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#getSchemaObject() + */ + @Override + public Object getSchemaObject() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance() + */ + @Override + public Object createNewInstance() { + if (schemaClass == null) { + final String returnString = + userKey.getID() + ": could not create an instance, schema class for the schema is null"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + try { + return schemaClass.newInstance(); + } catch (final Exception e) { + final String returnString = + userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName() + + "\" using the default constructor \"" + schemaClass.getSimpleName() + "()\""; + LOGGER.warn(returnString, e); + throw new ContextRuntimeException(returnString, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(java.lang.String) + */ + @Override + public Object createNewInstance(final String stringValue) { + if (schemaClass == null) { + final String returnString = + userKey.getID() + ": could not create an instance, schema class for the schema is null"; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + + try { + // Find a string constructor + final Constructor stringConstructor = schemaClass.getConstructor(String.class); + + // Invoke the constructor + return stringConstructor.newInstance(stringValue); + } catch (final Exception e) { + final String returnString = + userKey.getID() + ": could not create an instance of class \"" + schemaClass.getCanonicalName() + + "\" using the string constructor \"" + schemaClass.getSimpleName() + "(String)\""; + LOGGER.warn(returnString, e); + throw new ContextRuntimeException(returnString); + } + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java new file mode 100644 index 000000000..7252d3772 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java @@ -0,0 +1,119 @@ +/*- + * ============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.context.impl.schema; + +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.parameters.SchemaHelperParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.utilities.Assertions; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class returns a {@link SchemaHelper} for the particular type of schema mechanism configured for use. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SchemaHelperFactory { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SchemaHelperFactory.class); + + /** + * Return a {@link SchemaHelper} for the particular type of schema mechanism configured for use. + * + * @param owningEntityKey The key of the entity that owns the schema helper + * @param schemaKey The key of the schema the schema helper is operating on + * @return a lock schema that can handle translation of objects in a particular schema format + * @throws ContextRuntimeException the context runtime exception + */ + public SchemaHelper createSchemaHelper(final AxKey owningEntityKey, final AxArtifactKey schemaKey) + throws ContextRuntimeException { + LOGGER.entry("schema helper factory, owningEntityKey=" + owningEntityKey); + Assertions.argumentNotNull(owningEntityKey, ContextRuntimeException.class, + "Parameter \"owningEntityKey\" may not be null"); + Assertions.argumentNotNull(schemaKey, ContextRuntimeException.class, "Parameter \"schemaKey\" may not be null"); + + // Get the schema for items in the album + final AxContextSchema schema = ModelService.getModel(AxContextSchemas.class).get(schemaKey); + if (schema == null) { + final String resultString = + "schema \"" + schemaKey.getID() + "\" for entity " + owningEntityKey.getID() + " does not exist"; + LOGGER.warn(resultString); + throw new ContextRuntimeException(resultString); + } + + // Get the schema class using the parameter service + final SchemaParameters schemaParameters = ParameterService.getParameters(SchemaParameters.class); + if (schemaParameters == null) { + final String resultString = "context schema parameters \"" + SchemaParameters.class.getCanonicalName() + + "\" not found in parameter service"; + LOGGER.warn(resultString); + throw new ContextRuntimeException(resultString); + } + + // Get the class for the schema helper from the schema parameters + final SchemaHelperParameters schemaHelperParameters = + schemaParameters.getSchemaHelperParameters(schema.getSchemaFlavour()); + if (schemaHelperParameters == null) { + final String resultString = "context schema helper parameters not found for context schema \"" + + schema.getSchemaFlavour() + "\""; + LOGGER.warn(resultString); + throw new ContextRuntimeException(resultString); + } + + // Get the class for the schema helper using reflection + Object schemaHelperObject = null; + final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass(); + try { + schemaHelperObject = Class.forName(pluginClass).newInstance(); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + final String resultString = "Apex context schema helper class not found for context schema helper plugin \"" + + pluginClass + "\""; + LOGGER.warn(resultString, e); + throw new ContextRuntimeException(resultString, e); + } + + // Check the class is a schema helper + if (!(schemaHelperObject instanceof SchemaHelper)) { + final String resultString = "Specified Apex context schema helper plugin class \"" + pluginClass + + "\" does not implement the SchemaHelper interface"; + LOGGER.warn(resultString); + throw new ContextRuntimeException(resultString); + } + + // The context schema helper to return + final SchemaHelper schemaHelper = (SchemaHelper) schemaHelperObject; + + // Lock and load the schema helper + schemaHelper.init(owningEntityKey.getKey(), schema); + + LOGGER.exit("Schema Helper factory, owningEntityKey=" + owningEntityKey + ", selected schema helper of class " + + schemaHelper.getClass()); + return schemaHelper; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java new file mode 100644 index 000000000..b89efbf91 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java @@ -0,0 +1,214 @@ +/*- + * ============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.context.impl.schema.java; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; + +import java.lang.reflect.Constructor; +import java.util.HashMap; +import java.util.Map; + +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.utilities.typeutils.TypeBuilder; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class implements translation to and from Apex distributed objects and Java objects when a Java schema is used. + * It creates schema items as Java objects and marshals and unmarshals these objects in various formats. All objects + * must be of the type of Java class defined in the schema. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaSchemaHelper extends AbstractSchemaHelper { + // Get a reference to the logger + private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelper.class); + + // This map defines the built in types in types in Java + // @formatter:off + private static final Map> BUILT_IN_MAP = new HashMap<>(); + { + BUILT_IN_MAP.put("int", Integer .TYPE); + BUILT_IN_MAP.put("long", Long .TYPE); + BUILT_IN_MAP.put("double", Double .TYPE); + BUILT_IN_MAP.put("float", Float .TYPE); + BUILT_IN_MAP.put("bool", Boolean .TYPE); + BUILT_IN_MAP.put("char", Character.TYPE); + BUILT_IN_MAP.put("byte", Byte .TYPE); + BUILT_IN_MAP.put("void", Void .TYPE); + BUILT_IN_MAP.put("short", Short .TYPE); + } + // @formatter:on + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper#init(org.onap.policy.apex.model.basicmodel. + * concepts. AxKey, org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema) + */ + @Override + public void init(final AxKey userKey, final AxContextSchema schema) throws ContextRuntimeException { + super.init(userKey, schema); + + final String javatype = schema.getSchema(); + // For Java, the schema is the Java class canonical path + + try { + setSchemaClass(TypeBuilder.getJavaTypeClass(schema.getSchema())); + } catch (final IllegalArgumentException e) { + + String resultSting = userKey.getID() + ": class/type " + schema.getSchema() + " for context schema \"" + + schema.getID() + "\" not found."; + if (JavaSchemaHelper.BUILT_IN_MAP.get(javatype) != null) { + resultSting += " Primitive types are not supported. Use the appropriate Java boxing type instead."; + } else { + resultSting += " Check the class path of the JVM"; + } + LOGGER.warn(resultSting); + throw new ContextRuntimeException(resultSting, e); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#createNewInstance(com.google.gson.JsonElement) + */ + @Override + public Object createNewInstance(final JsonElement jsonElement) { + final String elementJsonString = new Gson().toJson(jsonElement); + + return new Gson().fromJson(elementJsonString, this.getSchemaClass()); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#object2SchemaObject(java.lang.Object) + */ + @Override + public Object unmarshal(final Object object) { + if (object == null) { + return null; + } + + // If the object is an instance of the incoming object, carry on + if (object.getClass().equals(getSchemaClass())) { + return object; + } + + // For numeric types, do a numeric conversion + if (Number.class.isAssignableFrom(getSchemaClass())) { + return numericConversion(object); + } + + if (getSchemaClass().isAssignableFrom(object.getClass())) { + return object; + } else { + return stringConversion(object); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#schemaObject2Json(java.lang.Object) + */ + @Override + public String marshal2Json(final Object schemaObject) { + if (schemaObject == null) { + return "null"; + } + + // Check the incoming object is of a correct class + if (getSchemaClass().isAssignableFrom(schemaObject.getClass())) { + // Use Gson to translate the object + return new Gson().toJson(schemaObject); + } else { + final String returnString = getUserKey().getID() + ": object \"" + schemaObject.toString() + + "\" of class \"" + schemaObject.getClass().getCanonicalName() + "\" not compatible with class \"" + + getSchemaClass().getCanonicalName() + "\""; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.SchemaHelper#marshal2JsonElement(java.lang.Object) + */ + @Override + public JsonElement marshal2JsonElement(final Object schemaObject) { + // Use Gson to marshal the schema object into a Json element to return + return new Gson().toJsonTree(schemaObject, getSchemaClass()); + } + + /** + * Do a numeric conversion between numeric types. + * + * @param object The incoming numeric object + * @return The converted object + */ + private Object numericConversion(final Object object) { + // Check if the incoming object is a number, if not do a string conversion + if (object instanceof Number) { + if (getSchemaClass().isAssignableFrom(Byte.class)) { + return ((Number) object).byteValue(); + } else if (getSchemaClass().isAssignableFrom(Integer.class)) { + return ((Number) object).intValue(); + } else if (getSchemaClass().isAssignableFrom(Long.class)) { + return ((Number) object).longValue(); + } else if (getSchemaClass().isAssignableFrom(Float.class)) { + return ((Number) object).floatValue(); + } else if (getSchemaClass().isAssignableFrom(Double.class)) { + return ((Number) object).doubleValue(); + } + } + + // OK, we'll try and convert from a string representation of the incoming object + return stringConversion(object); + } + + /** + * Do a string conversion to the class type. + * + * @param object The incoming numeric object + * @return The converted object + */ + private Object stringConversion(final Object object) { + // OK, we'll try and convert from a string representation of the incoming object + try { + final Constructor stringConstructor = getSchemaClass().getConstructor(String.class); + return stringConstructor.newInstance(object.toString()); + } catch (final Exception e) { + final String returnString = getUserKey().getID() + ": object \"" + object.toString() + "\" of class \"" + + object.getClass().getCanonicalName() + "\" not compatible with class \"" + + getSchemaClass().getCanonicalName() + "\""; + LOGGER.warn(returnString); + throw new ContextRuntimeException(returnString); + } + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParameters.java new file mode 100644 index 000000000..18339d8db --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelperParameters.java @@ -0,0 +1,38 @@ +/*- + * ============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.context.impl.schema.java; + +import org.onap.policy.apex.context.parameters.SchemaHelperParameters; + +/** + * The Schema helper parameter class for the Java schema helper is an empty parameter class that acts as a placeholder. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class JavaSchemaHelperParameters extends SchemaHelperParameters { + + /** + * The Constructor. + */ + public JavaSchemaHelperParameters() { + this.setSchemaHelperPluginClass(JavaSchemaHelper.class.getCanonicalName()); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/package-info.java new file mode 100644 index 000000000..052981379 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/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========================================================= + */ + +/** + * Provides the Java schema helper for APEX, which manages context items that are Java objects. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.schema.java; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/package-info.java new file mode 100644 index 000000000..458a86afe --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/package-info.java @@ -0,0 +1,29 @@ +/*- + * ============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 an implementation of context schema handling that uses a schema helper plugin to manage schemas and to + * create, marshal, and unmarshal context items in various formats for APEX context. It also provides a default java + * schema handler plugin. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.impl.schema; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/ContextMonitor.java b/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/ContextMonitor.java new file mode 100644 index 000000000..8e34ecb1b --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/ContextMonitor.java @@ -0,0 +1,210 @@ +/*- + * ============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.context.monitoring; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class is used to monitor context creates, deletes, gets, sets, locks and unlocks on context items in Apex + * context albums. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ContextMonitor { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextMonitor.class); + + /** + * Monitor an initiation on a context item. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param value The value of the item + */ + public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final Object value) { + LOGGER.trace(monitor("INIT", null, albumKey, schemaKey, name, value)); + } + + /** + * Monitor an initiation on a context item. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param value The value of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorInit(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final Object value, final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("INIT", userArtifactStack, albumKey, schemaKey, name, value)); + } + + /** + * Monitor a deletion on a context item. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param value The value of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorDelete(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final Object value, final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("DEL", userArtifactStack, albumKey, schemaKey, name, value)); + } + + /** + * Monitor get on a context item. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param value The value of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorGet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final Object value, final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("GET", userArtifactStack, albumKey, schemaKey, name, value)); + } + + /** + * Monitor set on a context item. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param value The value of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorSet(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final Object value, final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("SET", userArtifactStack, albumKey, schemaKey, name, value)); + } + + /** + * Monitor a read lock on a key. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorReadLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("READLOCK", userArtifactStack, albumKey, schemaKey, name, null)); + } + + /** + * Monitor a write lock on a key. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorWriteLock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("WRITELOCK", userArtifactStack, albumKey, schemaKey, name, null)); + } + + /** + * Monitor a read unlock on a key. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorReadUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("READUNLOCK", userArtifactStack, albumKey, schemaKey, name, null)); + } + + /** + * Monitor a write unlock on a key. + * + * @param albumKey The item album + * @param schemaKey The item schema + * @param name The name of the item + * @param userArtifactStack the keys of the artifacts using the context map at the moment + */ + public void monitorWriteUnlock(final AxArtifactKey albumKey, final AxArtifactKey schemaKey, final String name, + final AxConcept[] userArtifactStack) { + LOGGER.trace(monitor("WRITEUNLOCK", userArtifactStack, albumKey, schemaKey, name, null)); + } + + /** + * Monitor the user artifact stack. + * + * @param preamble the preamble + * @param userArtifactStack The user stack to print + * @param albumKey the album key + * @param schemaKey the schema key + * @param name the name + * @param value the value + * @return the string + */ + private String monitor(final String preamble, final AxConcept[] userArtifactStack, final AxArtifactKey albumKey, + final AxArtifactKey schemaKey, final String name, final Object value) { + final StringBuilder builder = new StringBuilder(); + + builder.append(preamble); + builder.append(",["); + + if (userArtifactStack != null) { + boolean first = true; + for (final AxConcept stackKey : userArtifactStack) { + if (first) { + first = false; + } else { + builder.append(','); + } + if (stackKey instanceof AxArtifactKey) { + builder.append(((AxArtifactKey) stackKey).getID()); + } else if (stackKey instanceof AxReferenceKey) { + builder.append(((AxReferenceKey) stackKey).getID()); + } else { + builder.append(stackKey.toString()); + } + } + } + builder.append("],"); + + builder.append(albumKey.getID()); + builder.append(','); + builder.append(schemaKey.getID()); + builder.append(','); + builder.append(name); + + if (value != null) { + builder.append(','); + builder.append(value.toString()); + } + + return builder.toString(); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/package-info.java new file mode 100644 index 000000000..2f8ac8281 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/monitoring/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========================================================= + */ + +/** + * Monitors all creation, deletion, get, set, lock, and unlock operations on items in APEX context albums. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.monitoring; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/package-info.java new file mode 100644 index 000000000..8707ae36b --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/package-info.java @@ -0,0 +1,29 @@ +/*- + * ============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 the context handling for APEX. It provides the infrastructure and generic implementation for context schemas + * and context albums. Schema handlers and context album handlers are implemented as plugins that implement interfaces + * defined in this package. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context; diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/ContextParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/ContextParameters.java new file mode 100644 index 000000000..e50cc60b4 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/ContextParameters.java @@ -0,0 +1,146 @@ +/*- + * ============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.context.parameters; + +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * Bean class to hold parameters for context handling in Apex. This class contains all the context parameters for schema + * handling, distribution, locking, and persistence of context albums. + *

+ * The following parameters are defined: + *

    + *
  1. flushPeriod: Context is flushed to any persistor plugin that is defined periodically, and the period for flushing + * is the flush period. + *
  2. distributorParameters: The parameters (a {@link distributorParameters} instance) for the distributor plugin that + * is being used for context album distribution + *
  3. schemaParameters: The parameters (a {@link SchemaParameters} instance) for the schema plugin that is being used + * for context album schemas + *
  4. lockManagerParameters: The parameters (a {@link LockManagerParameters} instance) for the locking mechanism plugin + * that is being used for context album locking + *
  5. persistorParameters: The parameters (a {@link PersistorParameters} instance) for the persistence plugin that is + * being used for context album persistence + *
+ * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ContextParameters extends AbstractParameters { + // @formatter:off + // Plugin Parameters + private DistributorParameters distributorParameters = new DistributorParameters(); + private SchemaParameters schemaParameters = new SchemaParameters(); + private LockManagerParameters lockManagerParameters = new LockManagerParameters(); + private PersistorParameters persistorParameters = new PersistorParameters(); + // @formatter:on + + /** + * Constructor to create a context parameters instance and register the instance with the parameter service. + */ + public ContextParameters() { + super(ContextParameters.class.getCanonicalName()); + ParameterService.registerParameters(ContextParameters.class, this); + } + + /** + * Gets the distributor parameters. + * + * @return the distributor parameters + */ + public DistributorParameters getDistributorParameters() { + return distributorParameters; + } + + /** + * Sets the distributor parameters. + * + * @param distributorParameters the distributor parameters + */ + public void setDistributorParameters(final DistributorParameters distributorParameters) { + this.distributorParameters = distributorParameters; + } + + /** + * Gets the schema parameters. + * + * @return the schema parameters + */ + public SchemaParameters getSchemaParameters() { + return schemaParameters; + } + + /** + * Sets the schema parameters. + * + * @param schemaParameters the schema parameters + */ + public void setSchemaParameters(final SchemaParameters schemaParameters) { + this.schemaParameters = schemaParameters; + } + + /** + * Gets the lock manager parameters. + * + * @return the lock manager parameters + */ + public LockManagerParameters getLockManagerParameters() { + return lockManagerParameters; + } + + /** + * Sets the lock manager parameters. + * + * @param lockManagerParameters the lock manager parameters + */ + public void setLockManagerParameters(final LockManagerParameters lockManagerParameters) { + this.lockManagerParameters = lockManagerParameters; + } + + /** + * Gets the persistor parameters. + * + * @return the persistor parameters + */ + public PersistorParameters getPersistorParameters() { + return persistorParameters; + } + + /** + * Sets the persistor parameters. + * + * @param persistorParameters the persistor parameters + */ + public void setPersistorParameters(final PersistorParameters persistorParameters) { + this.persistorParameters = persistorParameters; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.service.AbstractParameters#toString() + */ + @Override + public String toString() { + return "ContextParameters [distributorParameters=" + distributorParameters + ", schemaParameters=" + + schemaParameters + ", lockManagerParameters=" + lockManagerParameters + ", persistorParameters=" + + persistorParameters + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/DistributorParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/DistributorParameters.java new file mode 100644 index 000000000..147e4eb35 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/DistributorParameters.java @@ -0,0 +1,85 @@ +/*- + * ============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.context.parameters; + +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * An empty distributor parameter class that may be specialized by context distributor plugins that require plugin + * specific parameters. The class defines the default distributor plugin as the JVM local distributor. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class DistributorParameters extends AbstractParameters { + /** The default distributor makes context albums available to all threads in a single JVM. */ + public static final String DEFAULT_DISTRIBUTOR_PLUGIN_CLASS = + "org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"; + + // Plugin class names + private String pluginClass = DEFAULT_DISTRIBUTOR_PLUGIN_CLASS; + + /** + * Constructor to create a distributor parameters instance and register the instance with the parameter service. + */ + public DistributorParameters() { + super(DistributorParameters.class.getCanonicalName()); + ParameterService.registerParameters(DistributorParameters.class, this); + } + + /** + * Constructor to create a distributor parameters instance with the name of a sub class of this class and register + * the instance with the parameter service. + * + * @param parameterClassName the class name of a sub class of this class + */ + public DistributorParameters(final String parameterClassName) { + super(parameterClassName); + } + + /** + * Gets the plugin class. + * + * @return the plugin class + */ + public String getPluginClass() { + return pluginClass; + } + + /** + * Sets the plugin class. + * + * @param pluginClass the plugin class + */ + public void setPluginClass(final String pluginClass) { + this.pluginClass = pluginClass; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.service.AbstractParameters#toString() + */ + @Override + public String toString() { + return "DistributorParameters [pluginClass=" + pluginClass + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/LockManagerParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/LockManagerParameters.java new file mode 100644 index 000000000..93e422357 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/LockManagerParameters.java @@ -0,0 +1,85 @@ +/*- + * ============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.context.parameters; + +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * An empty lock manager parameter class that may be specialized by context lock manager plugins that require plugin + * specific parameters. The class defines the default lock manager plugin as the JVM local lock manager. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class LockManagerParameters extends AbstractParameters { + /** The default lock manager can lock context album instance across all threads in a single JVM. */ + public static final String DEFAULT_LOCK_MANAGER_PLUGIN_CLASS = + "org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager"; + + // Plugin class names + private String pluginClass = DEFAULT_LOCK_MANAGER_PLUGIN_CLASS; + + /** + * Constructor to create a lock manager parameters instance and register the instance with the parameter service. + */ + public LockManagerParameters() { + super(LockManagerParameters.class.getCanonicalName()); + ParameterService.registerParameters(LockManagerParameters.class, this); + } + + /** + * Constructor to create a lock manager parameters instance with the name of a sub class of this class and register + * the instance with the parameter service. + * + * @param parameterClassName the class name of a sub class of this class + */ + public LockManagerParameters(final String parameterClassName) { + super(parameterClassName); + } + + /** + * Gets the plugin class. + * + * @return the plugin class + */ + public String getPluginClass() { + return pluginClass; + } + + /** + * Sets the plugin class. + * + * @param pluginClass the plugin class + */ + public void setPluginClass(final String pluginClass) { + this.pluginClass = pluginClass; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.service.AbstractParameters#toString() + */ + @Override + public String toString() { + return "LockManagerParameters [pluginClass=" + pluginClass + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/PersistorParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/PersistorParameters.java new file mode 100644 index 000000000..c9b0d85f8 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/PersistorParameters.java @@ -0,0 +1,120 @@ +/*- + * ============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.context.parameters; + +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * A persistor parameter class that may be specialized by context persistor plugins that require plugin specific + * parameters. + *

+ * The following parameters are defined: + *

    + *
  1. pluginClass: the persistor plugin as the JVM local dummy ephemeral persistor + *
  2. flushPeriod: Context is flushed to any persistor plugin that is defined periodically, and the period for flushing + * is the flush period. + *
+ * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PersistorParameters extends AbstractParameters { + /** The default persistor is a dummy persistor that stubs the Persistor interface. */ + public static final String DEFAULT_PERSISTOR_PLUGIN_CLASS = + "org.onap.policy.apex.context.impl.persistence.ephemeral.EphemeralPersistor"; + + /** Default periodic flushing interval, 5 minutes in milliseconds. */ + public static final long DEFAULT_FLUSH_PERIOD = 300000; + + // Plugin class names + private String pluginClass = DEFAULT_PERSISTOR_PLUGIN_CLASS; + + // Parameters for flushing + private long flushPeriod = DEFAULT_FLUSH_PERIOD; + + /** + * Constructor to create a persistor parameters instance and register the instance with the parameter service. + */ + public PersistorParameters() { + super(PersistorParameters.class.getCanonicalName()); + ParameterService.registerParameters(PersistorParameters.class, this); + } + + /** + * Constructor to create a persistor parameters instance with the name of a sub class of this class and register the + * instance with the parameter service. + * + * @param parameterClassName the class name of a sub class of this class + */ + public PersistorParameters(final String parameterClassName) { + super(parameterClassName); + } + + /** + * Gets the plugin class. + * + * @return the plugin class + */ + public String getPluginClass() { + return pluginClass; + } + + /** + * Sets the plugin class. + * + * @param pluginClass the plugin class + */ + public void setPluginClass(final String pluginClass) { + this.pluginClass = pluginClass; + } + + /** + * Gets the flush period in milliseconds. + * + * @return the flush period + */ + public long getFlushPeriod() { + return flushPeriod; + } + + /** + * Sets the flush period in milliseconds. + * + * @param flushPeriod the flush period + */ + public void setFlushPeriod(final long flushPeriod) { + if (flushPeriod <= 0) { + this.flushPeriod = DEFAULT_FLUSH_PERIOD; + } else { + this.flushPeriod = flushPeriod; + } + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.service.AbstractParameters#toString() + */ + @Override + public String toString() { + return "PersistorParameters [pluginClass=" + pluginClass + ", flushPeriod=" + flushPeriod + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaHelperParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaHelperParameters.java new file mode 100644 index 000000000..9ccd431a5 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaHelperParameters.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.context.parameters; + +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * An empty schema helper parameter class that may be specialized by context schema helper plugins that require plugin + * specific parameters. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SchemaHelperParameters extends AbstractParameters { + // Schema helper plugin class for the schema + private String schemaHelperPluginClass; + + /** + * Constructor to create a schema helper parameters instance and register the instance with the parameter service. + */ + public SchemaHelperParameters() { + super(SchemaHelperParameters.class.getCanonicalName()); + ParameterService.registerParameters(SchemaHelperParameters.class, this); + } + + /** + * Constructor to create a schema helper parameters instance with the name of a sub class of this class and register + * the instance with the parameter service. + * + * @param parameterClassName the class name of a sub class of this class + */ + public SchemaHelperParameters(final String parameterClassName) { + super(parameterClassName); + } + + /** + * Gets the schema helper plugin class. + * + * @return the schema helper plugin class + */ + public String getSchemaHelperPluginClass() { + return schemaHelperPluginClass; + } + + /** + * Sets the schema helper plugin class. + * + * @param pluginClass the schema helper plugin class + */ + public void setSchemaHelperPluginClass(final String pluginClass) { + schemaHelperPluginClass = pluginClass; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.service.AbstractParameters#toString() + */ + @Override + public String toString() { + return "SchemaHelperParameters [schemaHelperPluginClass=" + schemaHelperPluginClass + "]"; + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaParameters.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaParameters.java new file mode 100644 index 000000000..858a6a3f7 --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/SchemaParameters.java @@ -0,0 +1,87 @@ +/*- + * ============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.context.parameters; + +import java.util.Map; +import java.util.TreeMap; + +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; + +/** + * Bean class holding schema parameters for schemas and their helpers. As more than one schema can be used in Apex + * simultaneously, this class is used to hold the schemas that are defined in a given Apex system and to get the schema + * helper plugin parameters {@link SchemaHelperParameters} for each schema. + *

+ * The default {@code Java} schema is always defined and its parameters are held in a {@link JavaSchemaHelperParameters} + * instance. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class SchemaParameters extends AbstractParameters { + /** The Java schema flavour is always available for use. */ + public static final String DEFAULT_SCHEMA_FLAVOUR = "Java"; + + // A map of parameters for executors of various logic types + private Map schemaHelperParameterMap; + + /** + * Constructor to create a distributor parameters instance and register the instance with the parameter service. + */ + public SchemaParameters() { + super(SchemaParameters.class.getCanonicalName()); + ParameterService.registerParameters(SchemaParameters.class, this); + + schemaHelperParameterMap = new TreeMap<>(); + + // The default schema helper + schemaHelperParameterMap.put(DEFAULT_SCHEMA_FLAVOUR, new JavaSchemaHelperParameters()); + } + + /** + * Gets a map of the schemas and schema helper parameters that are defined. + * + * @return the schema helper parameter map + */ + public Map getSchemaHelperParameterMap() { + return schemaHelperParameterMap; + } + + /** + * Sets the map of the schemas and schema helper parameters. + * + * @param schemaHelperParameterMap the schema helper parameter map + */ + public void setSchemaHelperParameterMap(final Map schemaHelperParameterMap) { + this.schemaHelperParameterMap = schemaHelperParameterMap; + } + + /** + * Gets the schema helper parameters for a given context schema flavour. + * + * @param schemaFlavour the schema flavour for which to get the schema helper parameters + * @return the schema helper parameters for the given schema flavour + */ + public SchemaHelperParameters getSchemaHelperParameters(final String schemaFlavour) { + return schemaHelperParameterMap.get(schemaFlavour); + } +} diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/package-info.java b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/package-info.java new file mode 100644 index 000000000..52a42709c --- /dev/null +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/parameters/package-info.java @@ -0,0 +1,28 @@ +/*- + * ============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========================================================= + */ + +/** + * Holds the definitions of generic parameters for schema helpers, distributors, lock managers, and persistors in APEX. + * All the parameter definitions can be specialized to provide parameters to APEX plugins. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.parameters; diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java new file mode 100644 index 000000000..9c2c685c3 --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/TestInstanceCreation.java @@ -0,0 +1,96 @@ +/*- + * ============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.context.impl.schema.java; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + * @version + */ +public class TestInstanceCreation { + private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1"); + private AxContextSchemas schemas; + + @Before + public void initTest() { + schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1")); + ModelService.registerModel(AxContextSchemas.class, schemas); + new SchemaParameters(); + } + + @Test + public void testNullEncoding() { + final AxContextSchema javaBooleanSchema = + new AxContextSchema(new AxArtifactKey("Boolean", "0.0.1"), "Java", "java.lang.Boolean"); + final AxContextSchema javaLongSchema = + new AxContextSchema(new AxArtifactKey("Long", "0.0.1"), "Java", "java.lang.Long"); + final AxContextSchema javaStringSchema = + new AxContextSchema(new AxArtifactKey("String", "0.0.1"), "Java", "java.lang.String"); + + schemas.getSchemasMap().put(javaBooleanSchema.getKey(), javaBooleanSchema); + schemas.getSchemasMap().put(javaLongSchema.getKey(), javaLongSchema); + schemas.getSchemasMap().put(javaStringSchema.getKey(), javaStringSchema); + + final SchemaHelper schemaHelper0 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaBooleanSchema.getKey()); + final SchemaHelper schemaHelper1 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaLongSchema.getKey()); + final SchemaHelper schemaHelper2 = + new SchemaHelperFactory().createSchemaHelper(testKey, javaStringSchema.getKey()); + + try { + schemaHelper0.createNewInstance(); + fail("this test should throw an exception here"); + } catch (final Exception e) { + assertEquals( + "AvroTest:0.0.1: could not create an instance of class \"java.lang.Boolean\" using the default constructor \"Boolean()\"", + e.getMessage()); + } + assertEquals(true, schemaHelper0.createNewInstance("true")); + + + try { + schemaHelper1.createNewInstance(); + fail("this test should throw an exception here"); + } catch (final Exception e) { + assertEquals( + "AvroTest:0.0.1: could not create an instance of class \"java.lang.Long\" using the default constructor \"Long()\"", + e.getMessage()); + } + assertEquals(65536L, schemaHelper1.createNewInstance("65536")); + + assertEquals("", schemaHelper2.createNewInstance()); + assertEquals("true", schemaHelper2.createNewInstance("true")); + } +} diff --git a/context/context-management/src/test/resources/logback-test.xml b/context/context-management/src/test/resources/logback-test.xml new file mode 100644 index 000000000..51398da38 --- /dev/null +++ b/context/context-management/src/test/resources/logback-test.xml @@ -0,0 +1,70 @@ + + + + + + Apex + + + + + + + %d %contextName [%t] %level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + ${LOG_DIR}/apex.log + + %d %-5relative [procId=${processId}] [%thread] %-5level + %logger{26} - %msg %n %ex{full} + + + + + ${LOG_DIR}/apex_ctxt.log + + %d %-5relative [procId=${processId}] [%thread] %-5level + %logger{26} - %msg %n %ex{full} + + + + + + + + + + + diff --git a/context/context-test/pom.xml b/context/context-test/pom.xml new file mode 100644 index 000000000..ded707adb --- /dev/null +++ b/context/context-test/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp.context + context + 2.0.0-SNAPSHOT + + + context-test + ${project.artifactId} + [${project.parent.artifactId}] module to hold test context date for testing + + + + org.onap.policy.apex-pdp.model + context-model + ${project.version} + + + org.onap.policy.apex-pdp.context + context-management + ${project.version} + + + com.google.code.gson + gson + + + org.apache.derby + derby + ${version.derby} + test + + + \ No newline at end of file diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem000.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem000.java new file mode 100644 index 000000000..49e527822 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem000.java @@ -0,0 +1,114 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem000. + */ +public class TestContextItem000 implements Serializable { + private static final int HASH_PRIME_1 = 31; + private static final int HASH_PRIME_2 = 1231; + private static final int HASH_PRIME_3 = 1237; + + private static final long serialVersionUID = 7241008665286367796L; + + private boolean flag = false;; + + /** + * The Constructor. + */ + public TestContextItem000() {} + + /** + * The Constructor. + * + * @param flag the flag + */ + public TestContextItem000(final Boolean flag) { + this.flag = flag; + } + + /** + * Gets the flag. + * + * @return the flag + */ + public boolean getFlag() { + return flag; + } + + /** + * Sets the flag. + * + * @param flag the flag + */ + public void setFlag(final boolean flag) { + this.flag = flag; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + (flag ? HASH_PRIME_2 : HASH_PRIME_3); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem000 other = (TestContextItem000) obj; + if (flag != other.flag) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem000 [flag=" + flag + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem001.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem001.java new file mode 100644 index 000000000..5d41269fe --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem001.java @@ -0,0 +1,112 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem001. + */ +public class TestContextItem001 implements Serializable { + private static final long serialVersionUID = 1361938145823720386L; + + private static final int HASH_PRIME_1 = 31; + + private byte byteValue = 0; + + /** + * The Constructor. + */ + public TestContextItem001() {} + + /** + * The Constructor. + * + * @param byteValue the byte value + */ + public TestContextItem001(final Byte byteValue) { + this.byteValue = byteValue; + } + + /** + * Gets the byte value. + * + * @return the byte value + */ + public byte getByteValue() { + return byteValue; + } + + /** + * Sets the byte value. + * + * @param byteValue the byte value + */ + public void setByteValue(final byte byteValue) { + this.byteValue = byteValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + byteValue; + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem001 other = (TestContextItem001) obj; + if (byteValue != other.byteValue) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem001 [byteValue=" + byteValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem002.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem002.java new file mode 100644 index 000000000..3b5242f6b --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem002.java @@ -0,0 +1,121 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem002. + */ +public class TestContextItem002 implements Serializable { + private static final long serialVersionUID = -8978435658277900984L; + + private static final int HASH_PRIME_1 = 31; + + private int intValue = 0; + + /** + * The Constructor. + */ + public TestContextItem002() {} + + /** + * The Constructor. + * + * @param intValue the int value + */ + public TestContextItem002(final Integer intValue) { + this.intValue = intValue; + } + + /** + * The Constructor. + * + * @param original the original + */ + public TestContextItem002(final TestContextItem002 original) { + this.intValue = original.intValue; + } + + /** + * Gets the int value. + * + * @return the int value + */ + public int getIntValue() { + return intValue; + } + + /** + * Sets the int value. + * + * @param intValue the int value + */ + public void setIntValue(final int intValue) { + this.intValue = intValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + intValue; + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem002 other = (TestContextItem002) obj; + if (intValue != other.intValue) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem002 [intValue=" + intValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem003.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem003.java new file mode 100644 index 000000000..e7532ecd8 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem003.java @@ -0,0 +1,113 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem003. + */ +public class TestContextItem003 implements Serializable { + private static final long serialVersionUID = 3599267534512489386L; + + private static final int HASH_PRIME_1 = 31; + private static final int FOUR_BYTES = 33; + + private long longValue = 0; + + /** + * The Constructor. + */ + public TestContextItem003() {} + + /** + * The Constructor. + * + * @param longValue the long value + */ + public TestContextItem003(final Long longValue) { + this.longValue = longValue; + } + + /** + * Gets the long value. + * + * @return the long value + */ + public long getLongValue() { + return longValue; + } + + /** + * Sets the long value. + * + * @param longValue the long value + */ + public void setLongValue(final long longValue) { + this.longValue = longValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + (int) (longValue ^ (longValue >>> FOUR_BYTES)); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem003 other = (TestContextItem003) obj; + if (longValue != other.longValue) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem003 [longValue=" + longValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem004.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem004.java new file mode 100644 index 000000000..1370db17f --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem004.java @@ -0,0 +1,112 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem004. + */ +public class TestContextItem004 implements Serializable { + private static final long serialVersionUID = -3359180576903272400L; + + private static final int HASH_PRIME_1 = 31; + + private float floatValue = 0; + + /** + * The Constructor. + */ + public TestContextItem004() {} + + /** + * The Constructor. + * + * @param floatValue the float value + */ + public TestContextItem004(final Float floatValue) { + this.floatValue = floatValue; + } + + /** + * Gets the float value. + * + * @return the float value + */ + public float getFloatValue() { + return floatValue; + } + + /** + * Sets the float value. + * + * @param floatValue the float value + */ + public void setFloatValue(final float floatValue) { + this.floatValue = floatValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + Float.floatToIntBits(floatValue); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem004 other = (TestContextItem004) obj; + if (Float.floatToIntBits(floatValue) != Float.floatToIntBits(other.floatValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem004 [floatValue=" + floatValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem005.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem005.java new file mode 100644 index 000000000..d8a3352ce --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem005.java @@ -0,0 +1,115 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem005. + */ +public class TestContextItem005 implements Serializable { + private static final long serialVersionUID = -2958758261076734821L; + + private static final int HASH_PRIME_1 = 31; + private static final int FOUR_BYTES = 32; + + private double doubleValue = 0; + + /** + * The Constructor. + */ + public TestContextItem005() {} + + /** + * The Constructor. + * + * @param doubleValue the double value + */ + public TestContextItem005(final Double doubleValue) { + this.doubleValue = doubleValue; + } + + /** + * Gets the double value. + * + * @return the double value + */ + public double getDoubleValue() { + return doubleValue; + } + + /** + * Sets the double value. + * + * @param doubleValue the double value + */ + public void setDoubleValue(final double doubleValue) { + this.doubleValue = doubleValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + long temp; + temp = Double.doubleToLongBits(doubleValue); + result = prime * result + (int) (temp ^ (temp >>> FOUR_BYTES)); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem005 other = (TestContextItem005) obj; + if (Double.doubleToLongBits(doubleValue) != Double.doubleToLongBits(other.doubleValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem005 [doubleValue=" + doubleValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem006.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem006.java new file mode 100644 index 000000000..5fd2f9d6b --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem006.java @@ -0,0 +1,116 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem006. + */ +public class TestContextItem006 implements Serializable { + private static final long serialVersionUID = -1074772190611125121L; + + private static final int HASH_PRIME_1 = 31; + + private String stringValue = ""; + + /** + * The Constructor. + */ + public TestContextItem006() {} + + /** + * The Constructor. + * + * @param stringValue the string value + */ + public TestContextItem006(final String stringValue) { + this.stringValue = stringValue; + } + + /** + * Gets the string value. + * + * @return the string value + */ + public String getStringValue() { + return stringValue; + } + + /** + * Sets the string value. + * + * @param stringValue the string value + */ + public void setStringValue(final String stringValue) { + this.stringValue = stringValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem006 other = (TestContextItem006) obj; + if (stringValue == null) { + if (other.stringValue != null) { + return false; + } + } else if (!stringValue.equals(other.stringValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem006 [stringValue=" + stringValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem007.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem007.java new file mode 100644 index 000000000..21c6f8cdf --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem007.java @@ -0,0 +1,116 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestContextItem007. + */ +public class TestContextItem007 implements Serializable { + private static final long serialVersionUID = -1029406737866392421L; + + private static final int HASH_PRIME_1 = 31; + + private Long longValue = new Long(0); + + /** + * The Constructor. + */ + public TestContextItem007() {} + + /** + * The Constructor. + * + * @param longValue the long value + */ + public TestContextItem007(final Long longValue) { + this.longValue = longValue; + } + + /** + * Gets the long value. + * + * @return the long value + */ + public Long getLongValue() { + return longValue; + } + + /** + * Sets the long value. + * + * @param longValue the long value + */ + public void setLongValue(final Long longValue) { + this.longValue = longValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((longValue == null) ? 0 : longValue.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem007 other = (TestContextItem007) obj; + if (longValue == null) { + if (other.longValue != null) { + return false; + } + } else if (!longValue.equals(other.longValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem007 [longValue=" + longValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem008.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem008.java new file mode 100644 index 000000000..82a02a80e --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem008.java @@ -0,0 +1,258 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +/** + * The Class TestContextItem008. + */ +public class TestContextItem008 implements Serializable { + private static final long serialVersionUID = -6984963129968805460L; + + private static final int HASH_PRIME_1 = 31; + private static final int FOUR_BYTES = 32; + + private long time; + + private int year; + private int month; + private int day; + private int hour; + private int minute; + private int second; + private int milliSecond; + + /** + * The Constructor. + */ + public TestContextItem008() { + this(new Date(System.currentTimeMillis())); + } + + /** + * The Constructor. + * + * @param dateValue the date value + */ + public TestContextItem008(final Date dateValue) { + setDateValue(dateValue.getTime()); + } + + /** + * The Constructor. + * + * @param time the time + */ + public TestContextItem008(final long time) { + setDateValue(time); + } + + /** + * Gets the time. + * + * @return the time + */ + public long getTime() { + return time; + } + + /** + * Gets the year. + * + * @return the year + */ + public int getYear() { + return year; + } + + /** + * Gets the month. + * + * @return the month + */ + public int getMonth() { + return month; + } + + /** + * Gets the day. + * + * @return the day + */ + public int getDay() { + return day; + } + + /** + * Gets the hour. + * + * @return the hour + */ + public int getHour() { + return hour; + } + + /** + * Gets the minute. + * + * @return the minute + */ + public int getMinute() { + return minute; + } + + /** + * Gets the second. + * + * @return the second + */ + public int getSecond() { + return second; + } + + /** + * Gets the milli second. + * + * @return the milli second + */ + public int getMilliSecond() { + return milliSecond; + } + + /** + * Gets the date value. + * + * @return the date value + */ + public Date getDateValue() { + return new Date(time); + } + + /** + * Sets the date value. + * + * @param dateValue the date value + */ + public void setDateValue(final Date dateValue) { + setDateValue(dateValue.getTime()); + } + + /** + * Sets the date value. + * + * @param dateValue the date value + */ + public void setDateValue(final long dateValue) { + this.time = dateValue; + + final Calendar calendar = Calendar.getInstance(); + calendar.setTimeZone(TimeZone.getTimeZone("UTC")); + calendar.setTimeInMillis(time); + + year = calendar.get(Calendar.YEAR); + month = calendar.get(Calendar.MONTH); + day = calendar.get(Calendar.DAY_OF_MONTH); + hour = calendar.get(Calendar.HOUR); + minute = calendar.get(Calendar.MINUTE); + second = calendar.get(Calendar.SECOND); + milliSecond = calendar.get(Calendar.MILLISECOND); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + day; + result = prime * result + hour; + result = prime * result + milliSecond; + result = prime * result + minute; + result = prime * result + month; + result = prime * result + second; + result = prime * result + (int) (time ^ (time >>> FOUR_BYTES)); + result = prime * result + year; + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem008 other = (TestContextItem008) obj; + if (day != other.day) { + return false; + } + if (hour != other.hour) { + return false; + } + if (milliSecond != other.milliSecond) { + return false; + } + if (minute != other.minute) { + return false; + } + if (month != other.month) { + return false; + } + if (second != other.second) { + return false; + } + if (time != other.time) { + return false; + } + if (year != other.year) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem008 [time=" + time + ", year=" + year + ", month=" + month + ", day=" + day + ", hour=" + + hour + ", minute=" + minute + ", second=" + second + ", milliSecond=" + milliSecond + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem009.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem009.java new file mode 100644 index 000000000..f3e89535f --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem009.java @@ -0,0 +1,186 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; +import java.util.TimeZone; + +/** + * The Class TestContextItem009. + */ +public class TestContextItem009 implements Serializable { + private static final long serialVersionUID = 5604426823170331706L; + + private static final int HASH_PRIME_1 = 31; + private static final int HASH_PRIME_2 = 1231; + private static final int HASH_PRIME_3 = 1237; + + private TestContextItem008 dateValue = new TestContextItem008(System.currentTimeMillis()); + private String timeZoneString = TimeZone.getTimeZone("Europe/Dublin").getDisplayName(); + private boolean dst = false; + + /** + * The Constructor. + */ + public TestContextItem009() { + dst = true; + } + + /** + * The Constructor. + * + * @param dateValue the date value + * @param tzValue the tz value + * @param dst the dst + */ + public TestContextItem009(final TestContextItem008 dateValue, final String tzValue, final boolean dst) { + this.dateValue = dateValue; + this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); + this.dst = dst; + } + + /** + * The Constructor. + * + * @param original the original + */ + public TestContextItem009(final TestContextItem009 original) { + this.dateValue = original.dateValue; + this.timeZoneString = original.timeZoneString; + this.dst = original.dst; + } + + /** + * Gets the date value. + * + * @return the date value + */ + public TestContextItem008 getDateValue() { + return dateValue; + } + + /** + * Sets the date value. + * + * @param dateValue the date value + */ + public void setDateValue(final TestContextItem008 dateValue) { + this.dateValue = dateValue; + } + + /** + * Gets the TZ value. + * + * @return the TZ value + */ + public String getTZValue() { + return timeZoneString; + } + + /** + * Sets the TZ value. + * + * @param tzValue the TZ value + */ + public void setTZValue(final String tzValue) { + this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); + } + + /** + * Gets the DST. + * + * @return the dst + */ + public boolean getDST() { + return dst; + } + + /** + * Sets the DST. + * + * @param newDst the dst + */ + public void setDST(final boolean newDst) { + this.dst = newDst; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((dateValue == null) ? 0 : dateValue.hashCode()); + result = prime * result + (dst ? HASH_PRIME_2 : HASH_PRIME_3); + result = prime * result + ((timeZoneString == null) ? 0 : timeZoneString.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem009 other = (TestContextItem009) obj; + if (dateValue == null) { + if (other.dateValue != null) { + return false; + } + } else if (!dateValue.equals(other.dateValue)) { + return false; + } + if (dst != other.dst) { + return false; + } + if (timeZoneString == null) { + if (other.timeZoneString != null) { + return false; + } + } else if (!timeZoneString.equals(other.timeZoneString)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem009 [dateValue=" + dateValue + ", tzValue=" + timeZoneString + ", dst=" + dst + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00A.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00A.java new file mode 100644 index 000000000..db380cc7a --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00A.java @@ -0,0 +1,261 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; +import java.util.Locale; +import java.util.TimeZone; + +/** + * The Class TestContextItem00A. + */ +public class TestContextItem00A implements Serializable { + private static final long serialVersionUID = -6579903685538233754L; + + private static final int HASH_PRIME_1 = 31; + private static final int HASH_PRIME_2 = 1231; + private static final int HASH_PRIME_3 = 1237; + + private TestContextItem008 dateValue = new TestContextItem008(System.currentTimeMillis()); + private String timeZoneString = TimeZone.getTimeZone("Europe/Dublin").getDisplayName(); + private boolean dst = false; + private int utcOffset = 0; + private String localeLanguage = Locale.ENGLISH.getLanguage(); + private String localeCountry = Locale.ENGLISH.getCountry(); + + /** + * The Constructor. + */ + public TestContextItem00A() {} + + /** + * The Constructor. + * + * @param dateValue the date value + * @param tzValue the tz value + * @param dst the dst + * @param utcOffset the utc offset + * @param language the language + * @param country the country + */ + public TestContextItem00A(final TestContextItem008 dateValue, final String tzValue, final boolean dst, + final int utcOffset, final String language, final String country) { + this.dateValue = dateValue; + this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); + this.dst = dst; + this.utcOffset = utcOffset; + + final Locale locale = new Locale(language, country); + this.localeLanguage = locale.getLanguage(); + this.localeCountry = locale.getCountry(); + } + + /** + * The Constructor. + * + * @param original the original + */ + public TestContextItem00A(final TestContextItem00A original) { + this.dateValue = original.dateValue; + this.timeZoneString = TimeZone.getTimeZone(original.timeZoneString).getDisplayName(); + this.dst = original.dst; + this.utcOffset = original.utcOffset; + + final Locale locale = new Locale(original.localeLanguage, original.localeCountry); + this.localeLanguage = locale.getLanguage(); + this.localeCountry = locale.getCountry(); + } + + /** + * Gets the date value. + * + * @return the date value + */ + public TestContextItem008 getDateValue() { + return dateValue; + } + + /** + * Sets the date value. + * + * @param dateValue the date value + */ + public void setDateValue(final TestContextItem008 dateValue) { + this.dateValue = dateValue; + } + + /** + * Gets the TZ value. + * + * @return the TZ value + */ + public String getTZValue() { + return timeZoneString; + } + + /** + * Sets the TZ value. + * + * @param tzValue the TZ value + */ + public void setTZValue(final String tzValue) { + this.timeZoneString = TimeZone.getTimeZone(tzValue).getDisplayName(); + } + + /** + * Gets the DST. + * + * @return the dst + */ + public boolean getDST() { + return dst; + } + + /** + * Sets the DST. + * + * @param newDst the dst + */ + public void setDST(final boolean newDst) { + this.dst = newDst; + } + + /** + * Gets the UTC offset. + * + * @return the UTC offset + */ + public int getUTCOffset() { + return utcOffset; + } + + /** + * Sets the UTC offset. + * + * @param newUtcOffset the UTC offset + */ + public void setUTCOffset(final int newUtcOffset) { + this.utcOffset = newUtcOffset; + } + + /** + * Gets the locale. + * + * @return the locale + */ + public Locale getLocale() { + return new Locale(localeLanguage, localeCountry); + } + + /** + * Sets the locale. + * + * @param locale the locale + */ + public void setLocale(final Locale locale) { + this.localeLanguage = locale.getLanguage(); + this.localeCountry = locale.getCountry(); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((dateValue == null) ? 0 : dateValue.hashCode()); + result = prime * result + (dst ? HASH_PRIME_2 : HASH_PRIME_3); + result = prime * result + ((localeCountry == null) ? 0 : localeCountry.hashCode()); + result = prime * result + ((localeLanguage == null) ? 0 : localeLanguage.hashCode()); + result = prime * result + ((timeZoneString == null) ? 0 : timeZoneString.hashCode()); + result = prime * result + utcOffset; + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem00A other = (TestContextItem00A) obj; + if (dateValue == null) { + if (other.dateValue != null) { + return false; + } + } else if (!dateValue.equals(other.dateValue)) { + return false; + } + if (dst != other.dst) { + return false; + } + if (localeCountry == null) { + if (other.localeCountry != null) { + return false; + } + } else if (!localeCountry.equals(other.localeCountry)) { + return false; + } + if (localeLanguage == null) { + if (other.localeLanguage != null) { + return false; + } + } else if (!localeLanguage.equals(other.localeLanguage)) { + return false; + } + if (timeZoneString == null) { + if (other.timeZoneString != null) { + return false; + } + } else if (!timeZoneString.equals(other.timeZoneString)) { + return false; + } + if (utcOffset != other.utcOffset) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem00A [dateValue=" + dateValue + ", timeZoneString=" + timeZoneString + ", dst=" + dst + + ", utcOffset=" + utcOffset + ", localeLanguage=" + localeLanguage + ", localeCountry=" + localeCountry + + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00B.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00B.java new file mode 100644 index 000000000..4990b1b9e --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00B.java @@ -0,0 +1,128 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; +import java.util.Set; +import java.util.TreeSet; + +/** + * The Class TestContextItem00B. + */ +public class TestContextItem00B implements Serializable { + private static final long serialVersionUID = 1254589722957250388L; + + private static final int HASH_PRIME_1 = 31; + + private TreeSet setValue = new TreeSet(); + + /** + * The Constructor. + */ + public TestContextItem00B() {} + + /** + * The Constructor. + * + * @param setArray the set array + */ + public TestContextItem00B(final String[] setArray) {} + + /** + * The Constructor. + * + * @param setValue the set value + */ + public TestContextItem00B(final TreeSet setValue) { + this.setValue = setValue; + } + + /** + * Gets the set value. + * + * @return the sets the value + */ + public Set getSetValue() { + if (setValue == null) { + setValue = new TreeSet(); + } + return setValue; + } + + /** + * Sets the set value. + * + * @param setValue the sets the value + */ + public void setSetValue(final TreeSet setValue) { + this.setValue = setValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((setValue == null) ? 0 : setValue.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem00B other = (TestContextItem00B) obj; + if (setValue == null) { + if (other.setValue != null) { + return false; + } + } else if (!setValue.equals(other.setValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem00B [setValue=" + setValue + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00C.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00C.java new file mode 100644 index 000000000..6c912977a --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestContextItem00C.java @@ -0,0 +1,122 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; +import java.util.Map; +import java.util.TreeMap; + +/** + * The Class TestContextItem00C. + */ +public class TestContextItem00C implements Serializable { + private static final long serialVersionUID = -7497746259264651884L; + + private static final int HASH_PRIME_1 = 31; + + private Map mapValue = new TreeMap(); + + /** + * The Constructor. + */ + public TestContextItem00C() {} + + /** + * The Constructor. + * + * @param mapValue the map value + */ + public TestContextItem00C(final Map mapValue) { + this.mapValue = mapValue; + } + + /** + * Gets the map value. + * + * @return the map value + */ + public Map getMapValue() { + if (mapValue == null) { + mapValue = new TreeMap(); + } + return mapValue; + } + + /** + * Sets the map value. + * + * @param mapValue the map value + */ + public void setMapValue(final Map mapValue) { + this.mapValue = mapValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((mapValue == null) ? 0 : mapValue.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestContextItem00C other = (TestContextItem00C) obj; + if (mapValue == null) { + if (other.mapValue != null) { + return false; + } + } else if (!mapValue.equals(other.mapValue)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestContextItem00C [mapValue=" + mapValue + "]"; + } + +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestExternalContextItem.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestExternalContextItem.java new file mode 100644 index 000000000..11a1af1e3 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestExternalContextItem.java @@ -0,0 +1,434 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestExternalContextItem. + */ +public class TestExternalContextItem implements Serializable { + private static final long serialVersionUID = 3512435733818672173L; + + private static final int HASH_PRIME_1 = 31; + + private TestContextItem000 testExternalContextItem000; + private TestContextItem001 testExternalContextItem001; + private TestContextItem002 testExternalContextItem002; + private TestContextItem003 testExternalContextItem003; + private TestContextItem004 testExternalContextItem004; + private TestContextItem005 testExternalContextItem005; + private TestContextItem006 testExternalContextItem006; + private TestContextItem007 testExternalContextItem007; + private TestContextItem008 testExternalContextItem008; + private TestContextItem009 testExternalContextItem009; + private TestContextItem00A testExternalContextItem00A; + private TestContextItem00B testExternalContextItem00B; + private TestContextItem00C testExternalContextItem00C; + + /** + * Gets the test external context item 000. + * + * @return the test external context item 000 + */ + public TestContextItem000 getTestExternalContextItem000() { + return testExternalContextItem000; + } + + /** + * Sets the test external context item 000. + * + * @param testExternalContextItem000 the test external context item 000 + */ + public void setTestExternalContextItem000(final TestContextItem000 testExternalContextItem000) { + this.testExternalContextItem000 = testExternalContextItem000; + } + + /** + * Gets the test external context item 001. + * + * @return the test external context item 001 + */ + public TestContextItem001 getTestExternalContextItem001() { + return testExternalContextItem001; + } + + /** + * Sets the test external context item 001. + * + * @param testExternalContextItem001 the test external context item 001 + */ + public void setTestExternalContextItem001(final TestContextItem001 testExternalContextItem001) { + this.testExternalContextItem001 = testExternalContextItem001; + } + + /** + * Gets the test external context item 002. + * + * @return the test external context item 002 + */ + public TestContextItem002 getTestExternalContextItem002() { + return testExternalContextItem002; + } + + /** + * Sets the test external context item 002. + * + * @param testExternalContextItem002 the test external context item 002 + */ + public void setTestExternalContextItem002(final TestContextItem002 testExternalContextItem002) { + this.testExternalContextItem002 = testExternalContextItem002; + } + + /** + * Gets the test external context item 003. + * + * @return the test external context item 003 + */ + public TestContextItem003 getTestExternalContextItem003() { + return testExternalContextItem003; + } + + /** + * Sets the test external context item 003. + * + * @param testExternalContextItem003 the test external context item 003 + */ + public void setTestExternalContextItem003(final TestContextItem003 testExternalContextItem003) { + this.testExternalContextItem003 = testExternalContextItem003; + } + + /** + * Gets the test external context item 004. + * + * @return the test external context item 004 + */ + public TestContextItem004 getTestExternalContextItem004() { + return testExternalContextItem004; + } + + /** + * Sets the test external context item 004. + * + * @param testExternalContextItem004 the test external context item 004 + */ + public void setTestExternalContextItem004(final TestContextItem004 testExternalContextItem004) { + this.testExternalContextItem004 = testExternalContextItem004; + } + + /** + * Gets the test external context item 005. + * + * @return the test external context item 005 + */ + public TestContextItem005 getTestExternalContextItem005() { + return testExternalContextItem005; + } + + /** + * Sets the test external context item 005. + * + * @param testExternalContextItem005 the test external context item 005 + */ + public void setTestExternalContextItem005(final TestContextItem005 testExternalContextItem005) { + this.testExternalContextItem005 = testExternalContextItem005; + } + + /** + * Gets the test external context item 006. + * + * @return the test external context item 006 + */ + public TestContextItem006 getTestExternalContextItem006() { + return testExternalContextItem006; + } + + /** + * Sets the test external context item 006. + * + * @param testExternalContextItem006 the test external context item 006 + */ + public void setTestExternalContextItem006(final TestContextItem006 testExternalContextItem006) { + this.testExternalContextItem006 = testExternalContextItem006; + } + + /** + * Gets the test external context item 007. + * + * @return the test external context item 007 + */ + public TestContextItem007 getTestExternalContextItem007() { + return testExternalContextItem007; + } + + /** + * Sets the test external context item 007. + * + * @param testExternalContextItem007 the test external context item 007 + */ + public void setTestExternalContextItem007(final TestContextItem007 testExternalContextItem007) { + this.testExternalContextItem007 = testExternalContextItem007; + } + + /** + * Gets the test external context item 008. + * + * @return the test external context item 008 + */ + public TestContextItem008 getTestExternalContextItem008() { + return testExternalContextItem008; + } + + /** + * Sets the test external context item 008. + * + * @param testExternalContextItem008 the test external context item 008 + */ + public void setTestExternalContextItem008(final TestContextItem008 testExternalContextItem008) { + this.testExternalContextItem008 = testExternalContextItem008; + } + + /** + * Gets the test external context item 009. + * + * @return the test external context item 009 + */ + public TestContextItem009 getTestExternalContextItem009() { + return testExternalContextItem009; + } + + /** + * Sets the test external context item 009. + * + * @param testExternalContextItem009 the test external context item 009 + */ + public void setTestExternalContextItem009(final TestContextItem009 testExternalContextItem009) { + this.testExternalContextItem009 = testExternalContextItem009; + } + + /** + * Gets the test external context item 00 A. + * + * @return the test external context item 00 A + */ + public TestContextItem00A getTestExternalContextItem00A() { + return testExternalContextItem00A; + } + + /** + * Sets the test external context item 00 A. + * + * @param testExternalContextItem00A the test external context item 00 A + */ + public void setTestExternalContextItem00A(final TestContextItem00A testExternalContextItem00A) { + this.testExternalContextItem00A = testExternalContextItem00A; + } + + /** + * Gets the test external context item 00 B. + * + * @return the test external context item 00 B + */ + public TestContextItem00B getTestExternalContextItem00B() { + return testExternalContextItem00B; + } + + /** + * Sets the test external context item 00 B. + * + * @param testExternalContextItem00B the test external context item 00 B + */ + public void setTestExternalContextItem00B(final TestContextItem00B testExternalContextItem00B) { + this.testExternalContextItem00B = testExternalContextItem00B; + } + + /** + * Gets the test external context item 00 C. + * + * @return the test external context item 00 C + */ + public TestContextItem00C getTestExternalContextItem00C() { + return testExternalContextItem00C; + } + + /** + * Sets the test external context item 00 C. + * + * @param testExternalContextItem00C the test external context item 00 C + */ + public void setTestExternalContextItem00C(final TestContextItem00C testExternalContextItem00C) { + this.testExternalContextItem00C = testExternalContextItem00C; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((testExternalContextItem000 == null) ? 0 : testExternalContextItem000.hashCode()); + result = prime * result + ((testExternalContextItem001 == null) ? 0 : testExternalContextItem001.hashCode()); + result = prime * result + ((testExternalContextItem002 == null) ? 0 : testExternalContextItem002.hashCode()); + result = prime * result + ((testExternalContextItem003 == null) ? 0 : testExternalContextItem003.hashCode()); + result = prime * result + ((testExternalContextItem004 == null) ? 0 : testExternalContextItem004.hashCode()); + result = prime * result + ((testExternalContextItem005 == null) ? 0 : testExternalContextItem005.hashCode()); + result = prime * result + ((testExternalContextItem006 == null) ? 0 : testExternalContextItem006.hashCode()); + result = prime * result + ((testExternalContextItem007 == null) ? 0 : testExternalContextItem007.hashCode()); + result = prime * result + ((testExternalContextItem008 == null) ? 0 : testExternalContextItem008.hashCode()); + result = prime * result + ((testExternalContextItem009 == null) ? 0 : testExternalContextItem009.hashCode()); + result = prime * result + ((testExternalContextItem00A == null) ? 0 : testExternalContextItem00A.hashCode()); + result = prime * result + ((testExternalContextItem00B == null) ? 0 : testExternalContextItem00B.hashCode()); + result = prime * result + ((testExternalContextItem00C == null) ? 0 : testExternalContextItem00C.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestExternalContextItem other = (TestExternalContextItem) obj; + if (testExternalContextItem000 == null) { + if (other.testExternalContextItem000 != null) { + return false; + } + } else if (!testExternalContextItem000.equals(other.testExternalContextItem000)) { + return false; + } + if (testExternalContextItem001 == null) { + if (other.testExternalContextItem001 != null) { + return false; + } + } else if (!testExternalContextItem001.equals(other.testExternalContextItem001)) { + return false; + } + if (testExternalContextItem002 == null) { + if (other.testExternalContextItem002 != null) { + return false; + } + } else if (!testExternalContextItem002.equals(other.testExternalContextItem002)) { + return false; + } + if (testExternalContextItem003 == null) { + if (other.testExternalContextItem003 != null) { + return false; + } + } else if (!testExternalContextItem003.equals(other.testExternalContextItem003)) { + return false; + } + if (testExternalContextItem004 == null) { + if (other.testExternalContextItem004 != null) { + return false; + } + } else if (!testExternalContextItem004.equals(other.testExternalContextItem004)) { + return false; + } + if (testExternalContextItem005 == null) { + if (other.testExternalContextItem005 != null) { + return false; + } + } else if (!testExternalContextItem005.equals(other.testExternalContextItem005)) { + return false; + } + if (testExternalContextItem006 == null) { + if (other.testExternalContextItem006 != null) { + return false; + } + } else if (!testExternalContextItem006.equals(other.testExternalContextItem006)) { + return false; + } + if (testExternalContextItem007 == null) { + if (other.testExternalContextItem007 != null) { + return false; + } + } else if (!testExternalContextItem007.equals(other.testExternalContextItem007)) { + return false; + } + if (testExternalContextItem008 == null) { + if (other.testExternalContextItem008 != null) { + return false; + } + } else if (!testExternalContextItem008.equals(other.testExternalContextItem008)) { + return false; + } + if (testExternalContextItem009 == null) { + if (other.testExternalContextItem009 != null) { + return false; + } + } else if (!testExternalContextItem009.equals(other.testExternalContextItem009)) { + return false; + } + if (testExternalContextItem00A == null) { + if (other.testExternalContextItem00A != null) { + return false; + } + } else if (!testExternalContextItem00A.equals(other.testExternalContextItem00A)) { + return false; + } + if (testExternalContextItem00B == null) { + if (other.testExternalContextItem00B != null) { + return false; + } + } else if (!testExternalContextItem00B.equals(other.testExternalContextItem00B)) { + return false; + } + if (testExternalContextItem00C == null) { + if (other.testExternalContextItem00C != null) { + return false; + } + } else if (!testExternalContextItem00C.equals(other.testExternalContextItem00C)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestExternalContextItem [testExternalContextItem000=" + testExternalContextItem000 + + ", testExternalContextItem001=" + testExternalContextItem001 + ", testExternalContextItem002=" + + testExternalContextItem002 + ", testExternalContextItem003=" + testExternalContextItem003 + + ", testExternalContextItem004=" + testExternalContextItem004 + ", testExternalContextItem005=" + + testExternalContextItem005 + ", testExternalContextItem006=" + testExternalContextItem006 + + ", testExternalContextItem007=" + testExternalContextItem007 + ", testExternalContextItem008=" + + testExternalContextItem008 + ", testExternalContextItem009=" + testExternalContextItem009 + + ", testExternalContextItem00A=" + testExternalContextItem00A + ", testExternalContextItem00B=" + + testExternalContextItem00B + ", testExternalContextItem00C=" + testExternalContextItem00C + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestGlobalContextItem.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestGlobalContextItem.java new file mode 100644 index 000000000..2e893d980 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestGlobalContextItem.java @@ -0,0 +1,434 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestGlobalContextItem. + */ +public class TestGlobalContextItem implements Serializable { + private static final long serialVersionUID = 3348445332683174361L; + + private static final int HASH_PRIME_1 = 31; + + private TestContextItem000 testGlobalContextItem000; + private TestContextItem001 testGlobalContextItem001; + private TestContextItem002 testGlobalContextItem002; + private TestContextItem003 testGlobalContextItem003; + private TestContextItem004 testGlobalContextItem004; + private TestContextItem005 testGlobalContextItem005; + private TestContextItem006 testGlobalContextItem006; + private TestContextItem007 testGlobalContextItem007; + private TestContextItem008 testGlobalContextItem008; + private TestContextItem009 testGlobalContextItem009; + private TestContextItem00A testGlobalContextItem00A; + private TestContextItem00B testGlobalContextItem00B; + private TestContextItem00C testGlobalContextItem00C; + + /** + * Gets the test global context item 000. + * + * @return the test global context item 000 + */ + public TestContextItem000 getTestGlobalContextItem000() { + return testGlobalContextItem000; + } + + /** + * Sets the test global context item 000. + * + * @param testGlobalContextItem000 the test global context item 000 + */ + public void setTestGlobalContextItem000(final TestContextItem000 testGlobalContextItem000) { + this.testGlobalContextItem000 = testGlobalContextItem000; + } + + /** + * Gets the test global context item 001. + * + * @return the test global context item 001 + */ + public TestContextItem001 getTestGlobalContextItem001() { + return testGlobalContextItem001; + } + + /** + * Sets the test global context item 001. + * + * @param testGlobalContextItem001 the test global context item 001 + */ + public void setTestGlobalContextItem001(final TestContextItem001 testGlobalContextItem001) { + this.testGlobalContextItem001 = testGlobalContextItem001; + } + + /** + * Gets the test global context item 002. + * + * @return the test global context item 002 + */ + public TestContextItem002 getTestGlobalContextItem002() { + return testGlobalContextItem002; + } + + /** + * Sets the test global context item 002. + * + * @param testGlobalContextItem002 the test global context item 002 + */ + public void setTestGlobalContextItem002(final TestContextItem002 testGlobalContextItem002) { + this.testGlobalContextItem002 = testGlobalContextItem002; + } + + /** + * Gets the test global context item 003. + * + * @return the test global context item 003 + */ + public TestContextItem003 getTestGlobalContextItem003() { + return testGlobalContextItem003; + } + + /** + * Sets the test global context item 003. + * + * @param testGlobalContextItem003 the test global context item 003 + */ + public void setTestGlobalContextItem003(final TestContextItem003 testGlobalContextItem003) { + this.testGlobalContextItem003 = testGlobalContextItem003; + } + + /** + * Gets the test global context item 004. + * + * @return the test global context item 004 + */ + public TestContextItem004 getTestGlobalContextItem004() { + return testGlobalContextItem004; + } + + /** + * Sets the test global context item 004. + * + * @param testGlobalContextItem004 the test global context item 004 + */ + public void setTestGlobalContextItem004(final TestContextItem004 testGlobalContextItem004) { + this.testGlobalContextItem004 = testGlobalContextItem004; + } + + /** + * Gets the test global context item 005. + * + * @return the test global context item 005 + */ + public TestContextItem005 getTestGlobalContextItem005() { + return testGlobalContextItem005; + } + + /** + * Sets the test global context item 005. + * + * @param testGlobalContextItem005 the test global context item 005 + */ + public void setTestGlobalContextItem005(final TestContextItem005 testGlobalContextItem005) { + this.testGlobalContextItem005 = testGlobalContextItem005; + } + + /** + * Gets the test global context item 006. + * + * @return the test global context item 006 + */ + public TestContextItem006 getTestGlobalContextItem006() { + return testGlobalContextItem006; + } + + /** + * Sets the test global context item 006. + * + * @param testGlobalContextItem006 the test global context item 006 + */ + public void setTestGlobalContextItem006(final TestContextItem006 testGlobalContextItem006) { + this.testGlobalContextItem006 = testGlobalContextItem006; + } + + /** + * Gets the test global context item 007. + * + * @return the test global context item 007 + */ + public TestContextItem007 getTestGlobalContextItem007() { + return testGlobalContextItem007; + } + + /** + * Sets the test global context item 007. + * + * @param testGlobalContextItem007 the test global context item 007 + */ + public void setTestGlobalContextItem007(final TestContextItem007 testGlobalContextItem007) { + this.testGlobalContextItem007 = testGlobalContextItem007; + } + + /** + * Gets the test global context item 008. + * + * @return the test global context item 008 + */ + public TestContextItem008 getTestGlobalContextItem008() { + return testGlobalContextItem008; + } + + /** + * Sets the test global context item 008. + * + * @param testGlobalContextItem008 the test global context item 008 + */ + public void setTestGlobalContextItem008(final TestContextItem008 testGlobalContextItem008) { + this.testGlobalContextItem008 = testGlobalContextItem008; + } + + /** + * Gets the test global context item 009. + * + * @return the test global context item 009 + */ + public TestContextItem009 getTestGlobalContextItem009() { + return testGlobalContextItem009; + } + + /** + * Sets the test global context item 009. + * + * @param testGlobalContextItem009 the test global context item 009 + */ + public void setTestGlobalContextItem009(final TestContextItem009 testGlobalContextItem009) { + this.testGlobalContextItem009 = testGlobalContextItem009; + } + + /** + * Gets the test global context item 00 A. + * + * @return the test global context item 00 A + */ + public TestContextItem00A getTestGlobalContextItem00A() { + return testGlobalContextItem00A; + } + + /** + * Sets the test global context item 00 A. + * + * @param testGlobalContextItem00A the test global context item 00 A + */ + public void setTestGlobalContextItem00A(final TestContextItem00A testGlobalContextItem00A) { + this.testGlobalContextItem00A = testGlobalContextItem00A; + } + + /** + * Gets the test global context item 00 B. + * + * @return the test global context item 00 B + */ + public TestContextItem00B getTestGlobalContextItem00B() { + return testGlobalContextItem00B; + } + + /** + * Sets the test global context item 00 B. + * + * @param testGlobalContextItem00B the test global context item 00 B + */ + public void setTestGlobalContextItem00B(final TestContextItem00B testGlobalContextItem00B) { + this.testGlobalContextItem00B = testGlobalContextItem00B; + } + + /** + * Gets the test global context item 00 C. + * + * @return the test global context item 00 C + */ + public TestContextItem00C getTestGlobalContextItem00C() { + return testGlobalContextItem00C; + } + + /** + * Sets the test global context item 00 C. + * + * @param testGlobalContextItem00C the test global context item 00 C + */ + public void setTestGlobalContextItem00C(final TestContextItem00C testGlobalContextItem00C) { + this.testGlobalContextItem00C = testGlobalContextItem00C; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((testGlobalContextItem000 == null) ? 0 : testGlobalContextItem000.hashCode()); + result = prime * result + ((testGlobalContextItem001 == null) ? 0 : testGlobalContextItem001.hashCode()); + result = prime * result + ((testGlobalContextItem002 == null) ? 0 : testGlobalContextItem002.hashCode()); + result = prime * result + ((testGlobalContextItem003 == null) ? 0 : testGlobalContextItem003.hashCode()); + result = prime * result + ((testGlobalContextItem004 == null) ? 0 : testGlobalContextItem004.hashCode()); + result = prime * result + ((testGlobalContextItem005 == null) ? 0 : testGlobalContextItem005.hashCode()); + result = prime * result + ((testGlobalContextItem006 == null) ? 0 : testGlobalContextItem006.hashCode()); + result = prime * result + ((testGlobalContextItem007 == null) ? 0 : testGlobalContextItem007.hashCode()); + result = prime * result + ((testGlobalContextItem008 == null) ? 0 : testGlobalContextItem008.hashCode()); + result = prime * result + ((testGlobalContextItem009 == null) ? 0 : testGlobalContextItem009.hashCode()); + result = prime * result + ((testGlobalContextItem00A == null) ? 0 : testGlobalContextItem00A.hashCode()); + result = prime * result + ((testGlobalContextItem00B == null) ? 0 : testGlobalContextItem00B.hashCode()); + result = prime * result + ((testGlobalContextItem00C == null) ? 0 : testGlobalContextItem00C.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestGlobalContextItem other = (TestGlobalContextItem) obj; + if (testGlobalContextItem000 == null) { + if (other.testGlobalContextItem000 != null) { + return false; + } + } else if (!testGlobalContextItem000.equals(other.testGlobalContextItem000)) { + return false; + } + if (testGlobalContextItem001 == null) { + if (other.testGlobalContextItem001 != null) { + return false; + } + } else if (!testGlobalContextItem001.equals(other.testGlobalContextItem001)) { + return false; + } + if (testGlobalContextItem002 == null) { + if (other.testGlobalContextItem002 != null) { + return false; + } + } else if (!testGlobalContextItem002.equals(other.testGlobalContextItem002)) { + return false; + } + if (testGlobalContextItem003 == null) { + if (other.testGlobalContextItem003 != null) { + return false; + } + } else if (!testGlobalContextItem003.equals(other.testGlobalContextItem003)) { + return false; + } + if (testGlobalContextItem004 == null) { + if (other.testGlobalContextItem004 != null) { + return false; + } + } else if (!testGlobalContextItem004.equals(other.testGlobalContextItem004)) { + return false; + } + if (testGlobalContextItem005 == null) { + if (other.testGlobalContextItem005 != null) { + return false; + } + } else if (!testGlobalContextItem005.equals(other.testGlobalContextItem005)) { + return false; + } + if (testGlobalContextItem006 == null) { + if (other.testGlobalContextItem006 != null) { + return false; + } + } else if (!testGlobalContextItem006.equals(other.testGlobalContextItem006)) { + return false; + } + if (testGlobalContextItem007 == null) { + if (other.testGlobalContextItem007 != null) { + return false; + } + } else if (!testGlobalContextItem007.equals(other.testGlobalContextItem007)) { + return false; + } + if (testGlobalContextItem008 == null) { + if (other.testGlobalContextItem008 != null) { + return false; + } + } else if (!testGlobalContextItem008.equals(other.testGlobalContextItem008)) { + return false; + } + if (testGlobalContextItem009 == null) { + if (other.testGlobalContextItem009 != null) { + return false; + } + } else if (!testGlobalContextItem009.equals(other.testGlobalContextItem009)) { + return false; + } + if (testGlobalContextItem00A == null) { + if (other.testGlobalContextItem00A != null) { + return false; + } + } else if (!testGlobalContextItem00A.equals(other.testGlobalContextItem00A)) { + return false; + } + if (testGlobalContextItem00B == null) { + if (other.testGlobalContextItem00B != null) { + return false; + } + } else if (!testGlobalContextItem00B.equals(other.testGlobalContextItem00B)) { + return false; + } + if (testGlobalContextItem00C == null) { + if (other.testGlobalContextItem00C != null) { + return false; + } + } else if (!testGlobalContextItem00C.equals(other.testGlobalContextItem00C)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestGlobalContextItem [testGlobalContextItem000=" + testGlobalContextItem000 + + ", testGlobalContextItem001=" + testGlobalContextItem001 + ", testGlobalContextItem002=" + + testGlobalContextItem002 + ", testGlobalContextItem003=" + testGlobalContextItem003 + + ", testGlobalContextItem004=" + testGlobalContextItem004 + ", testGlobalContextItem005=" + + testGlobalContextItem005 + ", testGlobalContextItem006=" + testGlobalContextItem006 + + ", testGlobalContextItem007=" + testGlobalContextItem007 + ", testGlobalContextItem008=" + + testGlobalContextItem008 + ", testGlobalContextItem009=" + testGlobalContextItem009 + + ", testGlobalContextItem00A=" + testGlobalContextItem00A + ", testGlobalContextItem00B=" + + testGlobalContextItem00B + ", testGlobalContextItem00C=" + testGlobalContextItem00C + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestPolicyContextItem.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestPolicyContextItem.java new file mode 100644 index 000000000..e9381ded4 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/TestPolicyContextItem.java @@ -0,0 +1,241 @@ +/*- + * ============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.context.test.concepts; + +import java.io.Serializable; + +/** + * The Class TestPolicyContextItem. + */ +public class TestPolicyContextItem implements Serializable { + private static final long serialVersionUID = 6336372857646152910L; + + private static final int HASH_PRIME_1 = 31; + + private TestContextItem006 testPolicyContextItem000; + private TestContextItem003 testPolicyContextItem001; + private TestContextItem005 testPolicyContextItem002; + private TestContextItem000 testPolicyContextItem003; + private TestContextItem003 testPolicyContextItem004; + private TestContextItem00C testPolicyContextItem005; + + /** + * Gets the test policy context item 000. + * + * @return the test policy context item 000 + */ + public TestContextItem006 getTestPolicyContextItem000() { + return testPolicyContextItem000; + } + + /** + * Sets the test policy context item 000. + * + * @param testPolicyContextItem000 the test policy context item 000 + */ + public void setTestPolicyContextItem000(final TestContextItem006 testPolicyContextItem000) { + this.testPolicyContextItem000 = testPolicyContextItem000; + } + + /** + * Gets the test policy context item 001. + * + * @return the test policy context item 001 + */ + public TestContextItem003 getTestPolicyContextItem001() { + return testPolicyContextItem001; + } + + /** + * Sets the test policy context item 001. + * + * @param testPolicyContextItem001 the test policy context item 001 + */ + public void setTestPolicyContextItem001(final TestContextItem003 testPolicyContextItem001) { + this.testPolicyContextItem001 = testPolicyContextItem001; + } + + /** + * Gets the test policy context item 002. + * + * @return the test policy context item 002 + */ + public TestContextItem005 getTestPolicyContextItem002() { + return testPolicyContextItem002; + } + + /** + * Sets the test policy context item 002. + * + * @param testPolicyContextItem002 the test policy context item 002 + */ + public void setTestPolicyContextItem002(final TestContextItem005 testPolicyContextItem002) { + this.testPolicyContextItem002 = testPolicyContextItem002; + } + + /** + * Gets the test policy context item 003. + * + * @return the test policy context item 003 + */ + public TestContextItem000 getTestPolicyContextItem003() { + return testPolicyContextItem003; + } + + /** + * Sets the test policy context item 003. + * + * @param testPolicyContextItem003 the test policy context item 003 + */ + public void setTestPolicyContextItem003(final TestContextItem000 testPolicyContextItem003) { + this.testPolicyContextItem003 = testPolicyContextItem003; + } + + /** + * Gets the test policy context item 004. + * + * @return the test policy context item 004 + */ + public TestContextItem003 getTestPolicyContextItem004() { + return testPolicyContextItem004; + } + + /** + * Sets the test policy context item 004. + * + * @param testPolicyContextItem004 the test policy context item 004 + */ + public void setTestPolicyContextItem004(final TestContextItem003 testPolicyContextItem004) { + this.testPolicyContextItem004 = testPolicyContextItem004; + } + + /** + * Gets the test policy context item 005. + * + * @return the test policy context item 005 + */ + public TestContextItem00C getTestPolicyContextItem005() { + return testPolicyContextItem005; + } + + /** + * Sets the test policy context item 005. + * + * @param testPolicyContextItem005 the test policy context item 005 + */ + public void setTestPolicyContextItem005(final TestContextItem00C testPolicyContextItem005) { + this.testPolicyContextItem005 = testPolicyContextItem005; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = HASH_PRIME_1; + int result = 1; + result = prime * result + ((testPolicyContextItem000 == null) ? 0 : testPolicyContextItem000.hashCode()); + result = prime * result + ((testPolicyContextItem001 == null) ? 0 : testPolicyContextItem001.hashCode()); + result = prime * result + ((testPolicyContextItem002 == null) ? 0 : testPolicyContextItem002.hashCode()); + result = prime * result + ((testPolicyContextItem003 == null) ? 0 : testPolicyContextItem003.hashCode()); + result = prime * result + ((testPolicyContextItem004 == null) ? 0 : testPolicyContextItem004.hashCode()); + result = prime * result + ((testPolicyContextItem005 == null) ? 0 : testPolicyContextItem005.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final TestPolicyContextItem other = (TestPolicyContextItem) obj; + if (testPolicyContextItem000 == null) { + if (other.testPolicyContextItem000 != null) { + return false; + } + } else if (!testPolicyContextItem000.equals(other.testPolicyContextItem000)) { + return false; + } + if (testPolicyContextItem001 == null) { + if (other.testPolicyContextItem001 != null) { + return false; + } + } else if (!testPolicyContextItem001.equals(other.testPolicyContextItem001)) { + return false; + } + if (testPolicyContextItem002 == null) { + if (other.testPolicyContextItem002 != null) { + return false; + } + } else if (!testPolicyContextItem002.equals(other.testPolicyContextItem002)) { + return false; + } + if (testPolicyContextItem003 == null) { + if (other.testPolicyContextItem003 != null) { + return false; + } + } else if (!testPolicyContextItem003.equals(other.testPolicyContextItem003)) { + return false; + } + if (testPolicyContextItem004 == null) { + if (other.testPolicyContextItem004 != null) { + return false; + } + } else if (!testPolicyContextItem004.equals(other.testPolicyContextItem004)) { + return false; + } + if (testPolicyContextItem005 == null) { + if (other.testPolicyContextItem005 != null) { + return false; + } + } else if (!testPolicyContextItem005.equals(other.testPolicyContextItem005)) { + return false; + } + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return "TestPolicyContextItem [testPolicyContextItem000=" + testPolicyContextItem000 + + ", testPolicyContextItem001=" + testPolicyContextItem001 + ", testPolicyContextItem002=" + + testPolicyContextItem002 + ", testPolicyContextItem003=" + testPolicyContextItem003 + + ", testPolicyContextItem004=" + testPolicyContextItem004 + ", testPolicyContextItem005=" + + testPolicyContextItem005 + "]"; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/package-info.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/package-info.java new file mode 100644 index 000000000..d57b3fda3 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/concepts/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 test Java context classes. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.test.concepts; diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextAlbumUpdate.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextAlbumUpdate.java new file mode 100644 index 000000000..ae0fea7ea --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextAlbumUpdate.java @@ -0,0 +1,121 @@ +/*- + * ============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.context.test.distribution; + +import java.io.IOException; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +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.handling.ApexModelException; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.onap.policy.apex.model.utilities.comparison.KeyedMapComparer; +import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class ContextAlbumUpdate is used to test Context Album updates. + */ +public class ContextAlbumUpdate { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumUpdate.class); + + /** + * Test context album update. + * + * @throws ApexModelException the apex model exception + * @throws IOException the IO exception + * @throws ApexException the apex exception + */ + public void testContextAlbumUpdate() throws ApexModelException, IOException, ApexException { + LOGGER.debug("Running TestContextAlbumUpdate test . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); + final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey); + + final AxContextModel longModel = TestContextAlbumFactory.createLongContextModel(); + contextDistributor.registerModel(longModel); + + final AxContextAlbum longAlbum1Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum1", "0.0.1")); + final ContextAlbum longAlbum1 = contextDistributor.createContextAlbum(longAlbum1Def.getKey()); + assert (longAlbum1 != null); + + final AxContextAlbum longAlbum2Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum2", "0.0.1")); + final ContextAlbum longAlbum2 = contextDistributor.createContextAlbum(longAlbum2Def.getKey()); + assert (longAlbum2 != null); + + // CHECKSTYLE:OFF: checkstyle:magicNumber + longAlbum1.put("0", (long) 0); + longAlbum1.put("1", (long) 1); + longAlbum1.put("2", (long) 2); + longAlbum1.put("3", (long) 3); + + final KeyedMapDifference result0 = + new KeyedMapComparer().compareMaps(longAlbum1, longAlbum2); + + assert (0 == result0.getDifferentValues().size()); + assert (0 == result0.getIdenticalValues().size()); + assert (0 == result0.getRightOnly().size()); + assert (4 == result0.getLeftOnly().size()); + + longAlbum2.putAll(longAlbum1); + + final KeyedMapDifference result1 = + new KeyedMapComparer().compareMaps(longAlbum1, longAlbum2); + + assert (0 == result1.getDifferentValues().size()); + assert (4 == result1.getIdenticalValues().size()); + assert (0 == result1.getRightOnly().size()); + assert (0 == result1.getLeftOnly().size()); + + longAlbum1.put("4", (long) 4); + longAlbum2.put("5", (long) 5); + longAlbum1.put("67", (long) 6); + longAlbum2.put("67", (long) 7); + + final KeyedMapDifference result2 = + new KeyedMapComparer().compareMaps(longAlbum1, longAlbum2); + + assert (1 == result2.getDifferentValues().size()); + assert (4 == result2.getIdenticalValues().size()); + assert (1 == result2.getRightOnly().size()); + assert (1 == result2.getLeftOnly().size()); + + longAlbum1.remove("0"); + longAlbum2.remove("3"); + // CHECKSTYLE:ON: checkstyle:magicNumber + + final KeyedMapDifference result3 = + new KeyedMapComparer().compareMaps(longAlbum1, longAlbum2); + + assert (1 == result3.getDifferentValues().size()); + assert (2 == result3.getIdenticalValues().size()); + assert (2 == result3.getRightOnly().size()); + assert (2 == result3.getLeftOnly().size()); + + contextDistributor.clear(); + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java new file mode 100644 index 000000000..4a9af5a4c --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextInstantiation.java @@ -0,0 +1,487 @@ +/*- + * ============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.context.test.distribution; + +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TimeZone; +import java.util.TreeSet; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.concepts.TestContextItem000; +import org.onap.policy.apex.context.test.concepts.TestContextItem001; +import org.onap.policy.apex.context.test.concepts.TestContextItem002; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.concepts.TestContextItem004; +import org.onap.policy.apex.context.test.concepts.TestContextItem005; +import org.onap.policy.apex.context.test.concepts.TestContextItem006; +import org.onap.policy.apex.context.test.concepts.TestContextItem007; +import org.onap.policy.apex.context.test.concepts.TestContextItem008; +import org.onap.policy.apex.context.test.concepts.TestContextItem009; +import org.onap.policy.apex.context.test.concepts.TestContextItem00A; +import org.onap.policy.apex.context.test.concepts.TestContextItem00B; +import org.onap.policy.apex.context.test.concepts.TestContextItem00C; +import org.onap.policy.apex.context.test.concepts.TestExternalContextItem; +import org.onap.policy.apex.context.test.concepts.TestGlobalContextItem; +import org.onap.policy.apex.context.test.concepts.TestPolicyContextItem; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestContextInstantiation is used to test Apex context insitiation is correct. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class ContextInstantiation { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextInstantiation.class); + + /** + * Test context instantiation. + * + * @throws ContextException the context exception + */ + // CHECKSTYLE:OFF: checkstyle:MaximumMethodLength + public void testContextInstantiation() throws ContextException { + // CHECKSTYLE:ON: checkstyle:MaximumMethodLength + LOGGER.debug("Running TestContextInstantiation test . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributorInit", "0.0.1"); + Distributor contextDistributor = null; + try { + contextDistributor = new DistributorFactory().getDistributor(distributorKey); + } catch (final Exception e) { + e.printStackTrace(); + return; + } + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + try { + // CHECKSTYLE:OFF: checkstyle:magicNumber + final AxContextModel policyContextModel = TestContextAlbumFactory.createPolicyContextModel(); + final AxValidationResult result = new AxValidationResult(); + policyContextModel.validate(result); + LOGGER.debug(result.toString()); + assert (result.isValid()); + + contextDistributor.registerModel(policyContextModel); + + final ContextAlbum policyContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("PolicyContextAlbum", "0.0.1")); + assert (policyContextAlbum != null); + policyContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + final Float testFloat = new Float(3.14159265359); + final Date testDate = new Date(); + + final TestContextItem009 tci9 = new TestContextItem009(); + tci9.setDateValue(new TestContextItem008(testDate)); + tci9.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tci9.setDST(true); + + final TestContextItem00A tciA = new TestContextItem00A(); + tciA.setDateValue(new TestContextItem008(testDate)); + tciA.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tciA.setDST(true); + tciA.setUTCOffset(-600); + tciA.setLocale(Locale.ENGLISH); + + final TreeSet testTreeSet = new TreeSet<>(); + testTreeSet.add("one hundred"); + testTreeSet.add("one hundred and one"); + testTreeSet.add("one hundred and two"); + testTreeSet.add("one hundred and three"); + testTreeSet.add("one hundred and four"); + + final Map testHashMap = new HashMap<>(); + testHashMap.put("0", "zero"); + testHashMap.put("1", "one"); + testHashMap.put("2", "two"); + testHashMap.put("3", "three"); + testHashMap.put("4", "four"); + + final TestContextItem006 testPolicyContextItem000 = + new TestContextItem006("This is a policy context string"); + final TestContextItem003 testPolicyContextItem001 = new TestContextItem003(0xFFFFFFFFFFFFFFFFL); + final TestContextItem005 testPolicyContextItem002 = new TestContextItem005(Math.PI); + final TestContextItem000 testPolicyContextItem003 = new TestContextItem000(true); + final TestContextItem003 testPolicyContextItem004 = new TestContextItem003(testDate.getTime()); + final TestContextItem00C testPolicyContextItem005 = new TestContextItem00C(testHashMap); + + final Map valueMapA = new LinkedHashMap<>(); + valueMapA.put("TestPolicyContextItem001", testPolicyContextItem001); + valueMapA.put("TestPolicyContextItem002", testPolicyContextItem002); + valueMapA.put("TestPolicyContextItem003", testPolicyContextItem003); + valueMapA.put("TestPolicyContextItem004", testPolicyContextItem004); + valueMapA.put("TestPolicyContextItem005", testPolicyContextItem005); + valueMapA.put("TestPolicyContextItem000", testPolicyContextItem000); + + try { + policyContextAlbum.put("TestPolicyContextItem000", testPolicyContextItem000); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "Failed to set context value for key \"TestPolicyContextItem000\" in album \"PolicyContextAlbum:0.0.1\": " + + "PolicyContextAlbum:0.0.1: object \"TestContextItem006 [stringValue=This is a policy context string]\" " + + "of class \"org.onap.policy.apex.context.test.concepts.TestContextItem006\"" + + " not compatible with class \"org.onap.policy.apex.context.test.concepts.TestPolicyContextItem\"")); + } + + try { + policyContextAlbum.putAll(valueMapA); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "Failed to set context value for key \"TestPolicyContextItem001\" in album \"PolicyContextAlbum:0.0.1\": " + + "PolicyContextAlbum:0.0.1: object \"TestContextItem003 [longValue=-1]\" " + + "of class \"org.onap.policy.apex.context.test.concepts.TestContextItem003\"" + + " not compatible with class \"org.onap.policy.apex.context.test.concepts.TestPolicyContextItem\"")); + } + + final TestPolicyContextItem policyContext = new TestPolicyContextItem(); + + LOGGER.debug(policyContextAlbum.toString()); + + policyContext.setTestPolicyContextItem000(testPolicyContextItem000); + policyContext.setTestPolicyContextItem001(testPolicyContextItem001); + policyContext.setTestPolicyContextItem002(testPolicyContextItem002); + policyContext.setTestPolicyContextItem003(testPolicyContextItem003); + policyContext.setTestPolicyContextItem004(testPolicyContextItem004); + policyContext.setTestPolicyContextItem005(testPolicyContextItem005); + + final Map valueMap0 = new HashMap<>(); + valueMap0.put("TestPolicyContextItem", policyContext); + + policyContextAlbum.putAll(valueMap0); + + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem000().getStringValue().equals("This is a policy context string")); + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem001().getLongValue() == 0xFFFFFFFFFFFFFFFFL); + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem002().getDoubleValue() == Math.PI); + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem003().getFlag()); + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem004().getLongValue() == testDate.getTime()); + assert (((TestPolicyContextItem) policyContextAlbum.get("TestPolicyContextItem")) + .getTestPolicyContextItem005().getMapValue().equals(testHashMap)); + + final AxContextModel globalContextModel = TestContextAlbumFactory.createGlobalContextModel(); + + final TestContextItem000 testGlobalContextItem000 = new TestContextItem000(false); + final TestContextItem001 testGlobalContextItem001 = new TestContextItem001((byte) 0xFF); + final TestContextItem002 testGlobalContextItem002 = new TestContextItem002(0xFFFFFFFF); + final TestContextItem003 testGlobalContextItem003 = new TestContextItem003(0xFFFFFFFFFFFFFFFFL); + final TestContextItem004 testGlobalContextItem004 = new TestContextItem004(testFloat); + final TestContextItem005 testGlobalContextItem005 = new TestContextItem005(Math.PI); + final TestContextItem006 testGlobalContextItem006 = + new TestContextItem006("This is a global context string"); + final TestContextItem007 testGlobalContextItem007 = new TestContextItem007(testDate.getTime()); + final TestContextItem008 testGlobalContextItem008 = new TestContextItem008(testDate); + final TestContextItem00B testGlobalContextItem00B = new TestContextItem00B(testTreeSet); + final TestContextItem00C testGlobalContextItem00C = new TestContextItem00C(testHashMap); + + final TestGlobalContextItem globalContext = new TestGlobalContextItem(); + + globalContext.setTestGlobalContextItem000(testGlobalContextItem000); + globalContext.setTestGlobalContextItem001(testGlobalContextItem001); + globalContext.setTestGlobalContextItem002(testGlobalContextItem002); + globalContext.setTestGlobalContextItem003(testGlobalContextItem003); + globalContext.setTestGlobalContextItem004(testGlobalContextItem004); + globalContext.setTestGlobalContextItem005(testGlobalContextItem005); + globalContext.setTestGlobalContextItem006(testGlobalContextItem006); + globalContext.setTestGlobalContextItem007(testGlobalContextItem007); + globalContext.setTestGlobalContextItem008(testGlobalContextItem008); + globalContext.setTestGlobalContextItem009(tci9); + globalContext.setTestGlobalContextItem00A(tciA); + globalContext.setTestGlobalContextItem00B(testGlobalContextItem00B); + globalContext.setTestGlobalContextItem00C(testGlobalContextItem00C); + final Map valueMap1 = new HashMap<>(); + valueMap1.put("globalContext", globalContext); + + try { + contextDistributor.registerModel(globalContextModel); + } catch (final ContextException e) { + assert (e.getMessage().equals( + "active context albums found in distributor, clear the distributor before registering models")); + } + + contextDistributor.registerModel(globalContextModel); + + final ContextAlbum globalContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("GlobalContextAlbum", "0.0.1")); + assert (globalContextAlbum != null); + globalContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + globalContextAlbum.putAll(valueMap1); + + assert (!((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem000() + .getFlag()); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem001() + .getByteValue() == (byte) 0xFF); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem002() + .getIntValue() == 0xFFFFFFFF); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem003() + .getLongValue() == 0xFFFFFFFFFFFFFFFFL); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem004() + .getFloatValue() == testFloat); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem005() + .getDoubleValue() == Math.PI); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem006() + .getStringValue().equals("This is a global context string")); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem007() + .getLongValue() == testDate.getTime()); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem008() + .getDateValue().equals(testDate)); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem009() + .getDateValue().getTime() == tci9.getDateValue().getTime()); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem00A() + .getDateValue().getTime() == tciA.getDateValue().getTime()); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem00B() + .getSetValue().equals(testTreeSet)); + assert (((TestGlobalContextItem) globalContextAlbum.get("globalContext")).getTestGlobalContextItem00C() + .getMapValue().equals(testHashMap)); + + final AxContextModel externalContextModel = TestContextAlbumFactory.createExternalContextModel(); + + final TestExternalContextItem externalContext = new TestExternalContextItem(); + + final TestContextItem000 testExternalContextItem000 = new TestContextItem000(false); + final TestContextItem001 testExternalContextItem001 = new TestContextItem001((byte) 0xFF); + final TestContextItem002 testExternalContextItem002 = new TestContextItem002(0xFFFFFFFF); + final TestContextItem003 testExternalContextItem003 = new TestContextItem003(0xFFFFFFFFFFFFFFFFL); + final TestContextItem004 testExternalContextItem004 = new TestContextItem004(testFloat); + final TestContextItem005 testExternalContextItem005 = new TestContextItem005(Math.PI); + final TestContextItem006 testExternalContextItem006 = + new TestContextItem006("This is an external context string"); + final TestContextItem007 testExternalContextItem007 = new TestContextItem007(testDate.getTime()); + final TestContextItem008 testExternalContextItem008 = new TestContextItem008(testDate); + final TestContextItem00B testExternalContextItem00B = new TestContextItem00B(testTreeSet); + final TestContextItem00C testExternalContextItem00C = new TestContextItem00C(testHashMap); + + final TestContextItem009 tci9A = new TestContextItem009(tci9); + final TestContextItem00A tciAA = new TestContextItem00A(tciA); + + externalContext.setTestExternalContextItem000(testExternalContextItem000); + externalContext.setTestExternalContextItem001(testExternalContextItem001); + externalContext.setTestExternalContextItem002(testExternalContextItem002); + externalContext.setTestExternalContextItem003(testExternalContextItem003); + externalContext.setTestExternalContextItem004(testExternalContextItem004); + externalContext.setTestExternalContextItem005(testExternalContextItem005); + externalContext.setTestExternalContextItem006(testExternalContextItem006); + externalContext.setTestExternalContextItem007(testExternalContextItem007); + externalContext.setTestExternalContextItem008(testExternalContextItem008); + externalContext.setTestExternalContextItem009(tci9A); + externalContext.setTestExternalContextItem00A(tciAA); + externalContext.setTestExternalContextItem00B(testExternalContextItem00B); + externalContext.setTestExternalContextItem00C(testExternalContextItem00C); + + final Map valueMap2 = new HashMap<>(); + valueMap2.put("externalContext", externalContext); + + contextDistributor.clear(); + contextDistributor.init(new AxArtifactKey("ClearedandInittedDistributor", "0.0.1")); + contextDistributor.registerModel(externalContextModel); + + final ContextAlbum externalContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("ExternalContextAlbum", "0.0.1")); + assert (externalContextAlbum != null); + externalContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + externalContextAlbum.putAll(valueMap2); + externalContextAlbum.getAlbumDefinition().setWritable(false); + + assert (!((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem000().getFlag()); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem001().getByteValue() == (byte) 0xFF); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem002().getIntValue() == 0xFFFFFFFF); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem003().getLongValue() == 0xFFFFFFFFFFFFFFFFL); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem004().getFloatValue() == testFloat); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem005().getDoubleValue() == Math.PI); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem006().getStringValue().equals("This is an external context string")); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem007().getLongValue() == testDate.getTime()); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem008().getDateValue().equals(testDate)); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem009().getDateValue().getTime() == tci9A.getDateValue().getTime()); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem00A().getDateValue().getTime() == tciAA.getDateValue().getTime()); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem00B().getSetValue().equals(testTreeSet)); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem00C().getMapValue().equals(testHashMap)); + + final Collection mapValues = externalContextAlbum.values(); + assert (externalContextAlbum.values().containsAll(mapValues)); + + // Check that clearing does not work + try { + externalContextAlbum.clear(); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage() + .equals("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums")); + } + + assert (externalContextAlbum.size() == 1); + + try { + externalContextAlbum.containsKey(null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals("null values are illegal on method parameter \"key\"")); + } + + assert (externalContextAlbum.containsKey("externalContext")); + assert (!externalContextAlbum.containsKey("globalContext")); + + try { + externalContextAlbum.containsValue(null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals("null values are illegal on method parameter \"value\"")); + } + + assert (externalContextAlbum.containsValue(externalContext)); + assert (!externalContextAlbum.containsValue(new String("Hello"))); + + final Set> entrySet = externalContextAlbum.entrySet(); + assert (entrySet.size() == 1); + + try { + externalContextAlbum.get(null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage() + .equals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()")); + } + + final Object aObject = externalContextAlbum.get("externalContext"); + assert (aObject.equals(externalContext)); + + // put null keys should fail, throws a runtime exception + try { + externalContextAlbum.put(null, null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage() + .equals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()")); + } + + try { + externalContextAlbum.put("TestExternalContextItem00A", null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "album \"ExternalContextAlbum:0.0.1\" null values are illegal on key \"TestExternalContextItem00A\" for put()")); + } + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem00A().equals(tciAA)); + + // Should return the hash set + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem00B().getSetValue().equals(testTreeSet)); + + assert (externalContextAlbum.values().containsAll(mapValues)); + + // Set the write flag back as it should be + externalContextAlbum.getAlbumDefinition().setWritable(true); + + // Put should return the previous contextItem + final TestExternalContextItem externalContextOther = new TestExternalContextItem(); + externalContextOther.setTestExternalContextItem002(new TestContextItem002()); + externalContextOther.getTestExternalContextItem002().setIntValue(2000); + + assert (externalContextAlbum.put("externalContext", externalContextOther).equals(externalContext)); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem002().getIntValue() == 2000); + assert (externalContextAlbum.put("externalContext", externalContext).equals(externalContextOther)); + assert (((TestExternalContextItem) externalContextAlbum.get("externalContext")) + .getTestExternalContextItem002().getIntValue() == -1); + + try { + externalContextAlbum.put("TestExternalContextItem00A", null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "album \"ExternalContextAlbum:0.0.1\" null values are illegal on key \"TestExternalContextItem00A\" for put()")); + } + assert (externalContextAlbum.get("externalContext").equals(externalContext)); + + try { + externalContextAlbum.put("TestExternalContextItemFFF", null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "album \"ExternalContextAlbum:0.0.1\" null values are illegal on key \"TestExternalContextItemFFF\" for put()")); + } + assert (externalContextAlbum.size() == 1); + + try { + externalContextAlbum.put("TestExternalContextItemFFF", null); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "album \"ExternalContextAlbum:0.0.1\" null values are illegal on key \"TestExternalContextItemFFF\" for put()")); + } + assert (externalContextAlbum.size() == 1); + + // Should ignore remove + externalContextAlbum.remove("TestExternalContextItem017"); + assert (externalContextAlbum.size() == 1); + + assert (externalContextAlbum.values().size() == 1); + assert (externalContextAlbum.values().containsAll(mapValues)); + // CHECKSTYLE:ON: checkstyle:magicNumber + } catch (final Exception e) { + e.printStackTrace(); + contextDistributor.clear(); + assert ("Test has failed".equals("")); + } + + try { + contextDistributor.clear(); + } catch (final Exception e) { + e.printStackTrace(); + assert ("Test has failed".equals("")); + } + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java new file mode 100644 index 000000000..d93f15ed9 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/ContextUpdate.java @@ -0,0 +1,155 @@ +/*- + * ============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.context.test.distribution; + +import java.io.IOException; +import java.util.Date; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.TimeZone; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.concepts.TestContextItem008; +import org.onap.policy.apex.context.test.concepts.TestContextItem00A; +import org.onap.policy.apex.context.test.concepts.TestContextItem00C; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +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.handling.ApexModelException; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestContextUpdate checks context updates. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class ContextUpdate { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class); + + /** + * Test context update. + * + * @throws ApexModelException the apex model exception + * @throws IOException the IO exception + * @throws ApexException the apex exception + */ + public void testContextUpdate() throws ApexModelException, IOException, ApexException { + LOGGER.debug("Running TestContextUpdate test . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); + final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey); + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + // CHECKSTYLE:OFF: checkstyle:magicNumber + + final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + contextDistributor.registerModel(multiModel); + + final ContextAlbum longContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1")); + assert (longContextAlbum != null); + longContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + final ContextAlbum dateContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1")); + assert (dateContextAlbum != null); + longContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + final ContextAlbum mapContextAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("MapContextAlbum", "0.0.1")); + assert (mapContextAlbum != null); + mapContextAlbum.setUserArtifactStack(usedArtifactStackArray); + + final TestContextItem00A tciA = new TestContextItem00A(); + tciA.setDateValue(new TestContextItem008(new Date())); + tciA.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tciA.setDST(true); + tciA.setUTCOffset(-600); + tciA.setLocale(Locale.ENGLISH); + + final Map testHashMap = new HashMap<>(); + testHashMap.put("0", "zero"); + testHashMap.put("1", "one"); + testHashMap.put("2", "two"); + testHashMap.put("3", "three"); + testHashMap.put("4", "four"); + + final TestContextItem00C tciC = new TestContextItem00C(testHashMap); + + longContextAlbum.put("0", (long) 0); + longContextAlbum.put("0", 0); + longContextAlbum.put("0", "0"); + + try { + longContextAlbum.put("0", "zero"); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\"")); + } + + try { + longContextAlbum.put("0", ""); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals( + "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\"")); + } + + try { + longContextAlbum.put("0", null); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage() + .equals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()")); + } + + try { + longContextAlbum.put(null, null); + assert ("Test should throw an exception".equals("")); + } catch (final ContextRuntimeException e) { + assert (e.getMessage().equals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()")); + } + + assert (dateContextAlbum.put("date0", tciA) == null); + assert (dateContextAlbum.put("date0", tciA).equals(tciA)); + + assert (mapContextAlbum.put("map0", tciC) == null); + assert (mapContextAlbum.put("map0", tciC).equals(tciC)); + + contextDistributor.clear(); + // CHECKSTYLE:ON: checkstyle:magicNumber + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java new file mode 100644 index 000000000..28439b716 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/SequentialContextInstantiation.java @@ -0,0 +1,343 @@ +/*- + * ============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.context.test.distribution; + +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TimeZone; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.concepts.TestContextItem000; +import org.onap.policy.apex.context.test.concepts.TestContextItem001; +import org.onap.policy.apex.context.test.concepts.TestContextItem002; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.concepts.TestContextItem004; +import org.onap.policy.apex.context.test.concepts.TestContextItem005; +import org.onap.policy.apex.context.test.concepts.TestContextItem006; +import org.onap.policy.apex.context.test.concepts.TestContextItem007; +import org.onap.policy.apex.context.test.concepts.TestContextItem008; +import org.onap.policy.apex.context.test.concepts.TestContextItem009; +import org.onap.policy.apex.context.test.concepts.TestContextItem00A; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class SequentialContextInstantiation checks sequential initiation of context. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class SequentialContextInstantiation { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(SequentialContextInstantiation.class); + + /** + * Test sequential context instantiation. + * + * @throws ContextException the context exception + */ + // CHECKSTYLE:OFF: checkstyle:MaximumMethodLength + public void testSequentialContextInstantiation() throws ContextException { + // CHECKSTYLE:ON: MaximumMethodLength + LOGGER.debug("Running TestContextInstantiation test . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); + Distributor contextDistributor = null; + try { + contextDistributor = new DistributorFactory().getDistributor(distributorKey); + } catch (final Exception e) { + e.printStackTrace(); + assert ("Test has failed".equals("")); + return; + } + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + try { + // CHECKSTYLE:OFF: checkstyle:magicNumber + final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + contextDistributor.registerModel(multiModel); + + final ContextAlbum dateAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1")); + assert (dateAlbum != null); + dateAlbum.setUserArtifactStack(usedArtifactStackArray); + + final Date testDate = new Date(); + + final TestContextItem00A tciA00 = new TestContextItem00A(); + tciA00.setDateValue(new TestContextItem008(new Date())); + tciA00.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tciA00.setDST(true); + tciA00.setUTCOffset(-600); + tciA00.setLocale(Locale.ENGLISH); + + dateAlbum.put("dv0", tciA00); + assert (tciA00.equals(dateAlbum.get("dv0"))); + + dateAlbum.put("dv1", tciA00); + assert (tciA00.equals(dateAlbum.get("dv1"))); + + final TestContextItem009 tci9 = new TestContextItem009(); + tci9.setDateValue(new TestContextItem008(testDate)); + tci9.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName()); + tci9.setDST(true); + + try { + dateAlbum.put("tci9", tci9); + } catch (final Exception e) { + assert (e.getMessage().contains( + "class \"org.onap.policy.apex.context.test.concepts.TestContextItem009\" not compatible with class \"org.onap.policy.apex.context.test.concepts.TestContextItem00A\"")); + } + + final TestContextItem00A tciA01 = new TestContextItem00A(tciA00); + final TestContextItem00A tciA02 = new TestContextItem00A(tciA00); + final TestContextItem00A tciA03 = new TestContextItem00A(tciA00); + + final Map valueMap0 = new HashMap<>(); + valueMap0.put("Item01", tciA01); + valueMap0.put("Item02", tciA02); + valueMap0.put("Item03", tciA03); + + dateAlbum.putAll(valueMap0); + assert (dateAlbum.size() == 5); + assert (tciA01.equals(dateAlbum.get("Item01"))); + assert (tciA02.equals(dateAlbum.get("Item02"))); + assert (tciA03.equals(dateAlbum.get("Item03"))); + + final Float testFloat = new Float(3.14159265359); + + final TestContextItem000 testBadItem000 = new TestContextItem000(); + final TestContextItem001 testBadItem001 = new TestContextItem001(); + final TestContextItem002 testBadItem002 = new TestContextItem002(); + final TestContextItem003 testBadItem003 = new TestContextItem003(); + final TestContextItem004 testBadItem004 = new TestContextItem004(); + final TestContextItem005 testBadItem005 = new TestContextItem005(); + final TestContextItem006 testBadItem006 = new TestContextItem006(); + final TestContextItem007 testBadItem007 = new TestContextItem007(); + final TestContextItem008 testBadItem008 = new TestContextItem008(); + + testBadItem000.setFlag(false); + testBadItem001.setByteValue((byte) 0xFF); + testBadItem002.setIntValue(0xFFFFFFFF); + testBadItem003.setLongValue(0xFFFFFFFFFFFFFFFFL); + testBadItem004.setFloatValue(testFloat); + testBadItem005.setDoubleValue(Math.PI); + testBadItem006.setStringValue("This is a global context string"); + testBadItem007.setLongValue(testDate.getTime()); + testBadItem008.setDateValue(testDate); + + final Map valueMap1 = new HashMap<>(); + valueMap1.put("TestBadItem000", testBadItem000); + valueMap1.put("TestBadItem001", testBadItem001); + valueMap1.put("TestBadItem002", testBadItem002); + valueMap1.put("TestBadItem003", testBadItem003); + valueMap1.put("TestBadItem004", testBadItem004); + valueMap1.put("TestBadItem005", testBadItem005); + valueMap1.put("TestBadItem006", testBadItem006); + valueMap1.put("TestBadItem007", testBadItem007); + valueMap1.put("TestBadItem008", testBadItem008); + valueMap1.put("TestBadItem009", tci9); + valueMap1.put("Item00_2", tciA00); + + // Get another reference to the album + final ContextAlbum dateAlbum1 = + contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1")); + assert (dateAlbum1 != null); + dateAlbum1.setUserArtifactStack(usedArtifactStackArray); + + try { + dateAlbum1.putAll(valueMap1); + } catch (final Exception e) { + assert (e.getMessage().endsWith( + "not compatible with class \"org.onap.policy.apex.context.test.concepts.TestContextItem00A\"")); + } + assert (dateAlbum1.size() == 5); + + valueMap1.clear(); + valueMap1.put("Item00_2", tciA00); + dateAlbum1.putAll(valueMap1); + assert (dateAlbum1.size() == 6); + + assert (tciA00.equals(dateAlbum1.get("Item00_2"))); + dateAlbum.remove("Item00_2"); + assert (5 == dateAlbum1.size()); + + final ContextAlbum dateAlbumCopy = + contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1")); + assert (dateAlbumCopy != null); + dateAlbumCopy.setUserArtifactStack(usedArtifactStackArray); + + final Map valueMap2 = new HashMap<>(); + valueMap2.put("Item04", tciA01); + valueMap2.put("Item05", tciA02); + valueMap2.put("Item06", tciA03); + + dateAlbumCopy.putAll(valueMap2); + assert (8 == dateAlbumCopy.size()); + assert (tciA03.equals(dateAlbumCopy.get("Item06"))); + + final Collection mapValues = dateAlbum.values(); + assert (dateAlbumCopy.values().containsAll(mapValues)); + + // Check that clearing works + dateAlbum1.clear(); + assert (dateAlbum1.size() == 0); + + dateAlbum.put("Item00", tciA00); + final Map valueMap3 = new HashMap<>(); + valueMap3.put("Item01", tciA01); + valueMap3.put("Item02", tciA02); + valueMap3.put("Item03", tciA03); + dateAlbum.putAll(valueMap3); + + final Map valueMap4 = new HashMap<>(); + valueMap4.put("Item04", tciA01); + valueMap4.put("Item05", tciA02); + valueMap4.put("Item06", tciA03); + + dateAlbumCopy.putAll(valueMap4); + + try { + dateAlbum.containsKey(null); + } catch (final Exception e) { + assert (e.getMessage().equals("null values are illegal on method parameter \"key\"")); + } + + for (final Entry entry : dateAlbum.entrySet()) { + System.out.println(entry.getKey() + "->" + entry.getValue()); + } + assert (dateAlbum.containsKey("Item05")); + assert (!dateAlbum.containsKey("Item07")); + + try { + dateAlbum.containsValue(null); + } catch (final Exception e) { + assert (e.getMessage().equals("null values are illegal on method parameter \"value\"")); + } + + assert (dateAlbum.containsValue(tciA01)); + assert (!dateAlbum.containsValue(new String("Hello"))); + + final Set> entrySet = dateAlbum.entrySet(); + assert (entrySet.size() == 7); + + try { + assert (dateAlbum.get(null) == null); + } catch (final Exception e) { + assert (e.getMessage() + .equals("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for get()")); + } + + final Object aObject = dateAlbum.get("Item03"); + assert (aObject.equals(tciA03)); + + try { + assert (dateAlbum.put(null, null) == null); + } catch (final Exception e) { + assert (e.getMessage() + .equals("album \"DateContextAlbum:0.0.1\" null keys are illegal on keys for put()")); + } + + // Put null ContextItem should work (return null) + try { + dateAlbum.put("ItemNull", null); + } catch (final Exception e) { + assert (e.getMessage().equals( + "album \"DateContextAlbum:0.0.1\" null values are illegal on key \"ItemNull\" for put()")); + } + + // Should return null + assert (dateAlbum.get("ItemNull") == null); + + // Put should return the previous contextItem + tciA00.setDST(false); + final TestContextItem00A tciA03_clone = new TestContextItem00A(tciA03); + tciA03_clone.setDST(true); + TestContextItem00A retItem = (TestContextItem00A) dateAlbum.put("Item03", tciA03_clone); + assert (retItem.equals(tciA03)); + retItem = (TestContextItem00A) dateAlbum.put("Item03", tciA03); + assert (retItem.equals(tciA03_clone)); + + try { + dateAlbum.put("ItemNull", null); + assert (dateAlbum.get("ItemNull") == null); + } catch (final Exception e) { + assert (e.getMessage().equals( + "album \"DateContextAlbum:0.0.1\" null values are illegal on key \"ItemNull\" for put()")); + } + + dateAlbum.put("TestAA", tciA00); + assert (dateAlbum.get("TestAA").equals(tciA00)); + + // Should print warning + try { + dateAlbum.put("TestAA", null); + } catch (final Exception e) { + assert (e.getMessage().equals( + "album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAA\" for put()")); + } + assert (dateAlbum.size() == 8); + try { + dateAlbum.put("TestAB", null); + } catch (final Exception e) { + assert (e.getMessage().equals( + "album \"DateContextAlbum:0.0.1\" null values are illegal on key \"TestAB\" for put()")); + } + assert (dateAlbum.size() == 8); + + // Should do removes + dateAlbum.remove("TestAA"); + dateAlbum.remove("TestAB"); + dateAlbum.remove("ItemNull"); + assert (dateAlbum.size() == 7); + assert (dateAlbumCopy.values().containsAll(mapValues)); + // CHECKSTYLE:ON: checkstyle:magicNumber + } catch (final Exception e) { + e.printStackTrace(); + contextDistributor.clear(); + assert ("Test has failed".equals("")); + } + + try { + contextDistributor.clear(); + } catch (final Exception e) { + e.printStackTrace(); + assert ("Test has failed".equals("")); + } + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/package-info.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/package-info.java new file mode 100644 index 000000000..148c8cac4 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/distribution/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========================================================= + */ + +/** + * Runs context distribution tests in APEX. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.test.distribution; diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/TestContextAlbumFactory.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/TestContextAlbumFactory.java new file mode 100644 index 000000000..64de009d5 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/TestContextAlbumFactory.java @@ -0,0 +1,189 @@ +/*- + * ============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.context.test.factory; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; + +/** + * The Class TestContextAlbumFactory creates test context albums. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class TestContextAlbumFactory { + /** + * Default constructor to prevent sub-classing. + */ + private TestContextAlbumFactory() {} + + /** + * Creates the policy context model. + * + * @return the ax context model + */ + public static AxContextModel createPolicyContextModel() { + final AxContextSchema policySchema = new AxContextSchema(new AxArtifactKey("PolicySchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestPolicyContextItem"); + final AxContextAlbum albumDefinition = new AxContextAlbum(new AxArtifactKey("PolicyContextAlbum", "0.0.1"), + "APPLICATION", true, policySchema.getKey()); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1")); + schemas.getSchemasMap().put(policySchema.getKey(), policySchema); + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(albumDefinition.getKey(), albumDefinition); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxContextModel contextModel = + new AxContextModel(new AxArtifactKey("PolicyContextModel", "0.0.1"), schemas, albums, keyInformation); + contextModel.setKeyInformation(keyInformation); + keyInformation.generateKeyInfo(contextModel); + + return contextModel; + } + + /** + * Creates the global context model. + * + * @return the ax context model + */ + public static AxContextModel createGlobalContextModel() { + final AxContextSchema globalSchema = new AxContextSchema(new AxArtifactKey("GlobalSchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestGlobalContextItem"); + final AxContextAlbum albumDefinition = new AxContextAlbum(new AxArtifactKey("GlobalContextAlbum", "0.0.1"), + "GLOBAL", true, globalSchema.getKey()); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1")); + schemas.getSchemasMap().put(globalSchema.getKey(), globalSchema); + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(albumDefinition.getKey(), albumDefinition); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxContextModel contextModel = + new AxContextModel(new AxArtifactKey("GlobalContextModel", "0.0.1"), schemas, albums, keyInformation); + contextModel.setKeyInformation(keyInformation); + keyInformation.generateKeyInfo(contextModel); + + return contextModel; + } + + /** + * Creates the external context model. + * + * @return the ax context model + */ + public static AxContextModel createExternalContextModel() { + final AxContextSchema externalSchema = new AxContextSchema(new AxArtifactKey("ExternalSchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestExternalContextItem"); + final AxContextAlbum albumDefinition = new AxContextAlbum(new AxArtifactKey("ExternalContextAlbum", "0.0.1"), + "EXTERNAL", true, externalSchema.getKey()); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1")); + schemas.getSchemasMap().put(externalSchema.getKey(), externalSchema); + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(albumDefinition.getKey(), albumDefinition); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxContextModel contextModel = + new AxContextModel(new AxArtifactKey("ExternalContextModel", "0.0.1"), schemas, albums, keyInformation); + contextModel.setKeyInformation(keyInformation); + keyInformation.generateKeyInfo(contextModel); + + return contextModel; + } + + /** + * Creates the long context model. + * + * @return the ax context model + */ + public static AxContextModel createLongContextModel() { + final AxContextSchema longSchema = + new AxContextSchema(new AxArtifactKey("LongSchema", "0.0.1"), "Java", "java.lang.Long"); + final AxContextAlbum albumDefinition1 = new AxContextAlbum(new AxArtifactKey("LongContextAlbum1", "0.0.1"), + "APPLICATION", true, longSchema.getKey()); + final AxContextAlbum albumDefinition2 = new AxContextAlbum(new AxArtifactKey("LongContextAlbum2", "0.0.1"), + "APPLICATION", true, longSchema.getKey()); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1")); + schemas.getSchemasMap().put(longSchema.getKey(), longSchema); + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(albumDefinition1.getKey(), albumDefinition1); + albums.getAlbumsMap().put(albumDefinition2.getKey(), albumDefinition2); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxContextModel contextModel = + new AxContextModel(new AxArtifactKey("LongContextModel", "0.0.1"), schemas, albums, keyInformation); + contextModel.setKeyInformation(keyInformation); + keyInformation.generateKeyInfo(contextModel); + + return contextModel; + } + + /** + * Creates the multi albums context model. + * + * @return the ax context model + */ + public static AxContextModel createMultiAlbumsContextModel() { + final AxContextSchema longSchema = + new AxContextSchema(new AxArtifactKey("LongSchema", "0.0.1"), "Java", "java.lang.Long"); + final AxContextSchema lTypeSchema = new AxContextSchema(new AxArtifactKey("LTypeSchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestContextItem003"); + final AxContextSchema dateSchema = new AxContextSchema(new AxArtifactKey("DateSchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestContextItem00A"); + final AxContextSchema mapSchema = new AxContextSchema(new AxArtifactKey("MapSchema", "0.0.1"), "Java", + "org.onap.policy.apex.context.test.concepts.TestContextItem00C"); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("Schemas", "0.0.1")); + schemas.getSchemasMap().put(longSchema.getKey(), longSchema); + schemas.getSchemasMap().put(lTypeSchema.getKey(), lTypeSchema); + schemas.getSchemasMap().put(dateSchema.getKey(), dateSchema); + schemas.getSchemasMap().put(mapSchema.getKey(), mapSchema); + + final AxContextAlbum longAlbumDefinition = new AxContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"), + "APPLICATION", true, longSchema.getKey()); + final AxContextAlbum lTypeAlbumDefinition = new AxContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1"), + "APPLICATION", true, lTypeSchema.getKey()); + final AxContextAlbum dateAlbumDefinition = new AxContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1"), + "APPLICATION", true, dateSchema.getKey()); + final AxContextAlbum mapAlbumDefinition = new AxContextAlbum(new AxArtifactKey("MapContextAlbum", "0.0.1"), + "APPLICATION", true, mapSchema.getKey()); + + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(longAlbumDefinition.getKey(), longAlbumDefinition); + albums.getAlbumsMap().put(lTypeAlbumDefinition.getKey(), lTypeAlbumDefinition); + albums.getAlbumsMap().put(dateAlbumDefinition.getKey(), dateAlbumDefinition); + albums.getAlbumsMap().put(mapAlbumDefinition.getKey(), mapAlbumDefinition); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxContextModel contextModel = new AxContextModel(new AxArtifactKey("MultiAlbumsContextModel", "0.0.1"), + schemas, albums, keyInformation); + contextModel.setKeyInformation(keyInformation); + keyInformation.generateKeyInfo(contextModel); + + return contextModel; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/package-info.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/package-info.java new file mode 100644 index 000000000..ea70ce2a8 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/factory/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========================================================= + */ + +/** + * Contains factories for creating test context albums. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.test.factory; diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java new file mode 100644 index 000000000..971d0c421 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContext.java @@ -0,0 +1,194 @@ +/*- + * ============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.context.test.locking; + +import java.io.IOException; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +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.handling.ApexModelException; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestConcurrentContext tests concurrent use of context. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ConcurrentContext { + private static final int TEN_MILLISECONDS = 10; + + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContext.class); + + // The context distributor and map used by each test + private Distributor contextDistributor = null; + private ContextAlbum lTypeAlbum = null; + + /** + * Test concurrent context. + * + * @param testType the test type + * @param jvmCount the jvm count + * @param threadCount the thread count + * @param threadLoops the thread loops + * @return the long + * @throws ApexModelException the apex model exception + * @throws IOException the IO exception + * @throws ApexException the apex exception + */ + public long testConcurrentContext(final String testType, final int jvmCount, final int threadCount, + final int threadLoops) throws ApexModelException, IOException, ApexException { + final ConcurrentContext concurrentContext = new ConcurrentContext(); + + try { + concurrentContext.setupAndVerifyContext(); + } catch (final Exception e) { + e.printStackTrace(); + throw e; + } + + LOGGER.debug("starting JVMs and threads . . ."); + + final Thread[] threadArray = new Thread[threadCount]; + + // Check if we have a single JVM or multiple JVMs + int runningThreadCount = -1; + if (jvmCount == 1) { + // Run everything in this JVM + for (int t = 0; t < threadCount; t++) { + threadArray[t] = new Thread(new ConcurrentContextThread(0, t, threadLoops)); + threadArray[t].setName(testType + ":TestConcurrentContextThread_0_" + t); + threadArray[t].start(); + } + + runningThreadCount = threadCount; + } else { + // Spawn JVMs to run the tests + for (int j = 0; j < jvmCount; j++) { + threadArray[j] = new Thread(new ConcurrentContextJVMThread(testType, j, threadCount, threadLoops)); + threadArray[j].setName(testType + ":TestConcurrentContextJVMThread_" + j); + threadArray[j].start(); + } + runningThreadCount = jvmCount; + } + + boolean allFinished; + do { + allFinished = true; + for (int i = 0; i < runningThreadCount; i++) { + if (threadArray[i].isAlive()) { + allFinished = false; + try { + Thread.sleep(TEN_MILLISECONDS); + } catch (final Exception e) { + } + break; + } + } + } while (!allFinished); + + return concurrentContext.verifyAndClearContext(jvmCount, threadCount, threadLoops); + } + + /** + * Setup and verify context. + * + * @throws ContextException the context exception + */ + private void setupAndVerifyContext() throws ContextException { + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1"); + contextDistributor = new DistributorFactory().getDistributor(distributorKey); + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + contextDistributor.registerModel(albumsModel); + + lTypeAlbum = contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); + assert (lTypeAlbum != null); + lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); + + // CHECKSTYLE:OFF: checkstyle:magicNumber + lTypeAlbum.put("lTypeValue0", new TestContextItem003(0xFFFFFFFFFFFFFFFFL)); + lTypeAlbum.put("lTypeValue1", new TestContextItem003(0xFFFFFFFFFFFFFFFEL)); + lTypeAlbum.put("lTypeValue2", new TestContextItem003(0xFFFFFFFFFFFFFFFDL)); + lTypeAlbum.put("lTypeValue3", new TestContextItem003(0xFFFFFFFFFFFFFFFCL)); + lTypeAlbum.put("lTypeValue4", new TestContextItem003(0xFFFFFFFFFFFFFFFBL)); + lTypeAlbum.put("lTypeValue5", new TestContextItem003(0xFFFFFFFFFFFFFFFAL)); + lTypeAlbum.put("lTypeValue6", new TestContextItem003(0xFFFFFFFFFFFFFFF9L)); + lTypeAlbum.put("lTypeValue7", new TestContextItem003(0xFFFFFFFFFFFFFFF8L)); + lTypeAlbum.put("lTypeValue8", new TestContextItem003(0xFFFFFFFFFFFFFFF7L)); + lTypeAlbum.put("lTypeValue9", new TestContextItem003(0xFFFFFFFFFFFFFFF6L)); + lTypeAlbum.put("lTypeValueA", new TestContextItem003(0xFFFFFFFFFFFFFFF5L)); + lTypeAlbum.put("lTypeValueB", new TestContextItem003(0xFFFFFFFFFFFFFFF4L)); + lTypeAlbum.put("lTypeValueC", new TestContextItem003(0xFFFFFFFFFFFFFFF3L)); + lTypeAlbum.put("lTypeValueD", new TestContextItem003(0xFFFFFFFFFFFFFFF2L)); + lTypeAlbum.put("lTypeValueE", new TestContextItem003(0xFFFFFFFFFFFFFFF1L)); + lTypeAlbum.put("lTypeValueF", new TestContextItem003(0xFFFFFFFFFFFFFFF0L)); + LOGGER.debug(lTypeAlbum.toString()); + assert (lTypeAlbum.size() >= 16); + // CHECKSTYLE:ON: checkstyle:magicNumber + + // The initial value for concurrent testing + final TestContextItem003 item = new TestContextItem003(0L); + lTypeAlbum.put("testValue", item); + + } + + /** + * Verify and clear context. + * + * @param jvmCount the jvm count + * @param threadCount the thread count + * @param threadLoops the thread loops + * @return the long + * @throws ContextException the context exception + */ + private long verifyAndClearContext(final int jvmCount, final int threadCount, final int threadLoops) + throws ContextException { + try { + LOGGER.debug("threads finished, end value is {}", + ((TestContextItem003) lTypeAlbum.get("testValue")).getLongValue()); + } catch (final Exception e) { + e.printStackTrace(); + } + final long total = ((TestContextItem003) lTypeAlbum.get("testValue")).getLongValue(); + + contextDistributor.clear(); + contextDistributor = null; + + return total; + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java new file mode 100644 index 000000000..0145bd161 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVM.java @@ -0,0 +1,219 @@ +/*- + * ============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.context.test.locking; + +import com.google.gson.Gson; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map.Entry; +import java.util.TreeSet; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +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.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class ConcurrentContextJVM tests concurrent use of context in a single JVM. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public final class ConcurrentContextJVM { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVM.class); + + private static final int TEN_MILLISECONDS = 10; + private static final int IPV4_ADDRESS_LENGTH = 4; + + /** + * The Constructor. + * + * @param testType the test type + * @param jvmNo the jvm no + * @param threadCount the thread count + * @param threadLoops the thread loops + * @throws ApexException the apex exception + */ + private ConcurrentContextJVM(final String testType, final int jvmNo, final int threadCount, final int threadLoops) + throws ApexException { + super(); + LOGGER.debug("starting JVMs and threads . . ."); + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor" + jvmNo, "0.0.1"); + final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey); + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + contextDistributor.registerModel(albumsModel); + + final ContextAlbum lTypeAlbum = + contextDistributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); + assert (lTypeAlbum != null); + lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); + + final Thread[] threadArray = new Thread[threadCount]; + + for (int t = 0; t < threadCount; t++) { + threadArray[t] = new Thread(new ConcurrentContextThread(jvmNo, t, threadLoops)); + threadArray[t].setName(testType + ":ConcurrentContextThread_" + jvmNo + "_" + t); + threadArray[t].start(); + LOGGER.debug("started thread " + threadArray[t].getName()); + } + + boolean allFinished; + do { + allFinished = true; + for (int t = 0; t < threadCount; t++) { + if (threadArray[t].isAlive()) { + allFinished = false; + try { + Thread.sleep(TEN_MILLISECONDS); + } catch (final Exception e) { + } + break; + } + } + } while (!allFinished); + + LOGGER.debug("threads finished, end value is {}", lTypeAlbum.get("testValue")); + contextDistributor.clear(); + } + + /** + * The main method. + * + * @param args the args + * @throws Exception Any exception thrown by the test code + */ + @SuppressWarnings("unchecked") + public static void main(final String[] args) throws Exception { + configure(); + + System.out.println("JVM Arguments: " + Arrays.toString(args)); + // CHECKSTYLE:OFF: checkstyle:magicNumber + + // An even number of arguments greater than 3 + if (args.length < 4 || (args.length % 2 != 0)) { + LOGGER.error("invalid arguments: " + Arrays.toString(args)); + LOGGER.error( + "usage: TestConcurrentContextJVM testType jvmNo threadCount threadLoops [parameterKey parameterJson].... "); + return; + } + + int jvmNo = -1; + int threadCount = -1; + int threadLoops = -1; + + try { + jvmNo = Integer.parseInt(args[1]); + } catch (final Exception e) { + LOGGER.error("invalid argument jvmNo", e); + return; + } + + try { + threadCount = Integer.parseInt(args[2]); + } catch (final Exception e) { + LOGGER.error("invalid argument threadCount", e); + return; + } + + try { + threadLoops = Integer.parseInt(args[3]); + } catch (final Exception e) { + LOGGER.error("invalid argument threadLoops", e); + return; + } + + for (int p = 4; p < args.length - 1; p += 2) { + @SuppressWarnings("rawtypes") + final Class parametersClass = Class.forName(args[p]); + final AbstractParameters parameters = + (AbstractParameters) new Gson().fromJson(args[p + 1], parametersClass); + ParameterService.registerParameters(parametersClass, parameters); + } + + for (final Entry, AbstractParameters> parameterEntry : ParameterService.getAll()) { + LOGGER.info("Parameter class " + parameterEntry.getKey().getCanonicalName() + "=" + + parameterEntry.getValue().toString()); + } + + try { + new ConcurrentContextJVM(args[0], jvmNo, threadCount, threadLoops); + } catch (final Exception e) { + LOGGER.error("error running test in JVM", e); + return; + } + // CHECKSTYLE:ON: checkstyle:magicNumber + } + + /** + * This method setus up any static configuration required by the JVM. + * + * @throws Exception on configuration errors + */ + public static void configure() throws Exception { + System.setProperty("java.net.preferIPv4Stack", "true"); + System.setProperty("hazelcast.config", "src/test/resources/hazelcast/hazelcast.xml"); + + // The JGroups IP address must be set to a real (not loopback) IP address for Infinispan to work. IN order to + // ensure that all + // the JVMs in a test pick up the same IP address, this function sets te address to be the first non-loopback + // IPv4 address + // on a host + final TreeSet ipAddressSet = new TreeSet(); + + final Enumeration nets = NetworkInterface.getNetworkInterfaces(); + for (final NetworkInterface netint : Collections.list(nets)) { + final Enumeration inetAddresses = netint.getInetAddresses(); + for (final InetAddress inetAddress : Collections.list(inetAddresses)) { + // Look for real IPv4 Internet addresses + if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == IPV4_ADDRESS_LENGTH) { + ipAddressSet.add(inetAddress.getHostAddress()); + } + } + } + + if (ipAddressSet.size() == 0) { + throw new Exception("cound not find real IP address for test"); + } + System.out.println("Setting jgroups.tcp.address to: " + ipAddressSet.first()); + System.setProperty("jgroups.tcp.address", ipAddressSet.first()); + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java new file mode 100644 index 000000000..0f8234196 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextJVMThread.java @@ -0,0 +1,123 @@ +/*- + * ============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.context.test.locking; + +import com.google.gson.Gson; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.service.AbstractParameters; +import org.onap.policy.apex.model.basicmodel.service.ParameterService; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestConcurrentContextThread tests concurrent use of context. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ConcurrentContextJVMThread implements Runnable { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextJVMThread.class); + + private final String testType; + private final int jvm; + private final int threadCount; + private final int target; + + /** + * The Constructor. + * + * @param testType the test type + * @param jvm the jvm + * @param threadCount the thread count + * @param target the target + * @throws ApexException the apex exception + */ + public ConcurrentContextJVMThread(final String testType, final int jvm, final int threadCount, final int target) + throws ApexException { + this.testType = testType; + this.jvm = jvm; + this.threadCount = threadCount; + this.target = target; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + final List commandList = new ArrayList<>(); + commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin" + + System.getProperty("file.separator") + "java"); + commandList.add("-cp"); + commandList.add(System.getProperty("java.class.path")); + commandList.add("org.onap.policy.apex.context.test.locking.ConcurrentContextJVM"); + commandList.add(testType); + commandList.add(new Integer(jvm).toString()); + commandList.add(new Integer(threadCount).toString()); + commandList.add(new Integer(target).toString()); + + for (final Entry, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) { + commandList.add(parameterServiceEntry.getKey().getCanonicalName()); + commandList.add(new Gson().toJson(parameterServiceEntry.getValue())); + } + + LOGGER.info("starting JVM " + jvm); + + // Run the JVM + final ProcessBuilder processBuilder = new ProcessBuilder(commandList); + processBuilder.redirectErrorStream(true); + Process process; + + try { + process = processBuilder.start(); + + final InputStream is = process.getInputStream(); + final InputStreamReader isr = new InputStreamReader(is); + final BufferedReader br = new BufferedReader(isr); + String line; + LOGGER.info("JVM Output for command " + commandList + "\n"); + while ((line = br.readLine()) != null) { + LOGGER.info(line); + } + + // Wait to get exit value + try { + final int exitValue = process.waitFor(); + LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + } catch (final IOException e1) { + e1.printStackTrace(); + } + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java new file mode 100644 index 000000000..0691b8ae5 --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/ConcurrentContextThread.java @@ -0,0 +1,149 @@ +/*- + * ============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.context.test.locking; + +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestConcurrentContextThread tests concurrent use of context. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ConcurrentContextThread implements Runnable { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextThread.class); + private final Distributor distributor; + private final int jvm; + private final int instance; + private final int threadLoops; + + /** + * The Constructor. + * + * @param jvm the jvm + * @param instance the instance + * @param threadLoops the thread loops + * @throws ApexException the apex exception + */ + public ConcurrentContextThread(final int jvm, final int instance, final int threadLoops) throws ApexException { + this.jvm = jvm; + this.instance = instance; + this.threadLoops = threadLoops; + + final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor_" + jvm + "_" + instance, "0.0.1"); + + new ContextParameters(); + distributor = new DistributorFactory().getDistributor(distributorKey); + final AxContextModel albumsModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + distributor.registerModel(albumsModel); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + LOGGER.info("running TestConcurrentContextThread_" + jvm + "_" + instance + " . . ."); + + ContextAlbum lTypeAlbum = null; + + try { + lTypeAlbum = distributor.createContextAlbum(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); + } catch (final Exception e) { + LOGGER.error("could not get the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + + if (lTypeAlbum == null) { + LOGGER.error("could not find the test context album"); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + + // @formatter:off + final AxArtifactKey[] usedArtifactStackArray = { + new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), + new AxArtifactKey("testC-bot", "0.0.1") + }; + // @formatter:on + + lTypeAlbum.setUserArtifactStack(usedArtifactStackArray); + + for (int i = 0; i < threadLoops; i++) { + try { + lTypeAlbum.lockForWriting("testValue"); + TestContextItem003 item = (TestContextItem003) lTypeAlbum.get("testValue"); + if (item != null) { + long value = item.getLongValue(); + item.setLongValue(++value); + } else { + item = new TestContextItem003(0L); + } + lTypeAlbum.put("testValue", item); + } catch (final Exception e) { + LOGGER.error("could not set the value in the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } finally { + try { + lTypeAlbum.unlockForWriting("testValue"); + } catch (final ContextException e) { + LOGGER.error("could not unlock test context album item", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + return; + } + } + } + + try { + lTypeAlbum.lockForWriting("testValue"); + final TestContextItem003 item = (TestContextItem003) lTypeAlbum.get("testValue"); + final long value = item.getLongValue(); + LOGGER.info("completed TestConcurrentContextThread_" + jvm + "_" + instance + ", value=" + value); + } catch (final Exception e) { + LOGGER.error("could not read the value in the test context album", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + } finally { + try { + lTypeAlbum.unlockForWriting("testValue"); + distributor.shutdown(); + } catch (final ContextException e) { + LOGGER.error("could not unlock test context album item", e); + LOGGER.error("failed TestConcurrentContextThread_" + jvm + "_" + instance); + } + } + } +} diff --git a/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/package-info.java b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/package-info.java new file mode 100644 index 000000000..0e80e611d --- /dev/null +++ b/context/context-test/src/main/java/org/onap/policy/apex/context/test/locking/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========================================================= + */ + +/** + * Contains tests to check APEX context album distributed locking. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ + +package org.onap.policy.apex.context.test.locking; diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextAlbumUpdate.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextAlbumUpdate.java new file mode 100644 index 000000000..7d5693d5a --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextAlbumUpdate.java @@ -0,0 +1,65 @@ +/*- + * ============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.context.test.distribution; + + +import java.io.IOException; + +import org.junit.After; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +public class TestContextAlbumUpdate { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(TestContextAlbumUpdate.class); + + @Test + public void testContextAlbumUpdateJVMLocalVarSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextAlbumUpdateJVMLocalVarSet test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getDistributorParameters() + .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"); + new ContextAlbumUpdate().testContextAlbumUpdate(); + + logger.debug("Ran testContextAlbumUpdateJVMLocalVarSet test"); + } + + @Test + public void testContextAlbumUpdateJVMLocalVarNotSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextAlbumUpdateJVMLocalVarNotSet test . . ."); + + new ContextParameters(); + new ContextAlbumUpdate().testContextAlbumUpdate(); + + logger.debug("Ran testContextAlbumUpdateJVMLocalVarNotSet test"); + } + + /** + * Test context update cleardown. + */ + @After + public void testContextAlbumUpdateCleardown() {} +} diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextInstantiation.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextInstantiation.java new file mode 100644 index 000000000..f640b339e --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextInstantiation.java @@ -0,0 +1,75 @@ +/*- + * ============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.context.test.distribution; + + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestContextInstantiation. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class TestContextInstantiation { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(TestContextInstantiation.class); + + @Before + public void beforeTest() {} + + /** + * Test context instantiation clear down. + */ + @After + public void afterTest() {} + + @Test + public void testContextInstantiationJVMLocalVarSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextInstantiationJVMLocalVarSet test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getDistributorParameters() + .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"); + new ContextInstantiation().testContextInstantiation(); + + logger.debug("Ran testContextInstantiationJVMLocalVarSet test"); + } + + @Test + public void testContextInstantiationJVMLocalVarNotSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextInstantiationJVMLocalVarNotSet test . . ."); + + new ContextParameters(); + new ContextInstantiation().testContextInstantiation(); + + logger.debug("Ran testContextInstantiationJVMLocalVarNotSet test"); + } + +} diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextUpdate.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextUpdate.java new file mode 100644 index 000000000..d140b0da1 --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestContextUpdate.java @@ -0,0 +1,62 @@ +/*- + * ============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.context.test.distribution; + +import java.io.IOException; + +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestContextUpdate. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class TestContextUpdate { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdate.class); + + @Test + public void testContextUpdateJVMLocalVarSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextUpdateJVMLocalVarSet test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getDistributorParameters() + .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"); + new ContextUpdate().testContextUpdate(); + + logger.debug("Ran testContextUpdateJVMLocalVarSet test"); + } + + @Test + public void testContextUpdateJVMLocalVarNotSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testContextUpdateJVMLocalVarNotSet test . . ."); + + new ContextParameters(); + new ContextUpdate().testContextUpdate(); + + logger.debug("Ran testContextUpdateJVMLocalVarNotSet test"); + } +} diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestSequentialContextInstantiation.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestSequentialContextInstantiation.java new file mode 100644 index 000000000..27987595c --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/distribution/TestSequentialContextInstantiation.java @@ -0,0 +1,73 @@ +/*- + * ============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.context.test.distribution; + + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestContextInstantiation. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class TestSequentialContextInstantiation { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(TestSequentialContextInstantiation.class); + + @Before + public void beforeTest() {} + + @After + public void afterTest() {} + + @Test + public void testSequentialContextInstantiationJVMLocalVarSet() + throws ApexModelException, IOException, ApexException { + logger.debug("Running testSequentialContextInstantiationJVMLocalVarSet test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getDistributorParameters() + .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"); + new SequentialContextInstantiation().testSequentialContextInstantiation(); + + logger.debug("Ran testSequentialContextInstantiationJVMLocalVarSet test"); + } + + @Test + public void testSequentialContextInstantiationJVMLocalVarNotSet() + throws ApexModelException, IOException, ApexException { + logger.debug("Running testSequentialContextInstantiationJVMLocalVarNotSet test . . ."); + + new ContextParameters(); + new SequentialContextInstantiation().testSequentialContextInstantiation(); + + logger.debug("Ran testSequentialContextInstantiationJVMLocalVarNotSet test"); + } +} diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/locking/TestConcurrentContext.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/locking/TestConcurrentContext.java new file mode 100644 index 000000000..0f6626ff1 --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/locking/TestConcurrentContext.java @@ -0,0 +1,97 @@ +/*- + * ============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.context.test.locking; + + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * The Class TestConcurrentContext tests concurrent use of context. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestConcurrentContext { + // Logger for this class + private static final XLogger logger = XLoggerFactory.getXLogger(TestConcurrentContext.class); + + // Test parameters + private static final int TEST_JVM_COUNT_SINGLE_JVM = 1; + private static final int TEST_JVM_COUNT_MULTI_JVM = 3; + private static final int TEST_THREAD_COUNT_SINGLE_JVM = 64; + private static final int TEST_THREAD_COUNT_MULTI_JVM = 20; + private static final int TEST_THREAD_LOOPS = 100; + + @Test + public void testConcurrentContextJVMLocalVarSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testConcurrentContextJVMLocalVarSet test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager"); + final long result = new ConcurrentContext().testConcurrentContext("JVMLocalVarSet", TEST_JVM_COUNT_SINGLE_JVM, + TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS); + + assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result); + + logger.debug("Ran testConcurrentContextJVMLocalVarSet test"); + } + + @Test + public void testConcurrentContextJVMLocalNoVarSet() throws ApexModelException, IOException, ApexException { + logger.debug("Running testConcurrentContextJVMLocalNoVarSet test . . ."); + + new ContextParameters(); + final long result = new ConcurrentContext().testConcurrentContext("JVMLocalNoVarSet", TEST_JVM_COUNT_SINGLE_JVM, + TEST_THREAD_COUNT_SINGLE_JVM, TEST_THREAD_LOOPS); + + assertEquals(TEST_JVM_COUNT_SINGLE_JVM * TEST_THREAD_COUNT_SINGLE_JVM * TEST_THREAD_LOOPS, result); + + logger.debug("Ran testConcurrentContextJVMLocalNoVarSet test"); + } + + @Test + public void testConcurrentContextMultiJVMNoLock() throws ApexModelException, IOException, ApexException { + logger.debug("Running testConcurrentContextMultiJVMNoLock test . . ."); + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getDistributorParameters() + .setPluginClass("org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor"); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager"); + + final long result = new ConcurrentContext().testConcurrentContext("testConcurrentContextMultiJVMNoLock", + TEST_JVM_COUNT_MULTI_JVM, TEST_THREAD_COUNT_MULTI_JVM, TEST_THREAD_LOOPS); + + // No concurrent map so result will be zero + assertEquals(0, result); + + logger.debug("Ran testConcurrentContextMultiJVMNoLock test"); + } +} diff --git a/context/context-test/src/test/java/org/onap/policy/apex/context/test/persistence/TestPersistentContextInstantiation.java b/context/context-test/src/test/java/org/onap/policy/apex/context/test/persistence/TestPersistentContextInstantiation.java new file mode 100644 index 000000000..db1ac47fe --- /dev/null +++ b/context/context-test/src/test/java/org/onap/policy/apex/context/test/persistence/TestPersistentContextInstantiation.java @@ -0,0 +1,184 @@ +/*- + * ============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.context.test.persistence; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.context.test.concepts.TestContextItem003; +import org.onap.policy.apex.context.test.concepts.TestContextItem008; +import org.onap.policy.apex.context.test.concepts.TestContextItem00A; +import org.onap.policy.apex.context.test.concepts.TestContextItem00C; +import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory; +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.dao.ApexDao; +import org.onap.policy.apex.model.basicmodel.dao.ApexDaoFactory; +import org.onap.policy.apex.model.basicmodel.dao.DAOParameters; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel; + +/** + * The Class TestContextInstantiation. + * + * @author Sergey Sachkov (sergey.sachkov@ericsson.com) + */ +public class TestPersistentContextInstantiation { + // Logger for this class + // private static final XLogger logger = XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class); + + private Connection connection; + + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @After + public void afterTest() throws IOException {} + + @Test + public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException { + + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.setPersistorParameters(new PersistorParameters()); + + final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1"); + final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey); + + final AxArtifactKey[] usedArtifactStackArray = { new AxArtifactKey("testC-top", "0.0.1"), + new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1") }; + + final DAOParameters daoParameters = new DAOParameters(); + daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + daoParameters.setPersistenceUnit("DAOTest"); + final ApexDao apexDao = new ApexDaoFactory().createApexDao(daoParameters); + apexDao.init(daoParameters); + + final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel(); + + // Context for Storing Map values + final AxContextAlbum axContextAlbumForMap = + someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("MapContextAlbum", "0.0.1")); + apexDao.create(axContextAlbumForMap); + contextDistributor.registerModel(someContextModel); + final ContextAlbum contextAlbumForMap = contextDistributor.createContextAlbum(axContextAlbumForMap.getKey()); + assertNotNull(contextAlbumForMap); + contextAlbumForMap.setUserArtifactStack(usedArtifactStackArray); + + final Map testMap = new HashMap(); + testMap.put("key", "This is a policy context string"); + + final Map valueMap0 = new HashMap(); + valueMap0.put("TestPolicyContextItem000", new TestContextItem00C(testMap)); + + contextAlbumForMap.putAll(valueMap0); + + assertEquals(((TestContextItem00C) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue().get("key"), + "This is a policy context string"); + + contextAlbumForMap.flush(); + + // Context for Storing Date values + final AxContextAlbum axContextAlbumForDate = + someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("DateContextAlbum", "0.0.1")); + apexDao.create(axContextAlbumForDate); + contextDistributor.registerModel(someContextModel); + final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey()); + assertNotNull(contextAlbumForDate); + contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray); + + final TestContextItem008 testDate = new TestContextItem008(new Date()); + final TestContextItem00A tci00A = new TestContextItem00A(); + tci00A.setDateValue(testDate); + tci00A.setTZValue(TimeZone.getTimeZone("Europe/Dublin").toString()); + tci00A.setDST(true); + + final Map valueMap1 = new HashMap(); + valueMap1.put("TestPolicyContextItem00A", tci00A); + + contextAlbumForDate.putAll(valueMap1); + + assertEquals(((TestContextItem00A) contextAlbumForDate.get("TestPolicyContextItem00A")).getDateValue(), + testDate); + assertEquals(((TestContextItem00A) contextAlbumForDate.get("TestPolicyContextItem00A")).getDST(), true); + + contextAlbumForDate.flush(); + + // Context for Storing Long values + final AxContextAlbum axContextAlbumForLong = + someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("LTypeContextAlbum", "0.0.1")); + apexDao.create(axContextAlbumForLong); + contextDistributor.registerModel(someContextModel); + final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey()); + assertNotNull(contextAlbumForLong); + contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray); + + final Map valueMap2 = new HashMap(); + valueMap2.put("TestPolicyContextItem0031", new TestContextItem003(0xFFFFFFFFFFFFFFFFL)); + valueMap2.put("TestPolicyContextItem0032", new TestContextItem003(0xFFFFFFFFFFFFFFFEL)); + valueMap2.put("TestPolicyContextItem0033", new TestContextItem003(0xFFFFFFFFFFFFFFFDL)); + valueMap2.put("TestPolicyContextItem0034", new TestContextItem003(0xFFFFFFFFFFFFFFFCL)); + valueMap2.put("TestPolicyContextItem0035", new TestContextItem003(0xFFFFFFFFFFFFFFFBL)); + + contextAlbumForLong.putAll(valueMap2); + + assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue(), + 0xFFFFFFFFFFFFFFFFL); + assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue(), + 0xFFFFFFFFFFFFFFFEL); + assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue(), + 0xFFFFFFFFFFFFFFFDL); + assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue(), + 0xFFFFFFFFFFFFFFFCL); + assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue(), + 0xFFFFFFFFFFFFFFFBL); + + contextAlbumForLong.flush(); + contextDistributor.clear(); + + } +} diff --git a/context/context-test/src/test/resources/META-INF/persistence.xml b/context/context-test/src/test/resources/META-INF/persistence.xml new file mode 100644 index 000000000..ec9357851 --- /dev/null +++ b/context/context-test/src/test/resources/META-INF/persistence.xml @@ -0,0 +1,50 @@ + + + + + + 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 + + + + + + + + + + + + diff --git a/context/pom.xml b/context/pom.xml new file mode 100644 index 000000000..dad850230 --- /dev/null +++ b/context/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + org.onap.policy.apex-pdp + apex-pdp + 2.0.0-SNAPSHOT + + + org.onap.policy.apex-pdp.context + context + pom + + ${project.artifactId} + The context handling framework of Apex. + + + context-management + context-test + + \ No newline at end of file -- cgit 1.2.3-korg