2 * ============LICENSE_START===================================================
3 * Copyright (c) 2018 Amdocs
4 * ============================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=====================================================
19 package org.onap.sdnc.apps.pomba.servicedecomposition.service.rs;
21 import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.*;
22 import java.util.UUID;
23 import javax.annotation.Resource;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.core.HttpHeaders;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.Response.ResponseBuilder;
28 import javax.ws.rs.core.Response.Status;
29 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
30 import org.onap.logging.ref.slf4j.ONAPLogConstants;
31 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
32 import org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException;
33 import org.onap.sdnc.apps.pomba.servicedecomposition.service.SpringService;
34 import org.onap.sdnc.apps.pomba.servicedecomposition.util.RestUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
41 public class RestServiceImpl implements RestService {
42 private static Logger log = LoggerFactory.getLogger(RestServiceImpl.class);
43 private static final String EMPTY_JSON_OBJECT = "{}";
44 private static UUID instanceId = UUID.randomUUID();
47 private SpringService service;
49 @Resource(name="basicAuthHeader")
50 private String basicAuthHeader;
53 public Response getContext(HttpServletRequest request,
57 String serviceInstanceId) {
59 ONAPLogAdapter adapter = new ONAPLogAdapter(log);
60 adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
61 adapter.getServiceDescriptor().setServiceUUID(instanceId.toString());
62 adapter.entering(request);
64 if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
65 throw new DiscoveryException(UNAUTHORIZED, Status.UNAUTHORIZED);
68 // Do some validation on Http headers and URL parameters
69 if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
70 throw new DiscoveryException(MISSING_HEADER, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
72 if (transactionId == null || transactionId.isEmpty()) {
73 transactionId = UUID.randomUUID().toString();
74 log.debug("{} is missing; using newly generated value: {}", ONAPLogConstants.Headers.REQUEST_ID, transactionId);
76 RestUtil.validateURL(serviceInstanceId);
77 String context = service.decomposeService(fromAppId, transactionId, serviceInstanceId, adapter);
79 if (context == null) {
80 context = EMPTY_JSON_OBJECT;
82 adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
83 return Response.ok().entity(context).build();
85 } catch (DiscoveryException x) {
86 log.error(x.getHttpStatus().getReasonPhrase(), x);
87 adapter.getResponseDescriptor()
88 .setResponseCode(x.getResponseCode())
89 .setResponseStatus(ResponseStatus.ERROR);
91 ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
92 if (authorization == null) {
93 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\""+ SERVICE_NAME + "\"");
95 return builder.build();
97 } catch (Exception e) {
98 log.error(Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), e);
99 adapter.getResponseDescriptor()
100 .setResponseCode(GENERAL_FAILURE.getResponseCode())
101 .setResponseStatus(ResponseStatus.ERROR);
103 return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();