50e8bb5fa491d5beb3704b01f64271c8a37f0a85
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelSaver.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.handling;
23
24 import java.io.File;
25
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;
31
32 /**
33  * This class is used to save Apex models to file in XML or JSON format.
34  *
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}
37  */
38 public class ApexModelSaver<M extends AxModel> {
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelSaver.class);
40
41     // The class of the model and the model to write to disk
42     private final Class<M> rootModelClass;
43     private final M model;
44
45     // The path into which to write the models
46     private final String writePath;
47
48     /**
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.
51      *
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}.
56      */
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");
61
62         this.rootModelClass = rootModelClass;
63         this.model = model;
64         this.writePath = writePath;
65     }
66
67     /**
68      * Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>}
69      *
70      * @throws ApexException on errors writing the Apex model
71      */
72     public void apexModelWriteXml() throws ApexException {
73         LOGGER.debug("running apexModelWriteXML . . .");
74
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());
78
79         LOGGER.debug("ran apexModelWriteXML");
80     }
81
82     /**
83      * Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>}
84      *
85      * @throws ApexException on errors writing the Apex model
86      */
87     public void apexModelWriteJson() throws ApexException {
88         LOGGER.debug("running apexModelWriteJSON . . .");
89
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());
93
94         LOGGER.debug("ran apexModelWriteJSON");
95     }
96 }