aboutsummaryrefslogtreecommitdiffstats
path: root/model/model-api/src/main/java/org/onap/policy/apex/model/modelapi/impl/ModelHandlerFacade.java
blob: b350af527cd65affa712384f7ce471985b51cb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2020 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.modelapi.impl;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
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.basicmodel.handling.ApexModelFileWriter;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter;
import org.onap.policy.apex.model.modelapi.ApexApiResult;
import org.onap.policy.apex.model.modelapi.ApexModel;
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.handling.PolicyAnalyser;
import org.onap.policy.apex.model.policymodel.handling.PolicyAnalysisResult;
import org.onap.policy.apex.model.policymodel.handling.PolicyModelComparer;
import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger;
import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.resources.TextFileUtils;
import org.onap.policy.common.utils.validation.Assertions;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * This class acts as a facade for model handling for the Apex Model API.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ModelHandlerFacade {
    private static final String FOUND_IN_DATABASE = " found in database";
    private static final String FILE_NAME_MAY_NOT_BE_NULL = "fileName may not be null";
    private static final String MODEL = "model ";
    private static final String ALREADY_LOADED = " already loaded";

    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ModelHandlerFacade.class);

    // Apex model we're working towards
    private final ApexModel apexModel;

    // JSON output on list/delete if set
    private final boolean jsonMode;

    /**
     * This Constructor creates a model handling facade for the given {@link ApexModel}.
     *
     * @param apexModel the apex model to manipulate
     * @param apexProperties properties for the model
     * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise set to false
     */
    public ModelHandlerFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
        Assertions.argumentNotNull(apexModel, "apexModel may not be null");
        Assertions.argumentNotNull(apexProperties, "apexProperties may not be null");

        this.apexModel = apexModel;
        this.jsonMode = jsonMode;
    }

    /**
     * Load an Apex model from a string.
     *
     * @param modelString the string with the model
     * @return the result of the operation
     */
    public ApexApiResult loadFromString(final String modelString) {
        Assertions.argumentNotNull(modelString, "modelString may not be null");

        if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
            return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
                    MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
        }

        ApexApiResult result = new ApexApiResult();
        AxPolicyModel newPolicyModel = loadModelFromString(modelString, result);
        apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());

        return result;
    }

    /**
     * Load an Apex model from a file.
     *
     * @param fileName the file name of the file with the model
     * @return the result of the operation
     */
    public ApexApiResult loadFromFile(final String fileName) {
        Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);

        if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
            return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
                    MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
        }

        ApexApiResult result = new ApexApiResult();
        AxPolicyModel newPolicyModel = loadModelFromFile(fileName, result);
        apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());

        return result;
    }

    /**
     * Save an Apex model to a file.
     *
     * @param fileName the file name
     * @param xmlFlag if true, save the file in XML format, otherwise save the file in the default JSON format
     * @return the result of the operation
     */
    public ApexApiResult saveToFile(final String fileName, final boolean xmlFlag) {
        Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);

        ApexModelFileWriter<AxPolicyModel> apexModelFileWriter = new ApexModelFileWriter<>(false);

        try {
            if (xmlFlag) {
                apexModelFileWriter.apexModelWriteXmlFile(apexModel.getPolicyModel(), AxPolicyModel.class, fileName);
            } else {
                apexModelFileWriter.apexModelWriteJsonFile(apexModel.getPolicyModel(), AxPolicyModel.class, fileName);
            }
            return new ApexApiResult();
        } catch (ApexException e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Load an Apex model from a database.
     *
     * @param modelName the name of the model to load
     * @param modelVersion the version of the model to load, loads the policy model from the database with this name, if
     *        more than one exist, an exception is thrown
     * @param daoParameters the parameters to use to access the database over JDBC
     * @return the result of the operation
     */
    public ApexApiResult loadFromDatabase(final String modelName, final String modelVersion,
            final DaoParameters daoParameters) {
        Assertions.argumentNotNull(modelName, "modelName may not be null");
        Assertions.argumentNotNull(daoParameters, "DaoParameters may not be null");

        if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
            return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
                    MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
        }

        ApexDao apexDao = null;
        try {
            apexDao = new ApexDaoFactory().createApexDao(daoParameters);
            apexDao.init(daoParameters);

            // Single specific model requested
            if (modelVersion != null) {
                AxPolicyModel daoPolicyModel =
                        apexDao.get(AxPolicyModel.class, new AxArtifactKey(modelName, modelVersion));

                if (daoPolicyModel != null) {
                    apexModel.setPolicyModel(daoPolicyModel);
                    return new ApexApiResult();
                } else {
                    apexModel.setPolicyModel(new AxPolicyModel());
                    return new ApexApiResult(ApexApiResult.Result.FAILED, "no policy model with name " + modelName
                            + " and version " + modelVersion + FOUND_IN_DATABASE);
                }
            } else {
                // Fishing expedition
                return searchInDatabase(modelName, apexDao, apexModel);
            }
        } catch (ApexException | ApexRuntimeException e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        } finally {
            if (apexDao != null) {
                apexDao.close();
            }
        }
    }

    /**
     * Search for an Apex model in the database.
     *
     * @param modelName the name of the model to load
     * @param apexDao the DAO to use to find the model
     * @param apexModel the APEX model we are loading the found model into
     * @return the result of the operation
     */
    private ApexApiResult searchInDatabase(String modelName, ApexDao apexDao, ApexModel apexModel) {
        AxPolicyModel foundPolicyModel = null;

        List<AxPolicyModel> policyModelList = apexDao.getAll(AxPolicyModel.class);
        for (AxPolicyModel dbPolicyModel : policyModelList) {
            if (dbPolicyModel.getKey().getName().equals(modelName)) {
                if (foundPolicyModel == null) {
                    foundPolicyModel = dbPolicyModel;
                } else {
                    return new ApexApiResult(ApexApiResult.Result.FAILED,
                            "more than one policy model with name " + modelName + FOUND_IN_DATABASE);
                }
            }
        }

        if (foundPolicyModel != null) {
            apexModel.setPolicyModel(foundPolicyModel);
            return new ApexApiResult();
        } else {
            apexModel.setPolicyModel(new AxPolicyModel());
            return new ApexApiResult(ApexApiResult.Result.FAILED,
                    "no policy model with name " + modelName + FOUND_IN_DATABASE);
        }
    }

    /**
     * Save an Apex model to a database.
     *
     * @param daoParameters the parameters to use to access the database over JDBC
     * @return the result of the operation
     */
    public ApexApiResult saveToDatabase(final DaoParameters daoParameters) {
        ApexDao apexDao = null;

        try {
            apexDao = new ApexDaoFactory().createApexDao(daoParameters);
            apexDao.init(daoParameters);

            apexDao.create(apexModel.getPolicyModel());
            return new ApexApiResult();
        } catch (ApexException e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        } finally {
            if (apexDao != null) {
                apexDao.close();
            }
        }
    }

    /**
     * Read an APEX model from a location identified by a URL.
     *
     * @param urlString the url string
     * @return the result of the operation
     */
    public ApexApiResult readFromUrl(final String urlString) {
        Assertions.argumentNotNull(urlString, "urlString may not be null");

        if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
            return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
                    MODEL + apexModel.getPolicyModel().getKey().getId() + ALREADY_LOADED);
        }

        URL apexModelUrl;
        try {
            apexModelUrl = new URL(urlString);
        } catch (MalformedURLException e) {
            ApexApiResult result = new ApexApiResult(ApexApiResult.Result.FAILED);
            result.addMessage("URL string " + urlString + " is not a valid URL");
            result.addThrowable(e);
            return result;
        }

        try {
            ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
            apexModelReader.setValidateFlag(false);
            AxPolicyModel newPolicyModel = apexModelReader.read(apexModelUrl.openStream());
            apexModel.setPolicyModel(newPolicyModel != null ? newPolicyModel : new AxPolicyModel());
            return new ApexApiResult();
        } catch (ApexModelException | IOException e) {
            apexModel.setPolicyModel(new AxPolicyModel());
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Write an APEX model to a location identified by a URL.
     *
     * @param urlString the URL to read the model from
     * @param xmlFlag if true, save the file in XML format, otherwise save the file in the default JSON format
     * @return the result of the operation
     */
    public ApexApiResult writeToUrl(final String urlString, final boolean xmlFlag) {
        Assertions.argumentNotNull(urlString, "urlString may not be null");

        URL apexModelUrl;
        try {
            apexModelUrl = new URL(urlString);
        } catch (MalformedURLException e) {
            ApexApiResult result = new ApexApiResult(ApexApiResult.Result.FAILED);
            result.addMessage("URL string " + urlString + " is not a valid URL");
            result.addThrowable(e);
            return result;
        }

        try {
            ApexModelWriter<AxPolicyModel> apexModelWriter = new ApexModelWriter<>(AxPolicyModel.class);
            apexModelWriter.setValidateFlag(false);
            apexModelWriter.setJsonOutput(!xmlFlag);

            // Open the URL for output and write the model
            URLConnection urlConnection = apexModelUrl.openConnection();
            urlConnection.setDoOutput(true);

            apexModelWriter.write(apexModel.getPolicyModel(), urlConnection.getOutputStream());
            return new ApexApiResult();
        } catch (ApexModelException | IOException e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Analyse an Apex model that shows the concept usage references of a policy model.
     *
     * @return the result of the operation
     */
    public ApexApiResult analyse() {
        PolicyAnalysisResult analysisResult = new PolicyAnalyser().analyse(apexModel.getPolicyModel());
        return new ApexApiResult(ApexApiResult.Result.SUCCESS, analysisResult.toString());
    }

    /**
     * Validate an Apex model, checking all concepts and references in the model.
     *
     * @return the result of the operation
     */
    public ApexApiResult validate() {
        ApexApiResult result = new ApexApiResult();
        try {
            AxValidationResult validationResult = apexModel.getPolicyModel().validate(new AxValidationResult());

            if (!validationResult.isValid()) {
                result.setResult(ApexApiResult.Result.FAILED);
            }
            result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
                    .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
            result.addMessage(validationResult.toString());
            return result;
        } catch (Exception e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Compare to Apex models, returning the differences between the models.
     *
     * @param otherModelFileName the file name of the other model
     * @param diffsOnly only returns differences between the model when set
     * @param keysOnly only returns the keys that are different when set, when not set values are also returned
     * @return the result of the operation
     */
    public ApexApiResult compare(final String otherModelFileName, final boolean diffsOnly, final boolean keysOnly) {
        ApexApiResult result = new ApexApiResult();
        try {
            AxPolicyModel otherPolicyModel = loadModelFromFile(otherModelFileName, result);
            if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
                return result;
            }

            PolicyModelComparer policyModelComparer =
                    new PolicyModelComparer(apexModel.getPolicyModel(), otherPolicyModel);
            result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
                    .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
            result.addMessage(policyModelComparer.toString());

            return result;
        } catch (Exception e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Compare two Apex models, returning the differences between the models.
     *
     * @param otherModelString the other model as a string
     * @param diffsOnly only returns differences between the model when set
     * @param keysOnly only returns the keys that are different when set, when not set values are also returned
     * @return the result of the operation
     */
    public ApexApiResult compareWithString(final String otherModelString, final boolean diffsOnly,
            final boolean keysOnly) {
        ApexApiResult result = new ApexApiResult();
        try {
            AxPolicyModel otherPolicyModel = loadModelFromString(otherModelString, result);
            if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
                return result;
            }

            PolicyModelComparer policyModelComparer =
                    new PolicyModelComparer(apexModel.getPolicyModel(), otherPolicyModel);
            result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false)
                    .writeString(apexModel.getPolicyModel().getKey(), AxArtifactKey.class, jsonMode));
            result.addMessage(policyModelComparer.toString());

            return result;
        } catch (Exception e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Split out a sub model from an Apex model that contains a given subset of the policies in the original model.
     *
     * @param targetModelName the file name of the target model in which to store the model split out from the original
     *        model
     * @param splitOutPolicies the policies form the original model to include in the split out model, specified as a
     *        comma delimited list of policy names
     * @return the result of the operation
     */
    public ApexApiResult split(final String targetModelName, final String splitOutPolicies) {
        Set<AxArtifactKey> requiredPolicySet = new LinkedHashSet<>();

        // Split the policy names on comma
        String[] policyNames = splitOutPolicies.split(",");

        // Iterate over the policy names
        for (String policyName : policyNames) {
            // Split out this specific policy
            AxPolicy requiredPolicy = apexModel.getPolicyModel().getPolicies().get(policyName);

            if (requiredPolicy != null) {
                requiredPolicySet.add(requiredPolicy.getKey());
            } else {
                return new ApexApiResult(ApexApiResult.Result.FAILED,
                        "policy for policy name " + policyName + " not found in model");
            }
        }

        try {
            AxPolicyModel splitPolicyModel =
                    PolicyModelSplitter.getSubPolicyModel(apexModel.getPolicyModel(), requiredPolicySet, false);

            ApexModelFileWriter<AxPolicyModel> apexModelFileWriter = new ApexModelFileWriter<>(false);
            apexModelFileWriter.apexModelWriteJsonFile(splitPolicyModel, AxPolicyModel.class, targetModelName);
            return new ApexApiResult();
        } catch (ApexException e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Split out a sub model from an Apex model that contains a given subset of the policies in the original model,
     * return the split model in the result as a string.
     *
     * @param splitOutPolicies the policies form the original model to include in the split out model, specified as a
     *        comma delimited list of policy names
     * @return the result of the operation
     */
    public ApexApiResult split(final String splitOutPolicies) {
        ApexApiResult splitResult = new ApexApiResult();
        File tempSplitPolicyFile = null;
        try {
            tempSplitPolicyFile = TextFileUtils.createTempFile("ApexTempPolicy", null);

            // Split the policy into a temporary file
            splitResult = split(tempSplitPolicyFile.getCanonicalPath(), splitOutPolicies);
            if (splitResult.isNok()) {
                return splitResult;
            }

            // Get the policy model into a string
            String splitPolicyModelString = TextFileUtils.getTextFileAsString(tempSplitPolicyFile.getCanonicalPath());

            // Return the policy model
            splitResult.addMessage(splitPolicyModelString);
            return splitResult;
        } catch (Exception e) {
            return new ApexApiResult(ApexApiResult.Result.FAILED,
                    "split of policy model " + apexModel.getPolicyModel().getId() + " failed", e);
        } finally {
            if (tempSplitPolicyFile != null) {
                try {
                    Files.delete(tempSplitPolicyFile.toPath());
                } catch (IOException e) {
                    LOGGER.debug("delete of temporary file failed", e);
                }
            }
        }
    }

    /**
     * Merge two Apex models together.
     *
     * @param mergeInModelName the file name of the model to merge into the current model
     * @param keepOriginal if this flag is set to true, if a concept exists in both models, the original model copy of
     *        that concept is kept, if the flag is set to false, then the copy of the concept from the mergeInModel
     *        overwrites the concept in the original model
     * @return the result of the operation
     */
    public ApexApiResult merge(final String mergeInModelName, final boolean keepOriginal) {
        ApexApiResult result = new ApexApiResult();
        AxPolicyModel mergeInPolicyModel = loadModelFromFile(mergeInModelName, result);
        if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
            return result;
        }

        try {
            AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(apexModel.getPolicyModel(),
                    mergeInPolicyModel, keepOriginal, false, false);
            apexModel.setPolicyModel(mergedPolicyModel != null ? mergedPolicyModel : new AxPolicyModel());
            return new ApexApiResult();
        } catch (ApexModelException e) {
            apexModel.setPolicyModel(new AxPolicyModel());
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Merge two Apex models together.
     *
     * @param otherModelString the model to merge as a string
     * @param keepOriginal if this flag is set to true, if a concept exists in both models, the original model copy of
     *        that concept is kept, if the flag is set to false, then the copy of the concept from the mergeInModel
     *        overwrites the concept in the original model
     * @return the result of the operation
     */
    public ApexApiResult mergeWithString(final String otherModelString, final boolean keepOriginal) {
        ApexApiResult result = new ApexApiResult();
        AxPolicyModel mergeInPolicyModel = loadModelFromString(otherModelString, result);
        if (!result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
            return result;
        }

        try {
            AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(apexModel.getPolicyModel(),
                    mergeInPolicyModel, keepOriginal, false, false);
            apexModel.setPolicyModel(mergedPolicyModel != null ? mergedPolicyModel : new AxPolicyModel());
            return new ApexApiResult();
        } catch (ApexModelException e) {
            apexModel.setPolicyModel(new AxPolicyModel());
            return new ApexApiResult(ApexApiResult.Result.FAILED, e);
        }
    }

    /**
     * Load a policy model from a file.
     *
     * @param fileName the name of the file containing the model
     * @param result the result of the operation
     * @return the model
     */
    private AxPolicyModel loadModelFromFile(final String fileName, final ApexApiResult result) {
        Assertions.argumentNotNull(fileName, FILE_NAME_MAY_NOT_BE_NULL);

        AxPolicyModel readModel = null;

        final URL apexModelUrl = ResourceUtils.getLocalFile(fileName);
        if (apexModelUrl == null) {
            result.setResult(ApexApiResult.Result.FAILED);
            result.addMessage("file " + fileName + " not found");
            return null;
        }

        try {
            ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
            apexModelReader.setValidateFlag(false);
            readModel = apexModelReader.read(apexModelUrl.openStream());
            result.setResult(ApexApiResult.Result.SUCCESS);
            return readModel;
        } catch (Exception e) {
            result.setResult(ApexApiResult.Result.FAILED);
            result.addThrowable(e);
            return null;
        }
    }

    /**
     * Load a policy model from a string.
     *
     * @param modelString the string containing the model
     * @param result the result of the operation
     * @return the model
     */
    private AxPolicyModel loadModelFromString(final String modelString, final ApexApiResult result) {
        Assertions.argumentNotNull(modelString, "modelString may not be null");

        AxPolicyModel readModel = null;

        InputStream modelStringStream = new ByteArrayInputStream(modelString.getBytes());

        try {
            ApexModelReader<AxPolicyModel> apexModelReader = new ApexModelReader<>(AxPolicyModel.class);
            apexModelReader.setValidateFlag(false);
            readModel = apexModelReader.read(modelStringStream);
            result.setResult(ApexApiResult.Result.SUCCESS);
            return readModel;
        } catch (Exception e) {
            result.setResult(ApexApiResult.Result.FAILED);
            result.addThrowable(e);
            return null;
        }
    }
}