Merge the POMBA code to ONAP AAI data router
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / POAAuditEvent.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.onap.aai.datarouter.entity;\r
22 \r
23 import com.google.gson.Gson;\r
24 import com.google.gson.GsonBuilder;\r
25 import java.util.ArrayList;\r
26 import java.util.List;\r
27 import javax.ws.rs.core.Response.Status;\r
28 import org.onap.aai.cl.api.Logger;\r
29 import org.onap.aai.cl.eelf.LoggerFactory;\r
30 import org.onap.aai.datarouter.exception.POAAuditException;\r
31 import org.onap.aai.datarouter.logging.DataRouterMsgs;\r
32 \r
33 /**\r
34  * Wrapper class for POMBA rest call message body\r
35  *\r
36  */\r
37 public class POAAuditEvent\r
38 {\r
39     private static final String ATTR_SERVICE_INST_LIST = "serviceInstanceList";\r
40 \r
41     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();\r
42     private List<POAServiceInstanceEntity> serviceInstanceList = new ArrayList<POAServiceInstanceEntity>();\r
43 \r
44     private static Logger logger = LoggerFactory.getInstance().getLogger(POAAuditEvent.class.getName());\r
45 \r
46     public String toJson() {\r
47         return gson.toJson(this);\r
48     }\r
49 \r
50     public static POAAuditEvent fromJson(String payload) throws POAAuditException {\r
51        if (payload == null || payload.isEmpty()) {\r
52             throw new POAAuditException("Invalid Json Payload", Status.BAD_REQUEST);\r
53         }\r
54 \r
55         try {\r
56             return gson.fromJson(payload, POAAuditEvent.class);\r
57         } catch (Exception ex) {\r
58             logger.debug("Invalid Json Payload: " + payload);\r
59             throw new POAAuditException("Invalid Json Payload", Status.BAD_REQUEST, DataRouterMsgs.BAD_REST_REQUEST, ex.getMessage());\r
60         }\r
61     }\r
62 \r
63     public List<POAServiceInstanceEntity> getServiceInstanceList() {\r
64         return serviceInstanceList;\r
65     }\r
66 \r
67     public void setServiceInstanceList(List<POAServiceInstanceEntity> instances) {\r
68         this.serviceInstanceList = instances;\r
69     }\r
70 \r
71 \r
72     /**\r
73      * Validates this service instance list\r
74      * @throws POAAuditException if the list is absent or empty\r
75      */\r
76     public void validate() throws POAAuditException {\r
77 \r
78         if((serviceInstanceList == null) || serviceInstanceList.isEmpty()) {\r
79             String error = "Missing attribute list: " + ATTR_SERVICE_INST_LIST;\r
80             throw new POAAuditException(error, Status.BAD_REQUEST, DataRouterMsgs.BAD_REST_REQUEST, error);\r
81         }\r
82     }\r
83 \r
84     @Override\r
85     public String toString() {\r
86         return "AuditEvent [serviceInstances=" + serviceInstanceList + "]";\r
87     }\r
88 }\r