2 * Copyright (c) 2019 Vodafone Group
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.nbi.apis.listener;
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;
34 import java.util.concurrent.ConcurrentHashMap;
36 public class ListenerResourceTarget {
37 private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory());
40 private SubscriberRepository subscriberRepository;
42 private NotifierService notifier;
44 Logger logger = LoggerFactory.getLogger(ListenerResourceTarget.class);
46 static Map<String, JsonNode> events = new ConcurrentHashMap<>();
49 listener resource test for hub resource
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());
58 Event eventListener = mapper.treeToValue(event, Event.class);
60 .findSubscribersUsingEvent(eventListener)
61 .forEach(sub -> notifier.run(sub, eventListener));
64 logger.error("listener not called " + " ," + e.getMessage());
65 return ResponseEntity.badRequest().build();
68 return ResponseEntity.ok().build();