e64c27a66997536aa508b1c0c31b5119e9f1c6d5
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / subscription / SubscriptionServiceProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.subscription;
24
25 import org.apache.camel.Exchange;
26 import org.apache.camel.component.restlet.RestletConstants;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.cl.mdc.MdcContext;
30 import org.onap.aai.restclient.client.OperationResult;
31 import org.onap.aai.sparky.logging.AaiUiMsgs;
32 import org.onap.aai.sparky.subscription.services.SubscriptionService;
33 import org.onap.aai.sparky.util.NodeUtils;
34 import org.restlet.Request;
35 import org.restlet.Response;
36 import org.restlet.data.ClientInfo;
37 import org.restlet.data.MediaType;
38 import org.restlet.data.Status;
39
40 public class SubscriptionServiceProcessor {
41
42   private static final String EMPTY_RESPONSE = "{}";
43
44   private static final Logger LOG =
45       LoggerFactory.getInstance().getLogger(SubscriptionServiceProcessor.class);
46
47   SubscriptionService subService;
48
49
50   public SubscriptionServiceProcessor(SubscriptionService subscriptionService) {
51     this.subService = subscriptionService;
52   }
53
54
55   public void getSubscription(Exchange exchange) {
56
57     Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
58     if (xTransactionId == null) {
59       xTransactionId = NodeUtils.getRandomTxnId();
60     }
61
62     Object partnerName = exchange.getIn().getHeader("X-FromAppId");
63     if (partnerName == null) {
64       partnerName = "Browser";
65     }
66
67     Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
68
69     /*
70      * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log
71      * entry which includes client (e.g. browser) information
72      */
73     request.setLoggable(false);
74
75     ClientInfo clientInfo = request.getClientInfo();
76     MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName,
77         clientInfo.getAddress() + ":" + clientInfo.getPort());
78
79     Response response =
80         exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
81
82     OperationResult subscriptionResult = null;
83     Status responseStatus = null;
84
85     try {
86
87       subscriptionResult = subService.buildSubscriptionPayload();
88       responseStatus = Status.SUCCESS_OK;
89
90     } catch (Exception exc) {
91       responseStatus = Status.SERVER_ERROR_INTERNAL;
92       subscriptionResult = new OperationResult();
93       subscriptionResult.setResult(EMPTY_RESPONSE);
94       LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST,
95           "Exception thrown during subscription processing: " + exc.getLocalizedMessage());
96     }
97
98
99     response.setStatus(responseStatus);
100     response.setEntity(subscriptionResult.getResult(), MediaType.APPLICATION_JSON);
101     exchange.getOut().setBody(response);
102
103   }
104 }