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;
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;
32 * This class writes an Apex model to a file.
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}
37 public class ApexModelFileWriter<M extends AxModel> {
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
40 // Should models being written to files be valid
41 private boolean validateFlag;
44 * Constructor, set the validation flag.
46 * @param validateFlag indicates if validation be performed prior to output
48 public ApexModelFileWriter(final boolean validateFlag) {
49 this.validateFlag = validateFlag;
53 * Write a model to an XML file.
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
60 public void apexModelWriteXmlFile(final M model, final Class<M> rootModelClass, final String modelFileName)
61 throws ApexException {
62 LOGGER.debug("running apexModelWriteXMLFile . . .");
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");
70 writeModelFile(model, modelWriter, modelFileName);
72 LOGGER.debug("ran apexModelWriteXMLFile");
76 * Write a model to an JSON file.
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
83 public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
84 throws ApexException {
85 LOGGER.debug("running apexModelWriteJSONFile . . .");
87 final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
88 modelWriter.setJsonOutput(true);
89 modelWriter.setValidateFlag(validateFlag);
91 writeModelFile(model, modelWriter, modelFileName);
93 LOGGER.debug("ran apexModelWriteJSONFile");
97 * Checks if the validation flag is set.
99 * @return true, the validation flag is set
101 public boolean isValidateFlag() {
106 * Sets the validate flag.
108 * @param validateFlag the validate flag value
110 public void setValidateFlag(final boolean validateFlag) {
111 this.validateFlag = validateFlag;
115 * Write a model to a file using a model writer.
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
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());
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);