2 * Copyright (c) 2019 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
13 package org.onap.nbi.api.listener;
15 import com.fasterxml.jackson.databind.JsonNode;
17 import java.util.ArrayList;
18 import java.util.Collection;
20 import java.util.concurrent.ConcurrentHashMap;
21 import org.apache.commons.lang3.StringUtils;
22 import org.onap.nbi.commons.ResourceManagement;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.http.MediaType;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.util.MultiValueMap;
28 import org.springframework.web.bind.annotation.DeleteMapping;
29 import org.springframework.web.bind.annotation.GetMapping;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.PostMapping;
32 import org.springframework.web.bind.annotation.RequestBody;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RequestParam;
35 import org.springframework.web.bind.annotation.RestController;
36 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
39 @RequestMapping("/test/listener")
40 public class ListenerResource extends ResourceManagement {
42 Logger logger = LoggerFactory.getLogger(ListenerResource.class);
44 static Map<String, JsonNode> events = new ConcurrentHashMap<>();
47 listener resource test for hub resource
49 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
50 public ResponseEntity<JsonNode> postListener(@RequestBody JsonNode event) {
51 if (logger.isDebugEnabled()) {
52 logger.debug("POST event from nbi : {}", event.toString());
54 String eventId = event.get("eventId").asText();
55 events.put(eventId, event);
57 URI location = ServletUriComponentsBuilder
60 .buildAndExpand(eventId)
63 return ResponseEntity.created(location).body(event);
67 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
68 public ResponseEntity<Collection<JsonNode>> findEvents(@RequestParam MultiValueMap<String, String> params) {
69 Collection<JsonNode> values = new ArrayList<>();
70 String serviceOrderId = params.getFirst("serviceOrderId");
71 if(StringUtils.isNotEmpty(serviceOrderId)) {
72 for (JsonNode jsonNode : events.values()) {
73 String id = jsonNode.get("event").get("id").asText();
74 logger.info("found event with service order id : "+id);
75 if(id.equals(serviceOrderId)) {
79 if(!values.isEmpty()) {
80 return ResponseEntity.ok(values);
82 logger.error("cannot found events with service order id : "+serviceOrderId);
83 return ResponseEntity.notFound().build();
86 values=events.values();
88 return ResponseEntity.ok(values);
91 @GetMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
92 public ResponseEntity<Object> getEvent(@PathVariable String eventId) {
94 return ResponseEntity.ok(events.get(eventId));
98 @DeleteMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
99 public ResponseEntity<Object> deleteEvent(@PathVariable String eventId) {
101 events.remove(eventId);
102 return ResponseEntity.noContent().build();
106 @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
107 public ResponseEntity<Object> deleteEvents() {
110 return ResponseEntity.noContent().build();