aboutsummaryrefslogtreecommitdiffstats
path: root/model/policy-model/src
diff options
context:
space:
mode:
authora.sreekumar <ajith.sreekumar@est.tech>2019-10-16 11:46:21 +0100
committerAjith Sreekumar <ajith.sreekumar@est.tech>2019-11-01 08:49:37 +0000
commit82da1e9fa73e6a19455ea979bbf084aeed43af90 (patch)
tree88efd6fe5317262d894f8945c39ec431748bfca8 /model/policy-model/src
parentc03a0455e2956e43e425d6f4121ab5d8d20158f1 (diff)
Resolve mapping between TOSCA policies and APEX policy models
Change-Id: Ifaedc5074bcc51a5d495e342feae89b6a2aac1cf Issue-ID: POLICY-1626 Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'model/policy-model/src')
-rw-r--r--model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMerger.java179
-rw-r--r--model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java95
2 files changed, 137 insertions, 137 deletions
diff --git a/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMerger.java b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMerger.java
index 28133cefa..37c3b2368 100644
--- a/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMerger.java
+++ b/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMerger.java
@@ -1,32 +1,38 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.model.policymodel.handling;
-import java.util.Set;
-import java.util.TreeSet;
-
+import java.util.Map;
+import java.util.Map.Entry;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
+import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
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.AxContextSchema;
+import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
+import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
+import org.onap.policy.apex.model.policymodel.concepts.AxTask;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
@@ -42,7 +48,8 @@ public final class PolicyModelMerger {
/**
* Private constructor used to prevent sub class instantiation.
*/
- private PolicyModelMerger() {}
+ private PolicyModelMerger() {
+ }
/**
* Get a merged policy model with information from two policy models merged into a larger policy
@@ -52,12 +59,14 @@ public final class PolicyModelMerger {
* @param rightPolicyModel the policies to include in sub policy model
* @param useLeftOnMatches if true, uses concepts from the left model if concepts with common
* keys are found, if false it uses the concepts from the right model
+ * @param failOnDuplicateKeys whether to fail or not on the occurence of duplicate concept keys
* @return the new Destination Model
* @throws ApexModelException on model transfer errors
*/
public static AxPolicyModel getMergedPolicyModel(final AxPolicyModel leftPolicyModel,
- final AxPolicyModel rightPolicyModel, final boolean useLeftOnMatches) throws ApexModelException {
- return getMergedPolicyModel(leftPolicyModel, rightPolicyModel, useLeftOnMatches, false);
+ final AxPolicyModel rightPolicyModel, final boolean useLeftOnMatches, final boolean failOnDuplicateKeys)
+ throws ApexModelException {
+ return getMergedPolicyModel(leftPolicyModel, rightPolicyModel, useLeftOnMatches, false, failOnDuplicateKeys);
}
/**
@@ -69,89 +78,105 @@ public final class PolicyModelMerger {
* @param useLeftOnMatches if true, uses concepts from the left model if concepts with common
* keys are found, if false it uses the concepts from the right model
* @param ignoreInvalidSource Ignore errors on the source model, do the best you can
+ * @param failOnDuplicateKeys whether to fail or not on the occurence of duplicate concept keys
* @return the new Destination Model
* @throws ApexModelException on model transfer errors
*/
public static AxPolicyModel getMergedPolicyModel(final AxPolicyModel leftPolicyModel,
- final AxPolicyModel rightPolicyModel, final boolean useLeftOnMatches, final boolean ignoreInvalidSource)
- throws ApexModelException {
- // Validate the left model
- if (!ignoreInvalidSource) {
- final AxValidationResult leftValidationResult = new AxValidationResult();
- leftPolicyModel.validate(leftValidationResult);
- if (!leftValidationResult.isValid()) {
- String message = "left model is invalid: " + leftValidationResult.toString();
- LOGGER.warn(message);
- throw new ApexModelException(message);
- }
- }
+ final AxPolicyModel rightPolicyModel, final boolean useLeftOnMatches, final boolean ignoreInvalidSource,
+ final boolean failOnDuplicateKeys) throws ApexModelException {
- // Validate the right model
if (!ignoreInvalidSource) {
- final AxValidationResult rightValidationResult = new AxValidationResult();
- rightPolicyModel.validate(rightValidationResult);
- if (!rightValidationResult.isValid()) {
- String message = "right model is invalid: " + rightValidationResult.toString();
- LOGGER.warn(message);
- throw new ApexModelException(message);
- }
+ validateModels(leftPolicyModel, "left");
+ validateModels(rightPolicyModel, "right");
}
// The new policy model uses the favoured copy side as its base
- final AxPolicyModel mergedPolicyModel = (useLeftOnMatches ? new AxPolicyModel(leftPolicyModel)
- : new AxPolicyModel(rightPolicyModel));
+ final AxPolicyModel mergedPolicyModel =
+ (useLeftOnMatches ? new AxPolicyModel(leftPolicyModel) : new AxPolicyModel(rightPolicyModel));
// The Compared to policy model is the unfavoured side
- final AxPolicyModel copyFromPolicyModel = (useLeftOnMatches ? rightPolicyModel : leftPolicyModel);
-
- //  Get the keys to copy over
- final Set<AxArtifactKey> copyOverKeyInfoKeys =
- new TreeSet<>(copyFromPolicyModel.getKeyInformation().getKeyInfoMap().keySet());
- final Set<AxArtifactKey> copyOverContextSchemaKeys =
- new TreeSet<>(copyFromPolicyModel.getSchemas().getSchemasMap().keySet());
- final Set<AxArtifactKey> copyOverEventKeys =
- new TreeSet<>(copyFromPolicyModel.getEvents().getEventMap().keySet());
- final Set<AxArtifactKey> copyOverContextAlbumKeys =
- new TreeSet<>(copyFromPolicyModel.getAlbums().getAlbumsMap().keySet());
- final Set<AxArtifactKey> copyOverTaskKeys = new TreeSet<>(copyFromPolicyModel.getTasks().getTaskMap().keySet());
- final Set<AxArtifactKey> copyOverPolicyKeys =
- new TreeSet<>(copyFromPolicyModel.getPolicies().getPolicyMap().keySet());
-
- //  Remove keys that already exist
- copyOverKeyInfoKeys.removeAll(mergedPolicyModel.getKeyInformation().getKeyInfoMap().keySet());
- copyOverContextSchemaKeys.removeAll(mergedPolicyModel.getSchemas().getSchemasMap().keySet());
- copyOverEventKeys.removeAll(mergedPolicyModel.getEvents().getEventMap().keySet());
- copyOverContextAlbumKeys.removeAll(mergedPolicyModel.getAlbums().getAlbumsMap().keySet());
- copyOverTaskKeys.removeAll(mergedPolicyModel.getTasks().getTaskMap().keySet());
- copyOverPolicyKeys.removeAll(mergedPolicyModel.getPolicies().getPolicyMap().keySet());
+ final AxPolicyModel copyFromPolicyModel =
+ (useLeftOnMatches ? new AxPolicyModel(rightPolicyModel) : new AxPolicyModel(leftPolicyModel));
- // Now add all the concepts that must be copied over
- for (final AxArtifactKey keyInfoKey : copyOverKeyInfoKeys) {
- mergedPolicyModel.getKeyInformation().getKeyInfoMap().put(keyInfoKey,
- copyFromPolicyModel.getKeyInformation().getKeyInfoMap().get(keyInfoKey));
- }
- for (final AxArtifactKey contextSchemaKey : copyOverContextSchemaKeys) {
- mergedPolicyModel.getSchemas().getSchemasMap().put(contextSchemaKey,
- copyFromPolicyModel.getSchemas().getSchemasMap().get(contextSchemaKey));
- }
- for (final AxArtifactKey eventKey : copyOverEventKeys) {
- mergedPolicyModel.getEvents().getEventMap().put(eventKey,
- copyFromPolicyModel.getEvents().getEventMap().get(eventKey));
- }
- for (final AxArtifactKey contextAlbumKey : copyOverContextAlbumKeys) {
- mergedPolicyModel.getAlbums().getAlbumsMap().put(contextAlbumKey,
- copyFromPolicyModel.getAlbums().getAlbumsMap().get(contextAlbumKey));
- }
- for (final AxArtifactKey taskKey : copyOverTaskKeys) {
- mergedPolicyModel.getTasks().getTaskMap().put(taskKey,
- copyFromPolicyModel.getTasks().getTaskMap().get(taskKey));
- }
- for (final AxArtifactKey policyKey : copyOverPolicyKeys) {
- mergedPolicyModel.getPolicies().getPolicyMap().put(policyKey,
- copyFromPolicyModel.getPolicies().getPolicyMap().get(policyKey));
+ Map<AxArtifactKey, AxKeyInfo> mergedKeyInfoMap = mergedPolicyModel.getKeyInformation().getKeyInfoMap();
+ Map<AxArtifactKey, AxContextSchema> mergedSchemasMap = mergedPolicyModel.getSchemas().getSchemasMap();
+ Map<AxArtifactKey, AxEvent> mergedEventMap = mergedPolicyModel.getEvents().getEventMap();
+ Map<AxArtifactKey, AxContextAlbum> mergedAlbumsMap = mergedPolicyModel.getAlbums().getAlbumsMap();
+ Map<AxArtifactKey, AxTask> mergedTaskMap = mergedPolicyModel.getTasks().getTaskMap();
+ Map<AxArtifactKey, AxPolicy> mergedPolicyMap = mergedPolicyModel.getPolicies().getPolicyMap();
+
+ Map<AxArtifactKey, AxKeyInfo> copyOverKeyInfoMap = copyFromPolicyModel.getKeyInformation().getKeyInfoMap();
+ Map<AxArtifactKey, AxContextSchema> copyOverSchemasMap = copyFromPolicyModel.getSchemas().getSchemasMap();
+ Map<AxArtifactKey, AxEvent> copyOverEventMap = copyFromPolicyModel.getEvents().getEventMap();
+ Map<AxArtifactKey, AxContextAlbum> copyOverAlbumsMap = copyFromPolicyModel.getAlbums().getAlbumsMap();
+ Map<AxArtifactKey, AxTask> copyOverTaskMap = copyFromPolicyModel.getTasks().getTaskMap();
+ Map<AxArtifactKey, AxPolicy> copyOverPolicyMap = copyFromPolicyModel.getPolicies().getPolicyMap();
+
+ if (failOnDuplicateKeys) {
+ StringBuilder errorMessage = new StringBuilder();
+ checkForDuplicateContextItem(mergedSchemasMap, copyOverSchemasMap, errorMessage, "schema");
+ checkForDuplicateItem(mergedEventMap, copyOverEventMap, errorMessage, "event");
+ checkForDuplicateContextItem(mergedAlbumsMap, copyOverAlbumsMap, errorMessage, "album");
+ checkForDuplicateItem(mergedTaskMap, copyOverTaskMap, errorMessage, "task");
+ checkForDuplicateItem(mergedPolicyMap, copyOverPolicyMap, errorMessage, "policy");
+ if (errorMessage.length() > 0) {
+ throw new ApexModelException(errorMessage.toString());
+ }
+ } else {
+ //  Remove entries that already exist
+ copyOverKeyInfoMap.keySet().removeIf(mergedKeyInfoMap::containsKey);
+ copyOverSchemasMap.keySet().removeIf(mergedSchemasMap::containsKey);
+ copyOverEventMap.keySet().removeIf(mergedEventMap::containsKey);
+ copyOverAlbumsMap.keySet().removeIf(mergedAlbumsMap::containsKey);
+ copyOverTaskMap.keySet().removeIf(mergedTaskMap::containsKey);
+ copyOverPolicyMap.keySet().removeIf(mergedPolicyMap::containsKey);
}
+ // Now add all the concepts that must be copied over
+ mergedKeyInfoMap.putAll(copyOverKeyInfoMap);
+ mergedSchemasMap.putAll(copyOverSchemasMap);
+ mergedEventMap.putAll(copyOverEventMap);
+ mergedAlbumsMap.putAll(copyOverAlbumsMap);
+ mergedTaskMap.putAll(copyOverTaskMap);
+ mergedPolicyMap.putAll(copyOverPolicyMap);
// That's it, return the model
return mergedPolicyModel;
}
+
+ private static <V> void checkForDuplicateItem(Map<AxArtifactKey, V> mergedItemsMap,
+ Map<AxArtifactKey, V> copyOverItemsMap, StringBuilder errorMessage, String itemType) {
+ for (AxArtifactKey key : copyOverItemsMap.keySet()) {
+ if (mergedItemsMap.containsKey(key)) {
+ errorMessage.append("\n Duplicate " + itemType + " found - ").append(key.getId());
+ }
+ }
+ }
+
+ private static <V> void checkForDuplicateContextItem(Map<AxArtifactKey, V> mergedItemsMap,
+ Map<AxArtifactKey, V> copyOverItemsMap, StringBuilder errorMessage, String itemType) {
+ for (Entry<AxArtifactKey, V> entry : copyOverItemsMap.entrySet()) {
+ V item = mergedItemsMap.get(entry.getKey());
+ // same context schema name with different definitions cannot occur in multiple policies
+ if (null != item) {
+ if (item.equals(entry.getValue())) {
+ LOGGER.info("Same {} - {} is used by multiple policies.", itemType, entry.getKey().getId());
+ } else {
+ errorMessage.append("\n Same " + itemType + " - ").append(entry.getKey().getId())
+ .append(" with different definitions used in different policies");
+ }
+ }
+ }
+ }
+
+ private static void validateModels(AxPolicyModel policyModel, String position) throws ApexModelException {
+ // Validate the model
+ final AxValidationResult validationResult = new AxValidationResult();
+ policyModel.validate(validationResult);
+ if (!validationResult.isValid()) {
+ String message = position + " model is invalid: " + validationResult.toString();
+ LOGGER.warn(message);
+ throw new ApexModelException(message);
+ }
+ }
}
diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java
index b477c47cd..560ddc6a9 100644
--- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java
+++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java
@@ -1,111 +1,86 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- *
+ *
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.model.policymodel.handling;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
-import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger;
/**
* Test model merging.
- *
+ *
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public class PolicyModelMergerTest {
@Test
- public void testPolicyModelMerger() {
+ public void testPolicyModelMerger() throws ApexModelException {
final AxPolicyModel leftPolicyModel = new SupportApexPolicyModelCreator().getModel();
- AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel();
+ final AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel();
- try {
- final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
- rightPolicyModel, false);
- assertEquals(leftPolicyModel, mergedPolicyModel);
- assertEquals(rightPolicyModel, mergedPolicyModel);
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ AxPolicyModel mergedPolicyModel =
+ PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
+ assertEquals(leftPolicyModel, mergedPolicyModel);
+ assertEquals(rightPolicyModel, mergedPolicyModel);
leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
- try {
- PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
- fail("test should throw an exception here");
- } catch (final ApexModelException e) {
- assertEquals("left model is invalid: \n***validation of model fai", e.getMessage().substring(0, 50));
- }
+ assertThatThrownBy(
+ () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
+ .hasMessageContaining("left model is invalid: \n***validation of model failed");
leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
- try {
- assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel);
- try {
- final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
- rightPolicyModel, true);
- assertNotNull(mergedPolicyModel);
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true, false);
+ assertNotNull(mergedPolicyModel);
rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
- try {
- PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
- fail("test should throw an exception here");
- } catch (final ApexModelException e) {
- assertEquals("right model is invalid: \n***validation of model fa", e.getMessage().substring(0, 50));
- }
+ assertThatThrownBy(
+ () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
+ .hasMessageContaining("right model is invalid: \n***validation of model failed");
rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
- try {
- assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel);
- try {
- final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
- rightPolicyModel, false);
- assertNotNull(mergedPolicyModel);
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
+ assertNotNull(mergedPolicyModel);
- rightPolicyModel = new SupportApexPolicyModelCreator().getAnotherModel();
- try {
- final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
- rightPolicyModel, true);
- assertNotNull(mergedPolicyModel);
- } catch (final ApexModelException e) {
- fail("test should not throw an exception");
- }
+ final AxPolicyModel rightPolicyModel2 = new SupportApexPolicyModelCreator().getAnotherModel();
+ mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, false);
+ assertNotNull(mergedPolicyModel);
+
+ mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, true);
+ assertNotNull(mergedPolicyModel);
+
+ final AxPolicyModel rightPolicyModel3 = new SupportApexPolicyModelCreator().getModel();
+ assertThatThrownBy(
+ () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel3, true, true))
+ .hasMessageContaining("Duplicate policy found");
}
}