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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
24 import java.io.FileOutputStream;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * This class writes an Apex model to a file.
33 * @author Liam Fallon (liam.fallon@ericsson.com)
34 * @param <M> the type of Apex model to write to file, must be a sub class of {@link AxModel}
36 public class ApexModelFileWriter<M extends AxModel> {
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
39 // Should models being written to files be valid
40 private boolean validateFlag;
43 * Constructor, set the validation flag.
45 * @param validateFlag indicates if validation be performed prior to output
47 public ApexModelFileWriter(final boolean validateFlag) {
48 this.validateFlag = validateFlag;
52 * Write a model to an XML file.
54 * @param model The model to write
55 * @param rootModelClass The concept class
56 * @param modelFileName The name of the file to write to
57 * @throws ApexException thrown on errors
59 public void apexModelWriteXmlFile(final M model, final Class<M> rootModelClass, final String modelFileName)
60 throws ApexException {
61 LOGGER.debug("running apexModelWriteXMLFile . . .");
63 final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
64 modelWriter.setValidateFlag(validateFlag);
65 modelWriter.getCDataFieldSet().add("description");
66 modelWriter.getCDataFieldSet().add("logic");
67 modelWriter.getCDataFieldSet().add("uiLogic");
69 writeModelFile(model, modelWriter, modelFileName);
71 LOGGER.debug("ran apexModelWriteXMLFile");
75 * Write a model to an JSON file.
77 * @param model The model to write
78 * @param rootModelClass The concept class
79 * @param modelFileName The name of the file to write to
80 * @throws ApexException thrown on errors
82 public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
83 throws ApexException {
84 LOGGER.debug("running apexModelWriteJSONFile . . .");
86 final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
87 modelWriter.setJsonOutput(true);
88 modelWriter.setValidateFlag(validateFlag);
90 writeModelFile(model, modelWriter, modelFileName);
92 LOGGER.debug("ran apexModelWriteJSONFile");
96 * Checks if the validation flag is set.
98 * @return true, the validation flag is set
100 public boolean isValidateFlag() {
105 * Sets the validate flag.
107 * @param validateFlag the validate flag value
109 public void setValidateFlag(final boolean validateFlag) {
110 this.validateFlag = validateFlag;
114 * Write a model to a file using a model writer.
116 * @param model The model to write
117 * @param modelWriter the model writer to use to write the model to the file
118 * @param modelFileName the file name of the file to write to
119 * @throws ApexException on exceptions writing the model
121 private void writeModelFile(final M model, final ApexModelWriter<M> modelWriter, final String modelFileName)
122 throws ApexException {
123 final File modelFile = new File(modelFileName);
124 if (!modelFile.getParentFile().exists() && !modelFile.getParentFile().mkdirs()) {
125 LOGGER.warn("could not create directory " + modelFile.getParentFile());
126 throw new ApexException("could not create directory " + modelFile.getParentFile());
130 final FileOutputStream fileOutputStream = new FileOutputStream(modelFile);
131 modelWriter.write(model, fileOutputStream);
132 fileOutputStream.close();
133 } catch (final Exception e) {
134 LOGGER.warn("error processing file " + modelFile.getAbsolutePath(), e);
135 throw new ApexException("error processing file " + modelFile.getAbsolutePath(), e);