a9df23afcf27f3145f8f8a38a6765cd88cab8a04
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2022 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 import lombok.Getter;
26 import lombok.Setter;
27 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
29 import org.onap.policy.common.utils.validation.Assertions;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * This class writes an Apex concept to a string.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  * @param <C> the type of Apex concept to write to a string, must be a sub class of {@link AxConcept}
38  */
39 @Getter
40 @Setter
41 public class ApexModelStringWriter<C extends AxConcept> {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelStringWriter.class);
43
44     // Should concepts being written to files be valid
45     private boolean validate;
46
47     /**
48      * Constructor, set the validation flag.
49      *
50      * @param validate Should validation be performed prior to output
51      */
52     public ApexModelStringWriter(final boolean validate) {
53         this.validate = validate;
54     }
55
56     /**
57      * Write a concept to a string.
58      *
59      * @param concept The concept to write
60      * @param rootConceptClass The concept class
61      * @return The string with the concept
62      * @throws ApexException thrown on errors
63      */
64     public String writeString(final C concept, final Class<C> rootConceptClass)
65                     throws ApexException {
66         Assertions.argumentNotNull(concept, "concept may not be null");
67
68         return writeJsonString(concept, rootConceptClass);
69     }
70
71     /**
72      * Write a concept to a JSON string.
73      *
74      * @param concept The concept to write
75      * @param rootConceptClass The concept class
76      * @return The string with the concept
77      * @throws ApexException thrown on errors
78      */
79     public String writeJsonString(final C concept, final Class<C> rootConceptClass) throws ApexException {
80         LOGGER.debug("running writeJSONString . . .");
81
82         final ApexModelWriter<C> conceptWriter = new ApexModelWriter<>(rootConceptClass);
83         conceptWriter.setValidate(validate);
84
85         try (var baOutputStream = new ByteArrayOutputStream()) {
86             conceptWriter.write(concept, baOutputStream);
87             return baOutputStream.toString();
88         } catch (final Exception e) {
89             LOGGER.warn("error writing JSON string", e);
90             throw new ApexException("error writing JSON string", e);
91         }
92
93     }
94 }