BpGen refactor Code Quality Issue-ID: DCAEGEN2-2502
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / main / java / org / onap / blueprintgenerator / service / common / ComponentSpecService.java
1 /*
2  *
3  *  * ============LICENSE_START=======================================================
4  *  *  org.onap.dcae
5  *  *  ================================================================================
6  *  *  Copyright (c) 2020  AT&T Intellectual Property. All rights reserved.
7  *  *  ================================================================================
8  *  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  *  you may not use this file except in compliance with the License.
10  *  *  You may obtain a copy of the License at
11  *  *
12  *  *       http://www.apache.org/licenses/LICENSE-2.0
13  *  *
14  *  *  Unless required by applicable law or agreed to in writing, software
15  *  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  *  See the License for the specific language governing permissions and
18  *  *  limitations under the License.
19  *  *  ============LICENSE_END=========================================================
20  *
21  *
22  */
23
24 package org.onap.blueprintgenerator.service.common;
25
26 import java.io.IOException;
27 import org.onap.blueprintgenerator.exception.ComponentSpecException;
28 import org.onap.blueprintgenerator.model.componentspec.OnapComponentSpec;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Qualifier;
32 import org.springframework.stereotype.Service;
33
34 import java.io.File;
35
36 /**
37  * @author : Ravi Mantena
38  * @date 10/16/2020 Application: ONAP - Blueprint Generator Common ONAP Service used by ONAP and
39  * DMAAP Blueprint to create Component Spec from File
40  */
41 @Service("onapComponentSpecService")
42 public class ComponentSpecService {
43
44     @Qualifier("objectMapper")
45     @Autowired
46     private ObjectMapper componentMapper;
47
48     @Qualifier("yamlObjectMapper")
49     @Autowired
50     private ObjectMapper yamlComponentMapper;
51
52     /**
53      * Creates ComponentSpec from given file path and validates if the input is json file or not
54      *
55      * @param componentSpecPath Path of Component Spec File Location
56      * @return
57      */
58     public OnapComponentSpec createComponentSpecFromFile(String componentSpecPath) {
59         OnapComponentSpec componentSpec;
60         try {
61             if (!componentSpecPath.endsWith(".json")) {
62                 componentSpec = yamlComponentMapper.readValue(new File(componentSpecPath), OnapComponentSpec.class);
63             }else{
64                 componentSpec = componentMapper.readValue(new File(componentSpecPath), OnapComponentSpec.class);
65             }
66         } catch (Exception ex) {
67             throw new ComponentSpecException(
68                 "Unable to create ONAP Component Spec from the input file: " + componentSpecPath,
69                 ex);
70         }
71         return componentSpec;
72     }
73
74     /**
75      * Creates the component spec from string.
76      * This method is used by RuntimeAPI
77      * @param specString the spec string of Component Spec
78      * @return
79      */
80     public OnapComponentSpec createComponentSpecFromString(String specString) {
81         OnapComponentSpec componentSpec;
82         try {
83             componentSpec = componentMapper.readValue(specString, OnapComponentSpec.class);
84         } catch (Exception ex) {
85             throw new ComponentSpecException(
86                 "Unable to create ONAP Component Spec from the input string: " + specString,
87                 ex);
88         }
89         return componentSpec;
90     }
91
92 }