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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.handling;
25 import java.io.FileOutputStream;
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;
34 * This class writes an Apex model to a file.
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}
41 public class ApexModelFileWriter<M extends AxModel> {
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
44 // Should models being written to files be valid
45 private boolean validate;
48 * Constructor, set the validation flag.
50 * @param validate indicates if validation be performed prior to output
52 public ApexModelFileWriter(final boolean validate) {
53 this.validate = validate;
57 * Write a model to an JSON file.
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
64 public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
65 throws ApexException {
66 LOGGER.debug("running apexModelWriteJSONFile . . .");
68 final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
69 modelWriter.setValidate(validate);
71 writeModelFile(model, modelWriter, modelFileName);
73 LOGGER.debug("ran apexModelWriteJSONFile");
77 * Write a model to a file using a model writer.
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
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());
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);