2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.subscription;
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;
40 public class SubscriptionServiceProcessor {
42 private static final String EMPTY_RESPONSE = "{}";
44 private static final Logger LOG =
45 LoggerFactory.getInstance().getLogger(SubscriptionServiceProcessor.class);
47 SubscriptionService subService;
50 public SubscriptionServiceProcessor(SubscriptionService subscriptionService) {
51 this.subService = subscriptionService;
55 public void getSubscription(Exchange exchange) {
57 Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
58 if (xTransactionId == null) {
59 xTransactionId = NodeUtils.getRandomTxnId();
62 Object partnerName = exchange.getIn().getHeader("X-FromAppId");
63 if (partnerName == null) {
64 partnerName = "Browser";
67 Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
70 * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log
71 * entry which includes client (e.g. browser) information
73 request.setLoggable(false);
75 ClientInfo clientInfo = request.getClientInfo();
76 MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName,
77 clientInfo.getAddress() + ":" + clientInfo.getPort());
80 exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
82 OperationResult subscriptionResult = null;
83 Status responseStatus = null;
87 subscriptionResult = subService.buildSubscriptionPayload();
88 responseStatus = Status.SUCCESS_OK;
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());
99 response.setStatus(responseStatus);
100 response.setEntity(subscriptionResult.getResult(), MediaType.APPLICATION_JSON);
101 exchange.getOut().setBody(response);