91e58d663929cea5204df31f9be5e33544617b64
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelStringWriter.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.ByteArrayOutputStream;
25
26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
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 writes an Apex concept to a string.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept}
37  */
38 public class ApexModelStringWriter<C extends AxConcept> {
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
40
41     // Should concepts being written to files be valid
42     private boolean validateFlag;
43
44     /**
45      * Constructor, set the validation flag.
46      *
47      * @param validateFlag Should validation be performed prior to output
48      */
49     public ApexModelStringWriter(final boolean validateFlag) {
50         this.validateFlag = validateFlag;
51     }
52
53     /**
54      * Write a concept to a string.
55      *
56      * @param concept The concept to write
57      * @param rootConceptClass The concept class
58      * @param jsonFlag writes JSON if true, and a generic string if false
59      * @return The string with the concept
60      * @throws ApexException thrown on errors
61      */
62     public String writeString(final C concept, final Class<C> rootConceptClass, final boolean jsonFlag)
63                     throws ApexException {
64         Assertions.argumentNotNull(concept, "concept may not be null");
65
66         if (jsonFlag) {
67             return writeJsonString(concept, rootConceptClass);
68         } else {
69             return concept.toString();
70         }
71     }
72
73     /**
74      * Write a concept to an XML string.
75      *
76      * @param concept The concept to write
77      * @param rootConceptClass The concept class
78      * @return The string with the concept
79      * @throws ApexException thrown on errors
80      */
81     public String writeXmlString(final C concept, final Class<C> rootConceptClass) throws ApexException {
82         LOGGER.debug("running writeXMLString . . .");
83
84         final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
85         conceptWriter.setValidateFlag(validateFlag);
86         conceptWriter.getCDataFieldSet().add("description");
87         conceptWriter.getCDataFieldSet().add("logic");
88         conceptWriter.getCDataFieldSet().add("uiLogic");
89
90         final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
91         try {
92             conceptWriter.write(concept, baOutputStream);
93             baOutputStream.close();
94         } catch (final Exception e) {
95             LOGGER.warn("error writing XML string", e);
96             throw new ApexException("error writing XML string", e);
97         }
98
99         LOGGER.debug("ran writeXMLString");
100         return baOutputStream.toString();
101     }
102
103     /**
104      * Write a concept to a JSON string.
105      *
106      * @param concept The concept to write
107      * @param rootConceptClass The concept class
108      * @return The string with the concept
109      * @throws ApexException thrown on errors
110      */
111     public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
112         LOGGER.debug("running writeJSONString . . .");
113
114         final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
115         conceptWriter.setJsonOutput(true);
116         conceptWriter.setValidateFlag(validateFlag);
117
118         final ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
119         try {
120             conceptWriter.write(concept, baOutputStream);
121             baOutputStream.close();
122         } catch (final Exception e) {
123             LOGGER.warn("error writing JSON string", e);
124             throw new ApexException("error writing JSON string", e);
125         }
126
127         LOGGER.debug("ran writeJSONString");
128         return baOutputStream.toString();
129     }
130
131     /**
132      * Checks if is validate flag.
133      *
134      * @return true, if checks if is validate flag
135      */
136     public boolean isValidateFlag() {
137         return validateFlag;
138     }
139
140     /**
141      * Sets the validate flag.
142      *
143      * @param validateFlag the validate flag
144      */
145     public void setValidateFlag(final boolean validateFlag) {
146         this.validateFlag = validateFlag;
147     }
148 }