8b8e50f3375ac0dbc520135355dcf86554aaed08
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / listener / ListenerResourceTarget.java
1 /**
2  * Copyright (c) 2019 Vodafone Group
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
17 package org.onap.nbi.apis.listener;
18
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.MappingJsonFactory;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import org.onap.nbi.apis.hub.repository.SubscriberRepository;
23 import org.onap.nbi.apis.hub.service.NotifierService;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.http.MediaType;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.web.bind.annotation.PostMapping;
30 import org.onap.nbi.apis.hub.model.Event;
31 import org.springframework.web.bind.annotation.RequestBody;
32
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35
36 public class ListenerResourceTarget {
37     private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory());
38
39     @Autowired
40     private SubscriberRepository subscriberRepository;
41     @Autowired
42     private NotifierService notifier;
43
44     Logger logger = LoggerFactory.getLogger(ListenerResourceTarget.class);
45
46     static Map<String, JsonNode> events = new ConcurrentHashMap<>();
47
48     /*
49         listener resource test for hub resource
50      */
51     @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
52     public ResponseEntity<Object> postListenerResource(@RequestBody JsonNode event) {
53         if (logger.isDebugEnabled()) {
54             logger.debug("POST event from nbi : {}", event.toString());
55
56         }
57         try {
58             Event eventListener = mapper.treeToValue(event, Event.class);
59             subscriberRepository
60                     .findSubscribersUsingEvent(eventListener)
61                     .forEach(sub -> notifier.run(sub, eventListener));
62         }
63         catch(Exception e){
64             logger.error("listener not called " + " ," + e.getMessage());
65             return ResponseEntity.badRequest().build();
66         }
67
68         return ResponseEntity.ok().build();
69     }
70 }
71