1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.model.basicmodel.handling;
import java.io.File;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
import org.onap.policy.apex.model.utilities.Assertions;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* This class is used to save Apex models to file in XML or JSON format.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
* @param <M> the type of Apex model to save to file, must be a sub class of {@link AxModel}
*/
public class ApexModelSaver<M extends AxModel> {
private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelSaver.class);
// The class of the model and the model to write to disk
private final Class<M> rootModelClass;
private final M model;
// The path into which to write the models
private final String writePath;
/**
* Constructor, specifies the type of the Apex model (a sub class of {@link AxModel}), the model to write, and the path of a directory to which to write the
* model.
*
* @param rootModelClass the class of the model, a sub class of {@link AxModel}
* @param model the model to write, an instance of a sub class of {@link AxModel}
* @param writePath the directory to which models will be written. The name of the written model will be the Model Name for its key with the suffix
* {@code .xml} or {@code .json}.
*/
public ApexModelSaver(final Class<M> rootModelClass, final M model, final String writePath) {
Assertions.argumentNotNull(rootModelClass, "argument rootModelClass may not be null");
Assertions.argumentNotNull(model, "argument model may not be null");
Assertions.argumentNotNull(writePath, "writePath rootModelClass may not be null");
this.rootModelClass = rootModelClass;
this.model = model;
this.writePath = writePath;
}
/**
* Write an Apex model to a file in XML format. The model will be written to {@code <writePath/modelKeyName.xml>}
*
* @throws ApexException on errors writing the Apex model
*/
public void apexModelWriteXML() throws ApexException {
LOGGER.debug("running apexModelWriteXML . . .");
// Write the file to disk
final File xmlFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".xml");
new ApexModelFileWriter<M>(true).apexModelWriteXMLFile(model, rootModelClass, xmlFile.getPath());
LOGGER.debug("ran apexModelWriteXML");
}
/**
* Write an Apex model to a file in JSON format. The model will be written to {@code <writePath/modelKeyName.json>}
*
* @throws ApexException on errors writing the Apex model
*/
public void apexModelWriteJSON() throws ApexException {
LOGGER.debug("running apexModelWriteJSON . . .");
// Write the file to disk
final File jsonFile = new File(writePath + File.separatorChar + model.getKey().getName() + ".json");
new ApexModelFileWriter<M>(true).apexModelWriteJSONFile(model, rootModelClass, jsonFile.getPath());
LOGGER.debug("ran apexModelWriteJSON");
}
}
|