2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
28 import org.onap.policy.common.utils.validation.Assertions;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
33 * This class is used to save Apex models to file in XML or JSON format.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
36 * @param <M> the type of Apex model to save to file, must be a sub class of {@link AxModel}
38 public class ApexModelSaver<M extends AxModel> {
39 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelSaver.class);
41 // The class of the model and the model to write to disk
42 private final Class<M> rootModelClass;
43 private final M model;
45 // The path into which to write the models
46 private final String writePath;
49 * Constructor, specifies the type of the Apex model (a sub class of {@link AxModel}), the model to write, and the
50 * path of a directory to which to write the model.
52 * @param rootModelClass the class of the model, a sub class of {@link AxModel}
53 * @param model the model to write, an instance of a sub class of {@link AxModel}
54 * @param writePath the directory to which models will be written. The name of the written model will be the Model
55 * Name for its key with the suffix {@code .xml} or {@code .json}.
57 public ApexModelSaver(final Class<M> rootModelClass, final M model, final String writePath) {
58 Assertions.argumentNotNull(rootModelClass, "argument rootModelClass may not be null");
59 Assertions.argumentNotNull(model, "argument model may not be null");
60 Assertions.argumentNotNull(writePath, "writePath rootModelClass may not be null");
62 this.rootModelClass = rootModelClass;
64 this.writePath = writePath;
68 * Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>}
70 * @throws ApexException on errors writing the Apex model
72 public void apexModelWriteXml() throws ApexException {
73 LOGGER.debug("running apexModelWriteXML . . .");
75 // Write the file to disk
76 final File xmlFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".xml");
77 new ApexModelFileWriter<M>(true).apexModelWriteXmlFile(model, rootModelClass, xmlFile.getPath());
79 LOGGER.debug("ran apexModelWriteXML");
83 * Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>}
85 * @throws ApexException on errors writing the Apex model
87 public void apexModelWriteJson() throws ApexException {
88 LOGGER.debug("running apexModelWriteJSON . . .");
90 // Write the file to disk
91 final File jsonFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".json");
92 new ApexModelFileWriter<M>(true).apexModelWriteJsonFile(model, rootModelClass, jsonFile.getPath());
94 LOGGER.debug("ran apexModelWriteJSON");