Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / ModelHandlerFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.modelapi.impl;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.net.URLConnection;
31 import java.nio.file.Files;
32 import java.util.LinkedHashSet;
33 import java.util.List;
34 import java.util.Properties;
35 import java.util.Set;
36 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
40 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
41 import org.onap.policy.apex.model.basicmodel.dao.ApexDaoFactory;
42 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
43 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
44 import org.onap.policy.apex.model.basicmodel.handling.ApexModelFileWriter;
45 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
46 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
47 import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
48 import org.onap.policy.apex.model.modelapi.ApexApiResult;
49 import org.onap.policy.apex.model.modelapi.ApexModel;
50 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
51 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
52 import org.onap.policy.apex.model.policymodel.handling.PolicyAnalyser;
53 import org.onap.policy.apex.model.policymodel.handling.PolicyAnalysisResult;
54 import org.onap.policy.apex.model.policymodel.handling.PolicyModelComparer;
55 import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger;
56 import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter;
57 import org.onap.policy.common.utils.resources.ResourceUtils;
58 import org.onap.policy.common.utils.resources.TextFileUtils;
59 import org.onap.policy.common.utils.validation.Assertions;
60 import org.slf4j.ext.XLogger;
61 import org.slf4j.ext.XLoggerFactory;
62
63 /**
64  * This class acts as a facade for model handling for the Apex Model API.
65  *
66  * @author Liam Fallon (liam.fallon@ericsson.com)
67  */
68 public class ModelHandlerFacade {
69     private static final String FOUND_IN_DATABASE = " found in database";
70     private static final String FILE_NAME_MAY_NOT_BE_NULL = "fileName may not be null";
71     private static final String MODEL = "model ";
72     private static final String ALREADY_LOADED = " already loaded";
73
74     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ModelHandlerFacade.class);
75
76     // Apex model we're working towards
77     private final ApexModel apexModel;
78
79     // JSON output on list/delete if set
80     private final boolean jsonMode;
81
82     /**
83      * This Constructor creates a model handling facade for the given {@link ApexModel}.
84      *
85      * @param apexModel the apex model to manipulate
86      * @param apexProperties properties for the model
87      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise set to false
88      */
89     public ModelHandlerFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
90         Assertions.argumentNotNull(apexModel, "apexModel may not be null");
91         Assertions.argumentNotNull(apexProperties, "apexProperties may not be null");
92
93         this.apexModel = apexModel;
94         this.jsonMode = jsonMode;
95     }
96
97     /**
98      * Load an Apex model from a string.
99      *
100      * @param modelString the string with the model
101      * @return the result of the operation
102      */
103     public ApexApiResult loadFromString(final String modelString) {
104         Assertions.argumentNotNull(modelString, "modelString may not be null");
105
106         if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
107             return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
108                     MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
109         }
110
111         ApexApiResult result = new ApexApiResult();
112         AxPolicyModel newPolicyModel = loadModelFromString(modelString, result);
113         apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());
114
115         return result;
116     }
117
118     /**
119      * Load an Apex model from a file.
120      *
121      * @param fileName the file name of the file with the model
122      * @return the result of the operation
123      */
124     public ApexApiResult loadFromFile(final String fileName) {
125         Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);
126
127         if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
128             return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
129                     MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
130         }
131
132         ApexApiResult result = new ApexApiResult();
133         AxPolicyModel newPolicyModel = loadModelFromFile(fileName, result);
134         apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());
135
136         return result;
137     }
138
139     /**
140      * Save an Apex model to a file.
141      *
142      * @param fileName the file name
143      * @param xmlFlag if true, save the file in XML format, otherwise save the file in the default JSON format
144      * @return the result of the operation
145      */
146     public ApexApiResult saveToFile(final String fileName, final boolean xmlFlag) {
147         Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);
148
149         ApexModelFileWriter<AxPolicyModel> apexModelFileWriter = new ApexModelFileWriter<>(false);
150
151         try {
152             if (xmlFlag) {
153                 apexModelFileWriter.apexModelWriteXmlFile(apexModel.getPolicyModel(), AxPolicyModel.class, fileName);
154             } else {
155                 apexModelFileWriter.apexModelWriteJsonFile(apexModel.getPolicyModel(), AxPolicyModel.class, fileName);
156             }
157             return new ApexApiResult();
158         } catch (ApexException e) {
159             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
160         }
161     }
162
163     /**
164      * Load an Apex model from a database.
165      *
166      * @param modelName the name of the model to load
167      * @param modelVersion the version of the model to load, loads the policy model from the database with this name, if
168      *        more than one exist, an exception is thrown
169      * @param daoParameters the parameters to use to access the database over JDBC
170      * @return the result of the operation
171      */
172     public ApexApiResult loadFromDatabase(final String modelName, final String modelVersion,
173             final DaoParameters daoParameters) {
174         Assertions.argumentNotNull(modelName, "modelName may not be null");
175         Assertions.argumentNotNull(daoParameters, "DaoParameters may not be null");
176
177         if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
178             return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
179                     MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
180         }
181
182         ApexDao apexDao = null;
183         try {
184             apexDao = new ApexDaoFactory().createApexDao(daoParameters);
185             apexDao.init(daoParameters);
186
187             // Single specific model requested
188             if (modelVersion != null) {
189                 AxPolicyModel daoPolicyModel =
190                         apexDao.get(AxPolicyModel.class, new AxArtifactKey(modelName, modelVersion));
191
192                 if (daoPolicyModel != null) {
193                     apexModel.setPolicyModel(daoPolicyModel);
194                     return new ApexApiResult();
195                 } else {
196                     apexModel.setPolicyModel(new AxPolicyModel());
197                     return new ApexApiResult(ApexApiResult.Result.FAILED, "no policy model with name " + modelName
198                             + " and version " + modelVersion + FOUND_IN_DATABASE);
199                 }
200             } else {
201                 // Fishing expedition
202                 return searchInDatabase(modelName, apexDao, apexModel);
203             }
204         } catch (ApexException | ApexRuntimeException e) {
205             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
206         } finally {
207             if (apexDao != null) {
208                 apexDao.close();
209             }
210         }
211     }
212
213     /**
214      * Search for an Apex model in the database.
215      *
216      * @param modelName the name of the model to load
217      * @param apexDao the DAO to use to find the model
218      * @param apexModel the APEX model we are loading the found model into
219      * @return the result of the operation
220      */
221     private ApexApiResult searchInDatabase(String modelName, ApexDao apexDao, ApexModel apexModel) {
222         AxPolicyModel foundPolicyModel = null;
223
224         List<AxPolicyModel> policyModelList = apexDao.getAll(AxPolicyModel.class);
225         for (AxPolicyModel dbPolicyModel : policyModelList) {
226             if (dbPolicyModel.getKey().getName().equals(modelName)) {
227                 if (foundPolicyModel == null) {
228                     foundPolicyModel = dbPolicyModel;
229                 } else {
230                     return new ApexApiResult(ApexApiResult.Result.FAILED,
231                             "more than one policy model with name " + modelName + FOUND_IN_DATABASE);
232                 }
233             }
234         }
235
236         if (foundPolicyModel != null) {
237             apexModel.setPolicyModel(foundPolicyModel);
238             return new ApexApiResult();
239         } else {
240             apexModel.setPolicyModel(new AxPolicyModel());
241             return new ApexApiResult(ApexApiResult.Result.FAILED,
242                     "no policy model with name " + modelName + FOUND_IN_DATABASE);
243         }
244     }
245
246     /**
247      * Save an Apex model to a database.
248      *
249      * @param daoParameters the parameters to use to access the database over JDBC
250      * @return the result of the operation
251      */
252     public ApexApiResult saveToDatabase(final DaoParameters daoParameters) {
253         ApexDao apexDao = null;
254
255         try {
256             apexDao = new ApexDaoFactory().createApexDao(daoParameters);
257             apexDao.init(daoParameters);
258
259             apexDao.create(apexModel.getPolicyModel());
260             return new ApexApiResult();
261         } catch (ApexException e) {
262             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
263         } finally {
264             if (apexDao != null) {
265                 apexDao.close();
266             }
267         }
268     }
269
270     /**
271      * Read an APEX model from a location identified by a URL.
272      *
273      * @param urlString the url string
274      * @return the result of the operation
275      */
276     public ApexApiResult readFromUrl(final String urlString) {
277         Assertions.argumentNotNull(urlString, "urlString may not be null");
278
279         if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
280             return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
281                     MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
282         }
283
284         URL apexModelUrl;
285         try {
286             apexModelUrl = new URL(urlString);
287         } catch (MalformedURLException e) {
288             ApexApiResult result = new ApexApiResult(ApexApiResult.Result.FAILED);
289             result.addMessage("URL string " + urlString + " is not a valid URL");
290             result.addThrowable(e);
291             return result;
292         }
293
294         try {
295             ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
296             apexModelReader.setValidateFlag(false);
297             AxPolicyModel newPolicyModel = apexModelReader.read(apexModelUrl.openStream());
298             apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());
299             return new ApexApiResult();
300         } catch (ApexModelException | IOException e) {
301             apexModel.setPolicyModel(new AxPolicyModel());
302             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
303         }
304     }
305
306     /**
307      * Write an APEX model to a location identified by a URL.
308      *
309      * @param urlString the URL to read the model from
310      * @param xmlFlag if true, save the file in XML format, otherwise save the file in the default JSON format
311      * @return the result of the operation
312      */
313     public ApexApiResult writeToUrl(final String urlString, final boolean xmlFlag) {
314         Assertions.argumentNotNull(urlString, "urlString may not be null");
315
316         URL apexModelUrl;
317         try {
318             apexModelUrl = new URL(urlString);
319         } catch (MalformedURLException e) {
320             ApexApiResult result = new ApexApiResult(ApexApiResult.Result.FAILED);
321             result.addMessage("URL string " + urlString + " is not a valid URL");
322             result.addThrowable(e);
323             return result;
324         }
325
326         try {
327             ApexModelWriter<AxPolicyModel> apexModelWriter = new ApexModelWriter<>(AxPolicyModel.class);
328             apexModelWriter.setValidateFlag(false);
329             apexModelWriter.setJsonOutput(!xmlFlag);
330
331             // Open the URL for output and write the model
332             URLConnection urlConnection = apexModelUrl.openConnection();
333             urlConnection.setDoOutput(true);
334
335             apexModelWriter.write(apexModel.getPolicyModel(), urlConnection.getOutputStream());
336             return new ApexApiResult();
337         } catch (ApexModelException | IOException e) {
338             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
339         }
340     }
341
342     /**
343      * Analyse an Apex model that shows the concept usage references of a policy model.
344      *
345      * @return the result of the operation
346      */
347     public ApexApiResult analyse() {
348         PolicyAnalysisResult analysisResult = new PolicyAnalyser().analyse(apexModel.getPolicyModel());
349         return new ApexApiResult(ApexApiResult.Result.SUCCESS, analysisResult.toString());
350     }
351
352     /**
353      * Validate an Apex model, checking all concepts and references in the model.
354      *
355      * @return the result of the operation
356      */
357     public ApexApiResult validate() {
358         ApexApiResult result = new ApexApiResult();
359         try {
360             AxValidationResult validationResult = apexModel.getPolicyModel().validate(new AxValidationResult());
361
362             if (!validationResult.isValid()) {
363                 result.setResult(ApexApiResult.Result.FAILED);
364             }
365             result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
366                     .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
367             result.addMessage(validationResult.toString());
368             return result;
369         } catch (Exception e) {
370             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
371         }
372     }
373
374     /**
375      * Compare to Apex models, returning the differences between the models.
376      *
377      * @param otherModelFileName the file name of the other model
378      * @param diffsOnly only returns differences between the model when set
379      * @param keysOnly only returns the keys that are different when set, when not set values are also returned
380      * @return the result of the operation
381      */
382     public ApexApiResult compare(final String otherModelFileName, final boolean diffsOnly, final boolean keysOnly) {
383         ApexApiResult result = new ApexApiResult();
384         try {
385             AxPolicyModel otherPolicyModel = loadModelFromFile(otherModelFileName, result);
386             if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
387                 return result;
388             }
389
390             PolicyModelComparer policyModelComparer =
391                     new PolicyModelComparer(apexModel.getPolicyModel(), otherPolicyModel);
392             result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
393                     .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
394             result.addMessage(policyModelComparer.toString());
395
396             return result;
397         } catch (Exception e) {
398             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
399         }
400     }
401
402     /**
403      * Compare two Apex models, returning the differences between the models.
404      *
405      * @param otherModelString the other model as a string
406      * @param diffsOnly only returns differences between the model when set
407      * @param keysOnly only returns the keys that are different when set, when not set values are also returned
408      * @return the result of the operation
409      */
410     public ApexApiResult compareWithString(final String otherModelString, final boolean diffsOnly,
411             final boolean keysOnly) {
412         ApexApiResult result = new ApexApiResult();
413         try {
414             AxPolicyModel otherPolicyModel = loadModelFromString(otherModelString, result);
415             if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
416                 return result;
417             }
418
419             PolicyModelComparer policyModelComparer =
420                     new PolicyModelComparer(apexModel.getPolicyModel(), otherPolicyModel);
421             result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
422                     .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
423             result.addMessage(policyModelComparer.toString());
424
425             return result;
426         } catch (Exception e) {
427             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
428         }
429     }
430
431     /**
432      * Split out a sub model from an Apex model that contains a given subset of the policies in the original model.
433      *
434      * @param targetModelName the file name of the target model in which to store the model split out from the original
435      *        model
436      * @param splitOutPolicies the policies form the original model to include in the split out model, specified as a
437      *        comma delimited list of policy names
438      * @return the result of the operation
439      */
440     public ApexApiResult split(final String targetModelName, final String splitOutPolicies) {
441         Set<AxArtifactKey> requiredPolicySet = new LinkedHashSet<>();
442
443         // Split the policy names on comma
444         String[] policyNames = splitOutPolicies.split(",");
445
446         // Iterate over the policy names
447         for (String policyName : policyNames) {
448             // Split out this specific policy
449             AxPolicy requiredPolicy = apexModel.getPolicyModel().getPolicies().get(policyName);
450
451             if (requiredPolicy != null) {
452                 requiredPolicySet.add(requiredPolicy.getKey());
453             } else {
454                 return new ApexApiResult(ApexApiResult.Result.FAILED,
455                         "policy for policy name " + policyName + " not found in model");
456             }
457         }
458
459         try {
460             AxPolicyModel splitPolicyModel =
461                     PolicyModelSplitter.getSubPolicyModel(apexModel.getPolicyModel(), requiredPolicySet, false);
462
463             ApexModelFileWriter<AxPolicyModel> apexModelFileWriter = new ApexModelFileWriter<>(false);
464             apexModelFileWriter.apexModelWriteJsonFile(splitPolicyModel, AxPolicyModel.class, targetModelName);
465             return new ApexApiResult();
466         } catch (ApexException e) {
467             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
468         }
469     }
470
471     /**
472      * Split out a sub model from an Apex model that contains a given subset of the policies in the original model,
473      * return the split model in the result as a string.
474      *
475      * @param splitOutPolicies the policies form the original model to include in the split out model, specified as a
476      *        comma delimited list of policy names
477      * @return the result of the operation
478      */
479     public ApexApiResult split(final String splitOutPolicies) {
480         ApexApiResult splitResult = new ApexApiResult();
481         File tempSplitPolicyFile = null;
482         try {
483             tempSplitPolicyFile = File.createTempFile("ApexTempPolicy", null);
484
485             // Split the policy into a temporary file
486             splitResult = split(tempSplitPolicyFile.getCanonicalPath(), splitOutPolicies);
487             if (splitResult.isNok()) {
488                 return splitResult;
489             }
490
491             // Get the policy model into a string
492             String splitPolicyModelString = TextFileUtils.getTextFileAsString(tempSplitPolicyFile.getCanonicalPath());
493
494             // Return the policy model
495             splitResult.addMessage(splitPolicyModelString);
496             return splitResult;
497         } catch (Exception e) {
498             return new ApexApiResult(ApexApiResult.Result.FAILED,
499                     "split of policy model " + apexModel.getPolicyModel().getId() + " failed", e);
500         } finally {
501             if (tempSplitPolicyFile != null) {
502                 try {
503                     Files.delete(tempSplitPolicyFile.toPath());
504                 } catch (IOException e) {
505                     LOGGER.debug("delete of temporary file failed", e);
506                 }
507             }
508         }
509     }
510
511     /**
512      * Merge two Apex models together.
513      *
514      * @param mergeInModelName the file name of the model to merge into the current model
515      * @param keepOriginal if this flag is set to true, if a concept exists in both models, the original model copy of
516      *        that concept is kept, if the flag is set to false, then the copy of the concept from the mergeInModel
517      *        overwrites the concept in the original model
518      * @return the result of the operation
519      */
520     public ApexApiResult merge(final String mergeInModelName, final boolean keepOriginal) {
521         ApexApiResult result = new ApexApiResult();
522         AxPolicyModel mergeInPolicyModel = loadModelFromFile(mergeInModelName, result);
523         if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
524             return result;
525         }
526
527         try {
528             AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(apexModel.getPolicyModel(),
529                     mergeInPolicyModel, keepOriginal, false, false);
530             apexModel.setPolicyModel(mergedPolicyModel != null ? mergedPolicyModel : new AxPolicyModel());
531             return new ApexApiResult();
532         } catch (ApexModelException e) {
533             apexModel.setPolicyModel(new AxPolicyModel());
534             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
535         }
536     }
537
538     /**
539      * Merge two Apex models together.
540      *
541      * @param otherModelString the model to merge as a string
542      * @param keepOriginal if this flag is set to true, if a concept exists in both models, the original model copy of
543      *        that concept is kept, if the flag is set to false, then the copy of the concept from the mergeInModel
544      *        overwrites the concept in the original model
545      * @return the result of the operation
546      */
547     public ApexApiResult mergeWithString(final String otherModelString, final boolean keepOriginal) {
548         ApexApiResult result = new ApexApiResult();
549         AxPolicyModel mergeInPolicyModel = loadModelFromString(otherModelString, result);
550         if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
551             return result;
552         }
553
554         try {
555             AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(apexModel.getPolicyModel(),
556                     mergeInPolicyModel, keepOriginal, false, false);
557             apexModel.setPolicyModel(mergedPolicyModel != null ? mergedPolicyModel : new AxPolicyModel());
558             return new ApexApiResult();
559         } catch (ApexModelException e) {
560             apexModel.setPolicyModel(new AxPolicyModel());
561             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
562         }
563     }
564
565     /**
566      * Load a policy model from a file.
567      *
568      * @param fileName the name of the file containing the model
569      * @param result the result of the operation
570      * @return the model
571      */
572     private AxPolicyModel loadModelFromFile(final String fileName, final ApexApiResult result) {
573         Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);
574
575         AxPolicyModel readModel = null;
576
577         final URL apexModelUrl = ResourceUtils.getLocalFile(fileName);
578         if (apexModelUrl == null) {
579             result.setResult(ApexApiResult.Result.FAILED);
580             result.addMessage("file " + fileName + " not found");
581             return null;
582         }
583
584         try {
585             ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
586             apexModelReader.setValidateFlag(false);
587             readModel = apexModelReader.read(apexModelUrl.openStream());
588             result.setResult(ApexApiResult.Result.SUCCESS);
589             return readModel;
590         } catch (Exception e) {
591             result.setResult(ApexApiResult.Result.FAILED);
592             result.addThrowable(e);
593             return null;
594         }
595     }
596
597     /**
598      * Load a policy model from a string.
599      *
600      * @param modelString the string containing the model
601      * @param result the result of the operation
602      * @return the model
603      */
604     private AxPolicyModel loadModelFromString(final String modelString, final ApexApiResult result) {
605         Assertions.argumentNotNull(modelString, "modelString may not be null");
606
607         AxPolicyModel readModel = null;
608
609         InputStream modelStringStream = new ByteArrayInputStream(modelString.getBytes());
610
611         try {
612             ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
613             apexModelReader.setValidateFlag(false);
614             readModel = apexModelReader.read(modelStringStream);
615             result.setResult(ApexApiResult.Result.SUCCESS);
616             return readModel;
617         } catch (Exception e) {
618             result.setResult(ApexApiResult.Result.FAILED);
619             result.addThrowable(e);
620             return null;
621         }
622     }
623 }