2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.policy;
23 import java.io.FileNotFoundException;
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;
34 public class GapEventTransformer {
35 protected Logger logger;
36 protected static final String additionalInfo = "Response of GapEvent to transform";
38 protected enum ResponseType {
39 SUCCESS, PARTIAL_SUCCESS, FAILURE;
42 public GapEventTransformer() throws FileNotFoundException {
43 LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance();
44 logger = loggerFactoryInstance.getLogger(AbstractSpikeEntityEventProcessor.class.getName());
48 public void process(Exchange exchange) throws Exception {
50 String payload = getExchangeBody(exchange);
51 JSONObject newPayload = transformToSpikePattern(payload);
52 exchange.getOut().setBody(newPayload.toString());
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");
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);
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");
85 private boolean isJSONValid(String test) {
88 } catch (JSONException ex) {
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);
100 protected void setResponse(Exchange exchange, ResponseType responseType, String additionalInfo) {
102 exchange.getOut().setHeader("ResponseType", responseType.toString());
103 exchange.getOut().setBody(additionalInfo);