adb5df0ccbf727ab9aea0adc27bcfe24446f0d33
[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.networkdiscovery.service.rs;
20
21 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.GENERAL_FAILURE;
22 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.MISSING_PARAM;
23 import static org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException.Error.UNAUTHORIZED;
24
25 import java.util.List;
26 import java.util.UUID;
27 import javax.annotation.Resource;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.HttpHeaders;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.Response.ResponseBuilder;
32 import javax.ws.rs.core.Response.Status;
33 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
34 import org.onap.logging.ref.slf4j.ONAPLogConstants;
35 import org.onap.logging.ref.slf4j.ONAPLogConstants.ResponseStatus;
36 import org.onap.sdnc.apps.pomba.networkdiscovery.ApplicationException;
37 import org.onap.sdnc.apps.pomba.networkdiscovery.JerseyConfiguration;
38 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.NetworkDiscoveryResponse;
39 import org.onap.sdnc.apps.pomba.networkdiscovery.service.SpringService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class RestServiceImpl implements RestService {
47
48     private static Logger log = LoggerFactory.getLogger(RestService.class);
49
50     @Autowired
51     private SpringService service;
52
53     @Resource(name="basicAuthHeader")
54     private String basicAuthHeader;
55
56     @Override
57     public Response findbyResourceIdAndType(HttpServletRequest request,
58                                             String version,
59                                             String authorization,
60                                             String fromAppId,
61                                             String transactionId,
62                                             String requestId,
63                                             String resourceType,
64                                             List<String> resourceIds) throws ApplicationException {
65
66         ONAPLogAdapter adapter = new ONAPLogAdapter(log);
67
68         try {
69             adapter.getServiceDescriptor().setServiceName(JerseyConfiguration.SERVICE_NAME);
70             adapter.entering(request);
71
72             validateInput(version, authorization, fromAppId, requestId, resourceType, resourceIds);
73
74             if (transactionId == null || transactionId.isEmpty()) {
75                 transactionId = UUID.randomUUID().toString();
76                 log.debug("transactionId is missing; using newly generated value: {}", transactionId);
77             }
78
79             NetworkDiscoveryResponse response = this.service.findbyResourceIdAndType(transactionId,
80                     requestId, resourceType, resourceIds, adapter);
81             adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
82             Response returnedResponse = Response.ok(response).build();
83             adapter.unwrap().info("request at url = {} resulted in http status {} and response: {}", request.getServletPath(),
84                     returnedResponse.getStatus(), returnedResponse.getEntity());
85             return returnedResponse;
86
87         } catch (ApplicationException x) {
88             adapter.getResponseDescriptor()
89                     .setResponseCode(x.getResponseCode())
90                     .setResponseStatus(ResponseStatus.ERROR);
91             log.error(x.getMessage());
92             ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
93             if (authorization == null) {
94                 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"network-discovery\"");
95             }
96             return builder.build();
97
98         } catch (Exception x) {
99             adapter.getResponseDescriptor()
100                     .setResponseCode(GENERAL_FAILURE.getResponseCode())
101                     .setResponseStatus(ResponseStatus.ERROR);
102             log.error(GENERAL_FAILURE.getMessage(x), x);
103             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(x.getMessage()).build();
104         } finally {
105             adapter.exiting();
106         }
107     }
108
109     private void validateInput(String version, String authorization, String fromAppId, String requestId,
110             String resourceType, List<String> resourceIds) throws ApplicationException {
111         if (version == null) {
112             throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "version");
113         }
114
115         if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
116             throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);
117         }
118         if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
119             throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
120         }
121         if (requestId == null || requestId.isEmpty()) {
122             throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "requestId");
123         }
124
125         if (resourceType == null || resourceType.isEmpty()) {
126             throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceType");
127         }
128         if (resourceIds == null || resourceIds.isEmpty()) {
129             throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceIds");
130         }
131     }
132 }