19734f078f670120185bf947436e06ec8bfc8c64
[sdnc/apps.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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=====================================================
17  */
18 package org.onap.sdnc.apps.pomba.servicedecomposition.service.rs;
19
20 import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.*;
21
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;
39
40 @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
45     @Autowired
46     private SpringService service;
47
48     @Resource(name="basicAuthHeader")
49     private String basicAuthHeader;
50
51     public RestServiceImpl() {}
52
53     @Override
54     public Response getContext(HttpServletRequest request,
55                                String authorization,
56                                String fromAppId,
57                                String transactionId,
58                                String serviceInstanceId) {
59
60         ONAPLogAdapter adapter = new ONAPLogAdapter(log);
61         adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
62         adapter.entering(request);
63         try {
64             if (!this.basicAuthHeader.equals(authorization)) {
65                 throw new DiscoveryException(UNAUTHORIZED, Status.UNAUTHORIZED);
66             }
67
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);
71             }
72             if (transactionId == null || transactionId.isEmpty()) {
73                 transactionId = UUID.randomUUID().toString();
74                 log.debug(ONAPLogConstants.Headers.REQUEST_ID+ " is missing; using newly generated value: " + transactionId);
75             }
76             RestUtil.validateURL(serviceInstanceId);
77             String context = service.decomposeService(fromAppId, transactionId, serviceInstanceId, adapter);
78
79             if (context == null) {
80                 context = EMPTY_JSON_OBJECT;
81             }
82             adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
83             return Response.ok().entity(context).build();
84
85         } catch (DiscoveryException x) {
86             adapter.getResponseDescriptor()
87                     .setResponseCode(x.getResponseCode())
88                     .setResponseStatus(ResponseStatus.ERROR);
89
90             ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
91             if (authorization == null) {
92                 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\""+ SERVICE_NAME + "\"");
93             }
94             return builder.build();
95
96         } catch (Exception e) {
97             adapter.getResponseDescriptor()
98                     .setResponseCode(GENERAL_FAILURE.getResponseCode())
99                     .setResponseStatus(ResponseStatus.ERROR);
100
101             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
102         } finally {
103             adapter.exiting();
104         }
105
106     }
107
108 }