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.sparky.subscription;
23 import org.apache.camel.Exchange;
24 import org.apache.camel.component.restlet.RestletConstants;
25 import org.onap.aai.cl.api.Logger;
26 import org.onap.aai.cl.eelf.LoggerFactory;
27 import org.onap.aai.cl.mdc.MdcContext;
28 import org.onap.aai.restclient.client.OperationResult;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
30 import org.onap.aai.sparky.subscription.services.SubscriptionService;
31 import org.onap.aai.sparky.util.NodeUtils;
32 import org.restlet.Request;
33 import org.restlet.Response;
34 import org.restlet.data.ClientInfo;
35 import org.restlet.data.MediaType;
36 import org.restlet.data.Status;
38 public class SubscriptionServiceProcessor {
40 private static final String EMPTY_RESPONSE = "{}";
42 private static final Logger LOG =
43 LoggerFactory.getInstance().getLogger(SubscriptionServiceProcessor.class);
45 SubscriptionService subService;
48 public SubscriptionServiceProcessor(SubscriptionService subscriptionService) {
49 this.subService = subscriptionService;
53 public void getSubscription(Exchange exchange) {
55 Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
56 if (xTransactionId == null) {
57 xTransactionId = NodeUtils.getRandomTxnId();
60 Object partnerName = exchange.getIn().getHeader("X-FromAppId");
61 if (partnerName == null) {
62 partnerName = "Browser";
65 Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
68 * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log
69 * entry which includes client (e.g. browser) information
71 request.setLoggable(false);
73 ClientInfo clientInfo = request.getClientInfo();
74 MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName,
75 clientInfo.getAddress() + ":" + clientInfo.getPort());
78 exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
80 OperationResult subscriptionResult = null;
81 Status responseStatus = null;
85 subscriptionResult = subService.buildSubscriptionPayload();
86 responseStatus = Status.SUCCESS_OK;
88 } catch (Exception exc) {
89 responseStatus = Status.SERVER_ERROR_INTERNAL;
90 subscriptionResult = new OperationResult();
91 subscriptionResult.setResult(EMPTY_RESPONSE);
92 LOG.error(AaiUiMsgs.FAILURE_TO_PROCESS_REQUEST,
93 "Exception thrown during subscription processing: " + exc.getLocalizedMessage());
97 response.setStatus(responseStatus);
98 response.setEntity(subscriptionResult.getResult(), MediaType.APPLICATION_JSON);
99 exchange.getOut().setBody(response);