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;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
27 import org.onap.policy.common.utils.validation.Assertions;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class is used to save Apex models to file in XML or JSON format.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
35 * @param <M> the type of Apex model to save to file, must be a sub class of {@link AxModel}
37 public class ApexModelSaver<M extends AxModel> {
38 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelSaver.class);
40 // The class of the model and the model to write to disk
41 private final Class<M> rootModelClass;
42 private final M model;
44 // The path into which to write the models
45 private final String writePath;
48 * Constructor, specifies the type of the Apex model (a sub class of {@link AxModel}), the model to write, and the
49 * path of a directory to which to write the model.
51 * @param rootModelClass the class of the model, a sub class of {@link AxModel}
52 * @param model the model to write, an instance of a sub class of {@link AxModel}
53 * @param writePath the directory to which models will be written. The name of the written model will be the Model
54 * Name for its key with the suffix {@code .xml} or {@code .json}.
56 public ApexModelSaver(final Class<M> rootModelClass, final M model, final String writePath) {
57 Assertions.argumentNotNull(rootModelClass, "argument rootModelClass may not be null");
58 Assertions.argumentNotNull(model, "argument model may not be null");
59 Assertions.argumentNotNull(writePath, "writePath rootModelClass may not be null");
61 this.rootModelClass = rootModelClass;
63 this.writePath = writePath;
67 * Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>}
69 * @throws ApexException on errors writing the Apex model
71 public void apexModelWriteXml() throws ApexException {
72 LOGGER.debug("running apexModelWriteXML . . .");
74 // Write the file to disk
75 final File xmlFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".xml");
76 new ApexModelFileWriter<M>(true).apexModelWriteXmlFile(model, rootModelClass, xmlFile.getPath());
78 LOGGER.debug("ran apexModelWriteXML");
82 * Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>}
84 * @throws ApexException on errors writing the Apex model
86 public void apexModelWriteJson() throws ApexException {
87 LOGGER.debug("running apexModelWriteJSON . . .");
89 // Write the file to disk
90 final File jsonFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".json");
91 new ApexModelFileWriter<M>(true).apexModelWriteJsonFile(model, rootModelClass, jsonFile.getPath());
93 LOGGER.debug("ran apexModelWriteJSON");