6840b82ac2869ce29234b93f2515622407420b2d
[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
19 package org.onap.sdnc.apps.pomba.servicedecomposition.service.rs;
20
21 import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.*;
22
23 import java.util.UUID;
24 import javax.annotation.Resource;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.core.HttpHeaders;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.ResponseBuilder;
29 import javax.ws.rs.core.Response.Status;
30 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
31 import org.onap.logging.ref.slf4j.ONAPLogConstants;
32 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
33 import org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException;
34 import org.onap.sdnc.apps.pomba.servicedecomposition.service.SpringService;
35 import org.onap.sdnc.apps.pomba.servicedecomposition.util.RestUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class RestServiceImpl implements RestService {
43     private static Logger log = LoggerFactory.getLogger(RestServiceImpl.class);
44     private static final String EMPTY_JSON_OBJECT = "{}";
45
46     @Autowired
47     private SpringService service;
48
49     @Resource(name="basicAuthHeader")
50     private String basicAuthHeader;
51
52     public RestServiceImpl() {}
53
54     @Override
55     public Response getContext(HttpServletRequest request,
56                                String authorization,
57                                String fromAppId,
58                                String transactionId,
59                                String serviceInstanceId) {
60
61         ONAPLogAdapter adapter = new ONAPLogAdapter(log);
62         adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
63         adapter.entering(request);
64         try {
65             if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
66                 throw new DiscoveryException(UNAUTHORIZED, Status.UNAUTHORIZED);
67             }
68
69             // Do some validation on Http headers and URL parameters
70             if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
71                 throw new DiscoveryException(MISSING_HEADER, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
72             }
73             if (transactionId == null || transactionId.isEmpty()) {
74                 transactionId = UUID.randomUUID().toString();
75                 log.debug(ONAPLogConstants.Headers.REQUEST_ID+ " is missing; using newly generated value: " + transactionId);
76             }
77             RestUtil.validateURL(serviceInstanceId);
78             String context = service.decomposeService(fromAppId, transactionId, serviceInstanceId, adapter);
79
80             if (context == null) {
81                 context = EMPTY_JSON_OBJECT;
82             }
83             adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
84             return Response.ok().entity(context).build();
85
86         } catch (DiscoveryException x) {
87             adapter.getResponseDescriptor()
88                     .setResponseCode(x.getResponseCode())
89                     .setResponseStatus(ResponseStatus.ERROR);
90
91             ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
92             if (authorization == null) {
93                 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\""+ SERVICE_NAME + "\"");
94             }
95             return builder.build();
96
97         } catch (Exception e) {
98             adapter.getResponseDescriptor()
99                     .setResponseCode(GENERAL_FAILURE.getResponseCode())
100                     .setResponseStatus(ResponseStatus.ERROR);
101
102             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
103         } finally {
104             adapter.exiting();
105         }
106     }
107 }