Changes for checkstyle 8.32
[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 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;
30
31 /**
32  * This class is used to save Apex models to file in XML or JSON format.
33  *
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}
36  */
37 public class ApexModelSaver<M extends AxModel> {
38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelSaver.class);
39
40     // The class of the model and the model to write to disk
41     private final Class<M> rootModelClass;
42     private final M model;
43
44     // The path into which to write the models
45     private final String writePath;
46
47     /**
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.
50      *
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}.
55      */
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");
60
61         this.rootModelClass = rootModelClass;
62         this.model = model;
63         this.writePath = writePath;
64     }
65
66     /**
67      * Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>}
68      *
69      * @throws ApexException on errors writing the Apex model
70      */
71     public void apexModelWriteXml() throws ApexException {
72         LOGGER.debug("running apexModelWriteXML . . .");
73
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());
77
78         LOGGER.debug("ran apexModelWriteXML");
79     }
80
81     /**
82      * Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>}
83      *
84      * @throws ApexException on errors writing the Apex model
85      */
86     public void apexModelWriteJson() throws ApexException {
87         LOGGER.debug("running apexModelWriteJSON . . .");
88
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());
92
93         LOGGER.debug("ran apexModelWriteJSON");
94     }
95 }