54b5651cc39f3b72158ed358b387ef4bfde70244
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021-2022 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.basicmodel.handling;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import lombok.Getter;
27 import lombok.Setter;
28 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * This class writes an Apex model to a file.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  * @param <M> the type of Apex model to write to file, must be a sub class of {@link AxModel}
38  */
39 @Getter
40 @Setter
41 public class ApexModelFileWriter<M extends AxModel> {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
43
44     // Should models being written to files be valid
45     private boolean validate;
46
47     /**
48      * Constructor, set the validation flag.
49      *
50      * @param validate indicates if validation be performed prior to output
51      */
52     public ApexModelFileWriter(final boolean validate) {
53         this.validate = validate;
54     }
55
56     /**
57      * Write a model to an JSON file.
58      *
59      * @param model The model to write
60      * @param rootModelClass The concept class
61      * @param modelFileName The name of the file to write to
62      * @throws ApexException thrown on errors
63      */
64     public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
65                     throws ApexException {
66         LOGGER.debug("running apexModelWriteJSONFile . . .");
67
68         final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
69         modelWriter.setValidate(validate);
70
71         writeModelFile(model, modelWriter, modelFileName);
72
73         LOGGER.debug("ran apexModelWriteJSONFile");
74     }
75
76     /**
77      * Write a model to a file using a model writer.
78      *
79      * @param model The model to write
80      * @param modelWriter the model writer to use to write the model to the file
81      * @param modelFileName the file name of the file to write to
82      * @throws ApexException on exceptions writing the model
83      */
84     private void writeModelFile(final M model, final ApexModelWriter<M> modelWriter, final String modelFileName)
85                     throws ApexException {
86         final var modelFile = new File(modelFileName);
87         if (!modelFile.getParentFile().exists() && !modelFile.getParentFile().mkdirs()) {
88             LOGGER.warn("could not create directory  " + modelFile.getParentFile());
89             throw new ApexException("could not create directory  " + modelFile.getParentFile());
90         }
91
92         try (final var fileOutputStream = new FileOutputStream(modelFile)) {
93             modelWriter.write(model, fileOutputStream);
94         } catch (final Exception e) {
95             LOGGER.warn("error processing file " + modelFile.getAbsolutePath(), e);
96             throw new ApexException("error processing file " + modelFile.getAbsolutePath(), e);
97         }
98     }
99 }