Merge "Add functionality to rAPP Catalogue"
[nonrtric.git] / r-app-catalogue / src / main / java / org / oransc / rappcatalogue / api / ServicesApiDelegateImpl.java
1 /*-\r
2  * ========================LICENSE_START=================================\r
3  * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
4  * ======================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ========================LICENSE_END===================================\r
17  */\r
18 \r
19 package org.oransc.rappcatalogue.api;\r
20 \r
21 import java.io.IOException;\r
22 import java.sql.Date;\r
23 import java.util.ArrayList;\r
24 import java.util.List;\r
25 import java.util.Optional;\r
26 import java.util.concurrent.ConcurrentHashMap;\r
27 import javax.servlet.http.HttpServletRequest;\r
28 import javax.servlet.http.HttpServletResponse;\r
29 import org.oransc.rappcatalogue.exception.HeaderException;\r
30 import org.oransc.rappcatalogue.exception.InvalidServiceException;\r
31 import org.oransc.rappcatalogue.exception.ServiceNotFoundException;\r
32 import org.oransc.rappcatalogue.model.InputService;\r
33 import org.oransc.rappcatalogue.model.Service;\r
34 import org.springframework.beans.factory.annotation.Autowired;\r
35 import org.springframework.http.HttpStatus;\r
36 import org.springframework.http.ResponseEntity;\r
37 import org.springframework.web.context.request.NativeWebRequest;\r
38 \r
39 @org.springframework.stereotype.Service\r
40 public class ServicesApiDelegateImpl implements ServicesApiDelegate {\r
41 \r
42     private static final String LOCATION_HEADER = "Location";\r
43 \r
44     @Autowired\r
45     private NativeWebRequest nativeWebRequest;\r
46 \r
47     private ConcurrentHashMap<String, Service> registeredServices = new ConcurrentHashMap<>();\r
48 \r
49     ServicesApiDelegateImpl(NativeWebRequest nativeWebRequest) {\r
50         this.nativeWebRequest = nativeWebRequest;\r
51     }\r
52 \r
53     @Override\r
54     public Optional<NativeWebRequest> getRequest() {\r
55         return Optional.of(nativeWebRequest);\r
56     }\r
57 \r
58     @Override\r
59     public ResponseEntity<Service> getIndividualService(String serviceName) {\r
60         Service service = registeredServices.get(serviceName);\r
61         if (service != null) {\r
62             return ResponseEntity.ok(service);\r
63         } else {\r
64             throw new ServiceNotFoundException(serviceName);\r
65         }\r
66     }\r
67 \r
68     @Override\r
69     public ResponseEntity<List<Service>> getServices() {\r
70         return ResponseEntity.ok(new ArrayList<>(registeredServices.values()));\r
71     }\r
72 \r
73     @Override\r
74     public ResponseEntity<Void> putIndividualService(String serviceName, InputService inputService) {\r
75         if (isServiceValid(inputService)) {\r
76             if (registeredServices.put(serviceName, createService(serviceName, inputService)) == null) {\r
77                 try {\r
78                     getRequest().ifPresent(request -> addLocationHeaderToResponse(serviceName, request));\r
79                 } catch (Exception e) {\r
80                     registeredServices.remove(serviceName);\r
81                     throw e;\r
82                 }\r
83                 return new ResponseEntity<>(HttpStatus.CREATED);\r
84             } else {\r
85                 return new ResponseEntity<>(HttpStatus.OK);\r
86             }\r
87         } else {\r
88             throw new InvalidServiceException();\r
89         }\r
90     }\r
91 \r
92     private void addLocationHeaderToResponse(String serviceName, NativeWebRequest request) {\r
93         try {\r
94             HttpServletRequest nativeRequest = request.getNativeRequest(HttpServletRequest.class);\r
95             HttpServletResponse nativeResponse = request.getNativeResponse(HttpServletResponse.class);\r
96             if (nativeRequest != null && nativeResponse != null) {\r
97                 StringBuffer requestURL = nativeRequest.getRequestURL();\r
98                 nativeResponse.addHeader(LOCATION_HEADER, requestURL.toString());\r
99                 nativeResponse.getWriter().print("");\r
100             } else {\r
101                 throw new HeaderException(LOCATION_HEADER, new Exception("Native Request or Response missing"));\r
102             }\r
103         } catch (IOException e) {\r
104             throw new HeaderException(LOCATION_HEADER, e);\r
105         }\r
106     }\r
107 \r
108     @Override\r
109     public ResponseEntity<Void> deleteIndividualService(String serviceName) {\r
110         registeredServices.remove(serviceName);\r
111         return new ResponseEntity<>(HttpStatus.NO_CONTENT);\r
112     }\r
113 \r
114     /*\r
115      * java:S2589: Boolean expressions should not be gratuitous.\r
116      * Even though the version property is marked as @NotNull, it might be null coming from the client, hence the null\r
117      * check is needed.\r
118      */\r
119     @SuppressWarnings("java:S2589")\r
120     private boolean isServiceValid(InputService service) {\r
121         String version = service.getVersion();\r
122         return version != null && !version.isBlank();\r
123     }\r
124 \r
125     private Service createService(String serviceName, InputService inputService) {\r
126         Service service = new Service();\r
127         service.setName(serviceName);\r
128         service.setDescription(inputService.getDescription());\r
129         service.setDisplayName(inputService.getDisplayName());\r
130         service.setVersion(inputService.getVersion());\r
131         service.setRegistrationDate(getTodaysDate());\r
132         return service;\r
133     }\r
134 \r
135     private String getTodaysDate() {\r
136         long millis = System.currentTimeMillis();\r
137         Date date = new Date(millis);\r
138         return date.toString();\r
139     }\r
140 }\r