HUB Resource
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / HubResource.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.apis.hub;
17
18 import org.onap.nbi.apis.hub.model.Subscriber;
19 import org.onap.nbi.apis.hub.model.Subscription;
20 import org.onap.nbi.apis.hub.service.SubscriptionService;
21 import org.onap.nbi.commons.JsonRepresentation;
22 import org.onap.nbi.commons.MultiCriteriaRequestBuilder;
23 import org.onap.nbi.commons.ResourceManagement;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.data.mongodb.core.MongoTemplate;
28 import org.springframework.data.mongodb.core.query.Query;
29 import org.springframework.http.HttpHeaders;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.scheduling.annotation.EnableScheduling;
34 import org.springframework.util.MultiValueMap;
35 import org.springframework.web.bind.annotation.*;
36 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
37
38 import java.net.URI;
39 import java.util.List;
40
41 @RestController
42 @RequestMapping("/hub")
43 @EnableScheduling
44 public class HubResource extends ResourceManagement {
45
46     Logger logger = LoggerFactory.getLogger(HubResource.class);
47
48     @Autowired
49     MongoTemplate mongoTemplate;
50
51     @Autowired
52     SubscriptionService subscriptionService;
53
54     @Autowired
55     MultiCriteriaRequestBuilder multiCriteriaRequestBuilder;
56
57     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
58     public ResponseEntity<Subscriber> createEventSubscription(@RequestBody Subscription subscription) {
59         logger.debug("POST request for subscription : {}", subscription);
60
61         Subscriber subscriber = subscriptionService.createSubscription(subscription);
62
63         URI location = ServletUriComponentsBuilder
64                 .fromCurrentRequest()
65                 .path("/{id}")
66                 .buildAndExpand(subscriber.getId())
67                 .toUri();
68
69         return ResponseEntity.created(location).build();
70     }
71
72     @GetMapping(value = "/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE)
73     public ResponseEntity<Subscription> getSubscription(@PathVariable String subscriptionId) {
74
75         Subscriber subscriber  = subscriptionService.findSubscriptionById(subscriptionId);
76         if (subscriber == null) {
77             return ResponseEntity.notFound().build();
78         }
79         return ResponseEntity.ok(Subscription.createFromSubscriber(subscriber));
80     }
81
82     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
83     public ResponseEntity<Object> findSubscribers(@RequestParam MultiValueMap<String, String> params) {
84
85         Query query = multiCriteriaRequestBuilder.buildRequest(params);
86         List<Subscriber> subscribers = mongoTemplate.find(query, Subscriber.class);
87         JsonRepresentation filter = new JsonRepresentation(params);
88         long totalCount = subscriptionService.countSubscription();
89         HttpHeaders headers = new HttpHeaders();
90         headers.add("X-Total-Count", String.valueOf(totalCount));
91         headers.add("X-Result-Count", String.valueOf(subscribers.size()));
92
93         return this.findResponse(subscribers, filter, headers);
94
95     }
96
97     @DeleteMapping("/{subscriptionId}")
98     @ResponseStatus(HttpStatus.NO_CONTENT)
99     public void deleteSubscription(@PathVariable String subscriptionId) {
100         logger.debug("DELETE request for subscription id #{}", subscriptionId);
101         subscriptionService.deleteSubscription(subscriptionId);
102     }
103 }