Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / main / java / org / onap / policy / apex / model / policymodel / handling / PolicyModelSplitter.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.policymodel.handling;
22
23 import java.util.Collection;
24 import java.util.Set;
25 import java.util.TreeSet;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
28 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
29 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
30 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * Helper class used to extract information from a policy model into a policy model that is a subset of the original
36  * policy model.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public final class PolicyModelSplitter {
41     private static final XLogger LOGGER = XLoggerFactory.getXLogger(PolicyModelSplitter.class);
42
43     /**
44      * Private constructor used to prevent sub class instantiation.
45      */
46     private PolicyModelSplitter() {
47         // Private constructor to block subclassing
48     }
49
50     /**
51      * Get a sub policy model with only the information required for the specified policies from a larger policy model.
52      *
53      * @param sourcePolicyModel the source Apex Model
54      * @param subPolicies the policies to include in sub policy model
55      * @return the new Destination Model
56      * @throws ApexModelException on model transfer errors
57      */
58     public static AxPolicyModel getSubPolicyModel(final AxPolicyModel sourcePolicyModel,
59             final Collection<AxArtifactKey> subPolicies) throws ApexModelException {
60         return getSubPolicyModel(sourcePolicyModel, subPolicies, false);
61     }
62
63     /**
64      * Get a sub policy model with only the information required for the specified policies from a larger policy model.
65      *
66      * @param sourcePolicyModel the source Apex Model
67      * @param subPolicies the policies to include in sub policy model
68      * @param ignoreInvalidSource Ignore errors on the source model, do the best you can
69      * @return the new Destination Model
70      * @throws ApexModelException on model transfer errors
71      */
72     public static AxPolicyModel getSubPolicyModel(final AxPolicyModel sourcePolicyModel,
73             final Collection<AxArtifactKey> subPolicies, final boolean ignoreInvalidSource) throws ApexModelException {
74         // Validate the source model
75         if (!ignoreInvalidSource) {
76             final AxValidationResult sourceValidationResult = new AxValidationResult();
77             sourcePolicyModel.validate(sourceValidationResult);
78             if (!sourceValidationResult.isValid()) {
79                 String message = "source model is invalid: " + sourceValidationResult.toString();
80                 LOGGER.warn(message);
81                 throw new ApexModelException(message);
82             }
83         }
84
85         // The new policy model
86         final AxPolicyModel newPolicyModel = new AxPolicyModel(sourcePolicyModel.getKey());
87         newPolicyModel.getKeyInformation().setKey(sourcePolicyModel.getKeyInformation().getKey());
88         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getKey(),
89                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getKey()));
90         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getKeyInformation().getKey(),
91                 sourcePolicyModel.getKeyInformation().getKeyInfoMap()
92                         .get(sourcePolicyModel.getKeyInformation().getKey()));
93
94         //  Get the events, tasks, context maps, and data types used by each policy
95         final Set<AxArtifactKey> contextSchemaSet = new TreeSet<>();
96         final Set<AxArtifactKey> eventSet = new TreeSet<>();
97         final Set<AxArtifactKey> contextAlbumSet = new TreeSet<>();
98         final Set<AxArtifactKey> taskSet = new TreeSet<>();
99
100         newPolicyModel.getPolicies().setKey(sourcePolicyModel.getPolicies().getKey());
101         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getPolicies().getKey(),
102                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getPolicies().getKey()));
103         for (final AxArtifactKey subPolicyKey : subPolicies) {
104             final AxPolicy subPolicy = sourcePolicyModel.getPolicies().getPolicyMap().get(subPolicyKey);
105             if (subPolicy == null) {
106                 LOGGER.warn("source sub policy not found: {}", subPolicyKey);
107                 continue;
108             }
109
110             // Transfer the policy across
111             newPolicyModel.getPolicies().getPolicyMap().put(subPolicyKey, subPolicy);
112             newPolicyModel.getKeyInformation().getKeyInfoMap().put(subPolicyKey,
113                     sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(subPolicyKey));
114
115             // Get the references for this policy
116             final PolicyAnalysisResult analysisResult = new PolicyAnalyser().analyse(sourcePolicyModel, subPolicy);
117             contextSchemaSet.addAll(analysisResult.getUsedContextSchemas());
118             eventSet.addAll(analysisResult.getUsedEvents());
119             contextAlbumSet.addAll(analysisResult.getUsedContextAlbums());
120             taskSet.addAll(analysisResult.getUsedTasks());
121
122         }
123
124         // Now add all the referenced data types, events, context maps, and tasks to the policy
125         // model
126         newPolicyModel.getSchemas().setKey(sourcePolicyModel.getSchemas().getKey());
127         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getSchemas().getKey(),
128                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getSchemas().getKey()));
129         for (final AxArtifactKey contextSchemaKey : contextSchemaSet) {
130             newPolicyModel.getSchemas().getSchemasMap().put(contextSchemaKey,
131                     sourcePolicyModel.getSchemas().getSchemasMap().get(contextSchemaKey));
132             newPolicyModel.getKeyInformation().getKeyInfoMap().put(contextSchemaKey,
133                     sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(contextSchemaKey));
134         }
135         newPolicyModel.getEvents().setKey(sourcePolicyModel.getEvents().getKey());
136         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getEvents().getKey(),
137                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getEvents().getKey()));
138         for (final AxArtifactKey eventKey : eventSet) {
139             newPolicyModel.getEvents().getEventMap().put(eventKey,
140                     sourcePolicyModel.getEvents().getEventMap().get(eventKey));
141             newPolicyModel.getKeyInformation().getKeyInfoMap().put(eventKey,
142                     sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(eventKey));
143         }
144         newPolicyModel.getAlbums().setKey(sourcePolicyModel.getAlbums().getKey());
145         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getAlbums().getKey(),
146                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getAlbums().getKey()));
147         for (final AxArtifactKey contextAlbumKey : contextAlbumSet) {
148             newPolicyModel.getAlbums().getAlbumsMap().put(contextAlbumKey,
149                     sourcePolicyModel.getAlbums().getAlbumsMap().get(contextAlbumKey));
150             newPolicyModel.getKeyInformation().getKeyInfoMap().put(contextAlbumKey,
151                     sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(contextAlbumKey));
152         }
153         newPolicyModel.getTasks().setKey(sourcePolicyModel.getTasks().getKey());
154         newPolicyModel.getKeyInformation().getKeyInfoMap().put(sourcePolicyModel.getTasks().getKey(),
155                 sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(sourcePolicyModel.getTasks().getKey()));
156         for (final AxArtifactKey taskKey : taskSet) {
157             newPolicyModel.getTasks().getTaskMap().put(taskKey, sourcePolicyModel.getTasks().getTaskMap().get(taskKey));
158             newPolicyModel.getKeyInformation().getKeyInfoMap().put(taskKey,
159                     sourcePolicyModel.getKeyInformation().getKeyInfoMap().get(taskKey));
160         }
161
162         // That's it, return the model
163         return newPolicyModel;
164     }
165 }