54e9001627ce9f63da3d557369506f6164db21bf
[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.repository.SubscriberRepository;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.http.MediaType;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.scheduling.annotation.EnableScheduling;
27 import org.springframework.web.bind.annotation.PostMapping;
28 import org.springframework.web.bind.annotation.RequestBody;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.bind.annotation.RestController;
31 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
32
33 import java.net.URI;
34
35 @RestController
36 @RequestMapping("/hub")
37 @EnableScheduling
38 public class HubResource {
39
40     Logger logger = LoggerFactory.getLogger(HubResource.class);
41
42     @Autowired
43     SubscriberRepository subscriberRepository;
44
45     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
46     public ResponseEntity<Subscriber> createEventSubscription(@RequestBody Subscription subscription) {
47         logger.debug("Received subscription request: {}", subscription);
48
49         Subscriber sub = Subscriber.createFromRequest(subscription);
50         sub = subscriberRepository.save(sub);
51
52         URI location = ServletUriComponentsBuilder
53                 .fromCurrentRequest()
54                 .path("{id}")
55                 .buildAndExpand(sub.getId())
56                 .toUri();
57
58         return ResponseEntity.created(location).build();
59     }
60 }