org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.mso;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.glassfish.jersey.client.ClientResponse;
26 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
27
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30
31 import static org.onap.vid.utils.Logging.getMethodName;
32
33 /**
34  * The Class MsoUtil.
35  */
36 public class MsoUtil {
37         
38         /** The logger. */
39         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoUtil.class);
40         
41         /** The Constant dateFormat. */
42         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
43         
44         /**
45          * Wrap response.
46          *
47          * @param body the body
48          * @param statusCode the status code
49          * @return the mso response wrapper
50          */
51         public static MsoResponseWrapper wrapResponse ( String body, int statusCode ) {
52                 
53                 MsoResponseWrapper w = new MsoResponseWrapper();
54                 w.setStatus (statusCode);
55                 w.setEntity(body);
56                 
57                 return w;
58         }
59
60         /**
61          * Wrap response.
62          *
63          * @param cres the cres
64          * @return the mso response wrapper
65          */
66         public static MsoResponseWrapper wrapResponse (ClientResponse cres) {   
67                 String resp_str = "";
68                 if ( cres != null ) {
69                         resp_str = cres.readEntity(String.class);
70                 }
71                 int statuscode = cres.getStatus();
72                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, statuscode );
73                 return (w);
74         }
75         
76         /**
77          * Wrap response.
78          *
79          * @param rs the rs
80          * @return the mso response wrapper
81          */
82         public static MsoResponseWrapper wrapResponse (RestObject<String> rs) {
83                 String resp_str = null;
84                 int status = 0;
85                 if ( rs != null ) {
86                         resp_str = rs.get() != null ? rs.get() : rs.getRaw();
87                         status = rs.getStatusCode();
88                 }
89                 MsoResponseWrapper w = MsoUtil.wrapResponse ( resp_str, status );
90                 return (w);
91         }       
92         
93         /**
94          * Convert pojo to string.
95          *
96          * @param <T> the generic type
97          * @param t the t
98          * @return the string
99          * @throws JsonProcessingException the json processing exception
100          */
101         public static <T> String convertPojoToString ( T t ) {
102                 
103                 String methodName = "convertPojoToString";
104                 ObjectMapper mapper = new ObjectMapper();
105                 String r_json_str = "";
106             if ( t != null ) {
107                     try {
108                         r_json_str = mapper.writeValueAsString(t);
109                     }
110                     catch ( com.fasterxml.jackson.core.JsonProcessingException j ) {
111                         logger.debug(EELFLoggerDelegate.debugLogger,getMethodName() + " Unable to parse object of type " + t.getClass().getName() + " as json", j);
112                     }
113             }
114             return (r_json_str);
115         }
116         
117         /**
118          * The main method.
119          *
120          * @param args the arguments
121          */
122         public static void main(String[] args) {
123                 // TODO Auto-generated method stub
124
125         }
126 }