2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.policymodel.handling;
23 import java.util.Collection;
25 import java.util.TreeSet;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
29 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
30 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
31 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * Helper class used to extract information from a policy model into a policy model that is a subset of the original
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public final class PolicyModelSplitter {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
45 * Private constructor used to prevent sub class instantiation.
47 private PolicyModelSplitter() {
48 // Private constructor to block subclassing
52 * Get a sub policy model with only the information required for the specified policies from a larger policy model.
54 * @param sourcePolicyModel the source Apex Model
55 * @param subPolicies the policies to include in sub policy model
56 * @return the new Destination Model
57 * @throws ApexModelException on model transfer errors
59 public static AxPolicyModel getSubPolicyModel(final AxPolicyModel sourcePolicyModel,
60 final Collection<AxArtifactKey> subPolicies) throws ApexModelException {
61 return getSubPolicyModel(sourcePolicyModel, subPolicies, false);
65 * Get a sub policy model with only the information required for the specified policies from a larger policy model.
67 * @param sourcePolicyModel the source Apex Model
68 * @param subPolicies the policies to include in sub policy model
69 * @param ignoreInvalidSource Ignore errors on the source model, do the best you can
70 * @return the new Destination Model
71 * @throws ApexModelException on model transfer errors
73 public static AxPolicyModel getSubPolicyModel(final AxPolicyModel sourcePolicyModel,
74 final Collection<AxArtifactKey> subPolicies, final boolean ignoreInvalidSource) throws ApexModelException {
75 // Validate the source model
76 if (!ignoreInvalidSource) {
77 final AxValidationResult sourceValidationResult = new AxValidationResult();
78 sourcePolicyModel.validate(sourceValidationResult);
79 if (!sourceValidationResult.isValid()) {
80 String message = "source model is invalid: " + sourceValidationResult.toString();
82 throw new ApexModelException(message);
86 // The new policy model
87 final AxPolicyModel newPolicyModel = new AxPolicyModel(sourcePolicyModel.getKey());
88 newPolicyModel.getKeyInformation().setKey(sourcePolicyModel.getKeyInformation().getKey());
89 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getKey(),
90 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getKey()));
91 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getKeyInformation().getKey(),
92 sourcePolicyModel.getKeyInformation().getKeyInfoMap()
93 .get(sourcePolicyModel.getKeyInformation().getKey()));
95 // Get the events, tasks, context maps, and data types used by each policy
96 final Set<AxArtifactKey> contextSchemaSet = new TreeSet<>();
97 final Set<AxArtifactKey> eventSet = new TreeSet<>();
98 final Set<AxArtifactKey> contextAlbumSet = new TreeSet<>();
99 final Set<AxArtifactKey> taskSet = new TreeSet<>();
101 newPolicyModel.getPolicies().setKey(sourcePolicyModel.getPolicies().getKey());
102 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getPolicies().getKey(),
103 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getPolicies().getKey()));
104 for (final AxArtifactKey subPolicyKey : subPolicies) {
105 final AxPolicy subPolicy = sourcePolicyModel.getPolicies().getPolicyMap().get(subPolicyKey);
106 if (subPolicy == null) {
107 LOGGER.warn("source sub policy not found: {}", subPolicyKey);
111 // Transfer the policy across
112 newPolicyModel.getPolicies().getPolicyMap().put(subPolicyKey, subPolicy);
113 newPolicyModel.getKeyInformation().getKeyInfoMap().put(subPolicyKey,
114 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(subPolicyKey));
116 // Get the references for this policy
117 final PolicyAnalysisResult analysisResult = new PolicyAnalyser().analyse(sourcePolicyModel, subPolicy);
118 contextSchemaSet.addAll(analysisResult.getUsedContextSchemas());
119 eventSet.addAll(analysisResult.getUsedEvents());
120 contextAlbumSet.addAll(analysisResult.getUsedContextAlbums());
121 taskSet.addAll(analysisResult.getUsedTasks());
125 // Now add all the referenced data types, events, context maps, and tasks to the policy
127 newPolicyModel.getSchemas().setKey(sourcePolicyModel.getSchemas().getKey());
128 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getSchemas().getKey(),
129 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getSchemas().getKey()));
130 for (final AxArtifactKey contextSchemaKey : contextSchemaSet) {
131 newPolicyModel.getSchemas().getSchemasMap().put(contextSchemaKey,
132 sourcePolicyModel.getSchemas().getSchemasMap().get(contextSchemaKey));
133 newPolicyModel.getKeyInformation().getKeyInfoMap().put(contextSchemaKey,
134 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(contextSchemaKey));
136 newPolicyModel.getEvents().setKey(sourcePolicyModel.getEvents().getKey());
137 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getEvents().getKey(),
138 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getEvents().getKey()));
139 for (final AxArtifactKey eventKey : eventSet) {
140 newPolicyModel.getEvents().getEventMap().put(eventKey,
141 sourcePolicyModel.getEvents().getEventMap().get(eventKey));
142 newPolicyModel.getKeyInformation().getKeyInfoMap().put(eventKey,
143 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(eventKey));
145 newPolicyModel.getAlbums().setKey(sourcePolicyModel.getAlbums().getKey());
146 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getAlbums().getKey(),
147 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getAlbums().getKey()));
148 for (final AxArtifactKey contextAlbumKey : contextAlbumSet) {
149 newPolicyModel.getAlbums().getAlbumsMap().put(contextAlbumKey,
150 sourcePolicyModel.getAlbums().getAlbumsMap().get(contextAlbumKey));
151 newPolicyModel.getKeyInformation().getKeyInfoMap().put(contextAlbumKey,
152 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(contextAlbumKey));
154 newPolicyModel.getTasks().setKey(sourcePolicyModel.getTasks().getKey());
155 newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getTasks().getKey(),
156 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getTasks().getKey()));
157 for (final AxArtifactKey taskKey : taskSet) {
158 newPolicyModel.getTasks().getTaskMap().put(taskKey, sourcePolicyModel.getTasks().getTaskMap().get(taskKey));
159 newPolicyModel.getKeyInformation().getKeyInfoMap().put(taskKey,
160 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(taskKey));
163 // That's it, return the model
164 return newPolicyModel;