2 * Copyright (c) 2019 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with 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
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
14 package org.onap.nbi.api.listener;
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;
37 import com.fasterxml.jackson.databind.JsonNode;
40 @RequestMapping("/test/listener")
41 public class ListenerResourceTest extends ResourceManagement {
43 Logger logger = LoggerFactory.getLogger(ListenerResourceTest.class);
45 static Map<String, JsonNode> events = new ConcurrentHashMap<>();
48 * listener resource test for hub resource
50 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
51 public ResponseEntity<JsonNode> postListener(@RequestBody JsonNode event) {
52 if (logger.isDebugEnabled()) {
53 logger.debug("POST event from nbi : {}", event.toString());
55 String eventId = event.get("eventId").asText();
56 logger.info("putting eventId {} in the events map", eventId);
57 events.put(eventId, event);
59 URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
60 .buildAndExpand(eventId).toUri();
62 return ResponseEntity.created(location).body(event);
66 @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
67 public ResponseEntity<Collection<JsonNode>> findEvents(
68 @RequestParam MultiValueMap<String, String> params) {
69 logger.info("called listener get with params {} : " + params.toString());
70 Collection<JsonNode> values = new ArrayList<>();
71 String serviceOrderId = params.getFirst("serviceOrderId");
72 String serviceInstanceId = params.getFirst("serviceInstanceId");
73 if (StringUtils.isNotEmpty(serviceOrderId)) {
74 for (JsonNode jsonNode : events.values()) {
75 String id = jsonNode.get("event").get("id").asText();
76 logger.info("found event with service order id : " + id);
77 if (id.equals(serviceOrderId)) {
81 if (!values.isEmpty()) {
82 return ResponseEntity.ok(values);
84 logger.error("cannot found events with service order id : " + serviceOrderId);
85 return ResponseEntity.notFound().build();
87 } else if (StringUtils.isNotEmpty(serviceInstanceId)) {
88 for (JsonNode jsonNode : events.values()) {
89 String id = jsonNode.get("event").get("id").asText();
90 logger.info("found event with service Instance id : " + id);
91 if (id.equals(serviceInstanceId)) {
95 if (!values.isEmpty()) {
96 return ResponseEntity.ok(values);
98 logger.error("cannot found events with service instance id : " + serviceInstanceId);
99 return ResponseEntity.notFound().build();
102 values = events.values();
104 return ResponseEntity.ok(values);
107 @GetMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
108 public ResponseEntity<Object> getEvent(@PathVariable String eventId) {
110 return ResponseEntity.ok(events.get(eventId));
114 @DeleteMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
115 public ResponseEntity<Object> deleteEvent(@PathVariable String eventId) {
117 events.remove(eventId);
118 return ResponseEntity.noContent().build();
122 @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
123 public ResponseEntity<Object> deleteEvents() {
126 return ResponseEntity.noContent().build();