add the gap event transformer
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / policy / GapEventTransformer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.datarouter.policy;
22
23 import java.io.FileNotFoundException;
24
25 import org.apache.camel.Exchange;
26 import org.json.JSONException;
27 import org.json.JSONObject;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.datarouter.logging.EntityEventPolicyMsgs;
31
32
33
34 public class GapEventTransformer {
35   protected Logger logger;
36   protected static final String additionalInfo = "Response of GapEvent to transform";
37
38   protected enum ResponseType {
39     SUCCESS, PARTIAL_SUCCESS, FAILURE;
40   }
41
42   public GapEventTransformer() throws FileNotFoundException {
43     LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance();
44     logger = loggerFactoryInstance.getLogger(AbstractSpikeEntityEventProcessor.class.getName());
45   }
46
47
48   public void process(Exchange exchange) throws Exception {
49
50     String payload = getExchangeBody(exchange);
51     JSONObject newPayload = transformToSpikePattern(payload);
52     exchange.getOut().setBody(newPayload.toString());
53
54   }
55
56   protected JSONObject transformToSpikePattern(String payload) {
57     JSONObject payloadJson = new JSONObject(payload);
58     JSONObject payloadEntity = payloadJson.getJSONObject("entity");
59     JSONObject payloadProperties = payloadEntity.getJSONObject("properties");
60     String payloadId = payloadEntity.getString("id");
61     String payloadType = payloadEntity.getString("type");
62
63     JSONObject newPayload = new JSONObject();
64     newPayload.put("operation", "UPDATE");
65     JSONObject newPayloadVertex = new JSONObject();
66     newPayloadVertex.put("key", payloadId);
67     newPayloadVertex.put("type", payloadType);
68     newPayloadVertex.put("properties", payloadProperties);
69     newPayload.put("vertex", newPayloadVertex);
70     return newPayload;
71   }
72
73   protected String getExchangeBody(Exchange exchange) {
74     String uebPayload = exchange.getIn().getBody().toString();
75     if (uebPayload == null || !isJSONValid(uebPayload)) {
76       uebPayload = exchange.getIn().getBody(String.class);
77       if (uebPayload == null || !isJSONValid(uebPayload)) {
78         returnWithError(exchange, uebPayload, "Invalid Payload");
79         return null;
80       }
81     }
82     return uebPayload;
83   }
84
85   private boolean isJSONValid(String test) {
86     try {
87       new JSONObject(test);
88     } catch (JSONException ex) {
89       return false;
90     }
91     return true;
92   }
93
94   protected void returnWithError(Exchange exchange, String payload, String errorMsg) {
95     logger.error(EntityEventPolicyMsgs.DISCARD_EVENT_NONVERBOSE, errorMsg);
96     logger.debug(EntityEventPolicyMsgs.DISCARD_EVENT_VERBOSE, errorMsg, payload);
97     setResponse(exchange, ResponseType.FAILURE, additionalInfo);
98   }
99
100   protected void setResponse(Exchange exchange, ResponseType responseType, String additionalInfo) {
101
102     exchange.getOut().setHeader("ResponseType", responseType.toString());
103     exchange.getOut().setBody(additionalInfo);
104   }
105 }