7447e670467e53d19a5af3f89db74c8508ba0035
[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.datamodel.NetworkDiscoveryResponse;
38 import org.onap.sdnc.apps.pomba.networkdiscovery.service.SpringService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class RestServiceImpl implements RestService {
46
47     private static Logger log = LoggerFactory.getLogger(RestService.class);
48
49     @Autowired
50     private SpringService service;
51
52     @Resource(name="basicAuthHeader")
53     private String basicAuthHeader;
54
55     @Override
56     public Response findbyResourceIdAndType(HttpServletRequest request,
57                                             String version,
58                                             String authorization,
59                                             String fromAppId,
60                                             String transactionId,
61                                             String requestId,
62                                             String resourceType,
63                                             List<String> resourceIds,
64                                             String notificationURL) throws ApplicationException {
65
66         ONAPLogAdapter adapter = new ONAPLogAdapter(log);
67         adapter.getServiceDescriptor().setServiceName(SERVICE_NAME);
68         adapter.entering(request);
69         try {
70
71             if (version == null) {
72                 // only unit tests can pass null here
73                 // url matching will guarantee non-null in real server
74                 version = "v1";
75             }
76
77             if (authorization == null || !this.basicAuthHeader.equals(authorization)) {
78                 throw new ApplicationException(UNAUTHORIZED, Status.UNAUTHORIZED);
79             }
80             if ((fromAppId == null) || fromAppId.trim().isEmpty()) {
81                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, ONAPLogConstants.Headers.PARTNER_NAME);
82             }
83             if (requestId == null || requestId.isEmpty()) {
84                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "requestId");
85             }
86             if (notificationURL == null) {
87                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "notificationURL");
88             }
89             if (resourceType == null || resourceType.isEmpty()) {
90                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceType");
91             }
92             if (resourceIds == null || resourceIds.isEmpty()) {
93                 throw new ApplicationException(MISSING_PARAM, Status.BAD_REQUEST, "resourceIds");
94             }
95
96             if (transactionId == null || transactionId.isEmpty()) {
97                 transactionId = UUID.randomUUID().toString();
98                 log.debug("transactionId is missing; using newly generated value: " + transactionId);
99             }
100
101             // just reuse received Authorization header in callback
102             NetworkDiscoveryResponse response = this.service.findbyResourceIdAndType(transactionId,
103                     requestId, resourceType, resourceIds, notificationURL, authorization, adapter);
104             adapter.getResponseDescriptor().setResponseStatus(ResponseStatus.COMPLETED);
105             return Response.ok(response).build();
106
107         } catch (ApplicationException x) {
108             adapter.getResponseDescriptor()
109                     .setResponseCode(x.getResponseCode())
110                     .setResponseStatus(ResponseStatus.ERROR);
111             log.error(x.getMessage(), x);
112             ResponseBuilder builder = Response.status(x.getHttpStatus()).entity(x.getMessage());
113             if (authorization == null) {
114                 builder.header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"network-discovery\"");
115             }
116             return builder.build();
117
118         } catch (Exception x) {
119             adapter.getResponseDescriptor()
120                     .setResponseCode(GENERAL_FAILURE.getResponseCode())
121                     .setResponseStatus(ResponseStatus.ERROR);
122             log.error(GENERAL_FAILURE.getMessage(x), x);
123             return Response.status(Status.INTERNAL_SERVER_ERROR).entity(x.getMessage()).build();
124         } finally {
125             adapter.exiting();
126         }
127     }
128 }