Adding interfaces in documentation
[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-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.sparky.subscription;
22
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;
37
38 public class SubscriptionServiceProcessor {
39
40   private static final String EMPTY_RESPONSE = "{}";
41
42   private static final Logger LOG =
43       LoggerFactory.getInstance().getLogger(SubscriptionServiceProcessor.class);
44
45   SubscriptionService subService;
46
47
48   public SubscriptionServiceProcessor(SubscriptionService subscriptionService) {
49     this.subService = subscriptionService;
50   }
51
52
53   public void getSubscription(Exchange exchange) {
54
55     Object xTransactionId = exchange.getIn().getHeader("X-TransactionId");
56     if (xTransactionId == null) {
57       xTransactionId = NodeUtils.getRandomTxnId();
58     }
59
60     Object partnerName = exchange.getIn().getHeader("X-FromAppId");
61     if (partnerName == null) {
62       partnerName = "Browser";
63     }
64
65     Request request = exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
66
67     /*
68      * Disables automatic Apache Camel Restlet component logging which prints out an undesirable log
69      * entry which includes client (e.g. browser) information
70      */
71     request.setLoggable(false);
72
73     ClientInfo clientInfo = request.getClientInfo();
74     MdcContext.initialize((String) xTransactionId, "AAI-UI", "", (String) partnerName,
75         clientInfo.getAddress() + ":" + clientInfo.getPort());
76
77     Response response =
78         exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
79
80     OperationResult subscriptionResult = null;
81     Status responseStatus = null;
82
83     try {
84
85       subscriptionResult = subService.buildSubscriptionPayload();
86       responseStatus = Status.SUCCESS_OK;
87
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());
94     }
95
96
97     response.setStatus(responseStatus);
98     response.setEntity(subscriptionResult.getResult(), MediaType.APPLICATION_JSON);
99     exchange.getOut().setBody(response);
100
101   }
102 }