aead4cb2bbf8537af2c28497abcf0b6212798bfb
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelFileWriter.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.basicmodel.handling;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
30
31 /**
32  * This class writes an Apex model to a file.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  * @param <M> the type of Apex model to write to file, must be a sub class of {@link AxModel}
36  */
37 public class ApexModelFileWriter<M extends AxModel> {
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
39
40     // Should models being written to files be valid
41     private boolean validateFlag;
42
43     /**
44      * Constructor, set the validation flag.
45      *
46      * @param validateFlag indicates if validation be performed prior to output
47      */
48     public ApexModelFileWriter(final boolean validateFlag) {
49         this.validateFlag = validateFlag;
50     }
51
52     /**
53      * Write a model to an XML file.
54      *
55      * @param model The model to write
56      * @param rootModelClass The concept class
57      * @param modelFileName The name of the file to write to
58      * @throws ApexException thrown on errors
59      */
60     public void apexModelWriteXmlFile(final M model, final Class<M> rootModelClass, final String modelFileName)
61                     throws ApexException {
62         LOGGER.debug("running apexModelWriteXMLFile . . .");
63
64         final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
65         modelWriter.setValidateFlag(validateFlag);
66         modelWriter.getCDataFieldSet().add("description");
67         modelWriter.getCDataFieldSet().add("logic");
68         modelWriter.getCDataFieldSet().add("uiLogic");
69
70         writeModelFile(model, modelWriter, modelFileName);
71
72         LOGGER.debug("ran apexModelWriteXMLFile");
73     }
74
75     /**
76      * Write a model to an JSON file.
77      *
78      * @param model The model to write
79      * @param rootModelClass The concept class
80      * @param modelFileName The name of the file to write to
81      * @throws ApexException thrown on errors
82      */
83     public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
84                     throws ApexException {
85         LOGGER.debug("running apexModelWriteJSONFile . . .");
86
87         final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
88         modelWriter.setJsonOutput(true);
89         modelWriter.setValidateFlag(validateFlag);
90
91         writeModelFile(model, modelWriter, modelFileName);
92
93         LOGGER.debug("ran apexModelWriteJSONFile");
94     }
95
96     /**
97      * Checks if the validation flag is set.
98      *
99      * @return true, the validation flag is set
100      */
101     public boolean isValidateFlag() {
102         return validateFlag;
103     }
104
105     /**
106      * Sets the validate flag.
107      *
108      * @param validateFlag the validate flag value
109      */
110     public void setValidateFlag(final boolean validateFlag) {
111         this.validateFlag = validateFlag;
112     }
113
114     /**
115      * Write a model to a file using a model writer.
116      *
117      * @param model The model to write
118      * @param modelWriter the model writer to use to write the model to the file
119      * @param modelFileName the file name of the file to write to
120      * @throws ApexException on exceptions writing the model
121      */
122     private void writeModelFile(final M model, final ApexModelWriter<M> modelWriter, final String modelFileName)
123                     throws ApexException {
124         final File modelFile = new File(modelFileName);
125         if (!modelFile.getParentFile().exists() && !modelFile.getParentFile().mkdirs()) {
126             LOGGER.warn("could not create directory  " + modelFile.getParentFile());
127             throw new ApexException("could not create directory  " + modelFile.getParentFile());
128         }
129
130         try {
131             final FileOutputStream fileOutputStream = new FileOutputStream(modelFile);
132             modelWriter.write(model, fileOutputStream);
133             fileOutputStream.close();
134         } catch (final Exception e) {
135             LOGGER.warn("error processing file " + modelFile.getAbsolutePath(), e);
136             throw new ApexException("error processing file " + modelFile.getAbsolutePath(), e);
137         }
138     }
139 }