diff options
author | NicolasLaplaud <nicolas.laplaud@orange.com> | 2018-07-17 10:32:42 +0200 |
---|---|---|
committer | Quoc-Nghia Nguyen <quocnghia.nguyen@orange.com> | 2018-07-18 16:49:44 +0200 |
commit | 9dc24ae6efc0d070ae446b87c6268bb3cd353034 (patch) | |
tree | 9fa843e72c78f432af443fedd1bb9b117b616491 /src/main/java | |
parent | 04ec0d3d8dbce788be1ec1fec405bd77c0d39bf4 (diff) |
HUB Resource
- Adding DELETE, GET, FIND operations on subscriptions
Change-Id: If0c326ffc6a3d0ec456e5258a04b3110bb8df583
Issue-ID: EXTAPI-96
Signed-off-by: NicolasLaplaud <nicolas.laplaud@orange.com>
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/onap/nbi/Application.java | 8 | ||||
-rwxr-xr-x | src/main/java/org/onap/nbi/apis/hub/HubResource.java | 67 | ||||
-rwxr-xr-x | src/main/java/org/onap/nbi/apis/hub/model/Event.java | 2 | ||||
-rwxr-xr-x | src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java | 3 | ||||
-rwxr-xr-x | src/main/java/org/onap/nbi/apis/hub/model/Subscription.java | 36 | ||||
-rwxr-xr-x | src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java | 8 | ||||
-rw-r--r-- | src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java | 47 | ||||
-rw-r--r-- | src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java | 1 | ||||
-rw-r--r-- | src/main/java/org/onap/nbi/commons/MultiCriteriaRequestBuilder.java (renamed from src/main/java/org/onap/nbi/apis/serviceorder/MultiCriteriaRequestBuilder.java) | 27 | ||||
-rw-r--r-- | src/main/java/org/onap/nbi/configuration/HubConfig.java (renamed from src/main/java/org/onap/nbi/apis/hub/HubConfig.java) | 2 |
10 files changed, 154 insertions, 47 deletions
diff --git a/src/main/java/org/onap/nbi/Application.java b/src/main/java/org/onap/nbi/Application.java index e9fcc2d..05588b4 100644 --- a/src/main/java/org/onap/nbi/Application.java +++ b/src/main/java/org/onap/nbi/Application.java @@ -15,11 +15,8 @@ */ package org.onap.nbi; -import org.onap.nbi.apis.hub.service.CriteriaBuilder; -import org.onap.nbi.apis.hub.service.CriteriaBuilderServiceOrder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @@ -30,9 +27,4 @@ public class Application { SpringApplication.run(Application.class, args); } - @Bean - CriteriaBuilder criteriaBuilder() { - return new CriteriaBuilderServiceOrder(); - } - } diff --git a/src/main/java/org/onap/nbi/apis/hub/HubResource.java b/src/main/java/org/onap/nbi/apis/hub/HubResource.java index 54e9001..811dd51 100755 --- a/src/main/java/org/onap/nbi/apis/hub/HubResource.java +++ b/src/main/java/org/onap/nbi/apis/hub/HubResource.java @@ -17,44 +17,87 @@ package org.onap.nbi.apis.hub; import org.onap.nbi.apis.hub.model.Subscriber; import org.onap.nbi.apis.hub.model.Subscription; -import org.onap.nbi.apis.hub.repository.SubscriberRepository; +import org.onap.nbi.apis.hub.service.SubscriptionService; +import org.onap.nbi.commons.JsonRepresentation; +import org.onap.nbi.commons.MultiCriteriaRequestBuilder; +import org.onap.nbi.commons.ResourceManagement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.net.URI; +import java.util.List; @RestController @RequestMapping("/hub") @EnableScheduling -public class HubResource { +public class HubResource extends ResourceManagement { Logger logger = LoggerFactory.getLogger(HubResource.class); @Autowired - SubscriberRepository subscriberRepository; + MongoTemplate mongoTemplate; + + @Autowired + SubscriptionService subscriptionService; + + @Autowired + MultiCriteriaRequestBuilder multiCriteriaRequestBuilder; @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Subscriber> createEventSubscription(@RequestBody Subscription subscription) { - logger.debug("Received subscription request: {}", subscription); + logger.debug("POST request for subscription : {}", subscription); - Subscriber sub = Subscriber.createFromRequest(subscription); - sub = subscriberRepository.save(sub); + Subscriber subscriber = subscriptionService.createSubscription(subscription); URI location = ServletUriComponentsBuilder .fromCurrentRequest() - .path("{id}") - .buildAndExpand(sub.getId()) + .path("/{id}") + .buildAndExpand(subscriber.getId()) .toUri(); return ResponseEntity.created(location).build(); } + + @GetMapping(value = "/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Subscription> getSubscription(@PathVariable String subscriptionId) { + + Subscriber subscriber = subscriptionService.findSubscriptionById(subscriptionId); + if (subscriber == null) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok(Subscription.createFromSubscriber(subscriber)); + } + + @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<Object> findSubscribers(@RequestParam MultiValueMap<String, String> params) { + + Query query = multiCriteriaRequestBuilder.buildRequest(params); + List<Subscriber> subscribers = mongoTemplate.find(query, Subscriber.class); + JsonRepresentation filter = new JsonRepresentation(params); + long totalCount = subscriptionService.countSubscription(); + HttpHeaders headers = new HttpHeaders(); + headers.add("X-Total-Count", String.valueOf(totalCount)); + headers.add("X-Result-Count", String.valueOf(subscribers.size())); + + return this.findResponse(subscribers, filter, headers); + + } + + @DeleteMapping("/{subscriptionId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteSubscription(@PathVariable String subscriptionId) { + logger.debug("DELETE request for subscription id #{}", subscriptionId); + subscriptionService.deleteSubscription(subscriptionId); + } } diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Event.java b/src/main/java/org/onap/nbi/apis/hub/model/Event.java index e68d322..4c8ae79 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Event.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Event.java @@ -27,7 +27,7 @@ public class Event { private LocalDateTime eventDate; @NotNull - private String eventType; + private String eventType = "string"; @NotNull private JsonNode event; diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java b/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java index a18833e..afb9472 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java @@ -15,6 +15,7 @@ */ package org.onap.nbi.apis.hub.model; +import org.onap.nbi.commons.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.annotation.Id; @@ -26,7 +27,7 @@ import java.util.stream.Stream; @Document -public class Subscriber { +public class Subscriber implements Resource { private static final Logger logger = LoggerFactory.getLogger(Subscriber.class); @Id diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java index 70b50b4..53f33ab 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java @@ -15,11 +15,13 @@ */ package org.onap.nbi.apis.hub.model; -import java.util.Objects; +import java.util.stream.Collectors; public class Subscription { + private String id; + private String callback; private String query; @@ -28,11 +30,20 @@ public class Subscription { } - public Subscription(String callback, String query) { + public Subscription(String id, String callback, String query) { + this.id = id; this.callback = callback; this.query = query; } + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + public String getCallback() { return callback; } @@ -49,18 +60,17 @@ public class Subscription { this.query = query; } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Subscription that = (Subscription) o; - return Objects.equals(callback, that.callback) && - Objects.equals(query, that.query); - } + public static Subscription createFromSubscriber(Subscriber subscriber) { + Subscription sub = new Subscription(); + sub.setId(subscriber.getId()); + sub.setCallback(subscriber.getCallback()); - @Override - public int hashCode() { + String query = subscriber.getQuery().entrySet() + .stream() + .map(entry -> entry.getKey()+ "=" + String.join(" ",entry.getValue())) + .collect(Collectors.joining()); - return Objects.hash(callback, query); + sub.setQuery(query); + return sub; } } diff --git a/src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java b/src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java index 31f0839..4a780a5 100755 --- a/src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java @@ -34,10 +34,9 @@ public class CriteriaBuilderServiceOrder implements CriteriaBuilder { if (stateNode.isValueNode()) return base.orOperator( Criteria.where("query.serviceOrder__state").exists(false), - Criteria.where("query.serviceOrder__state").in(event.getEvent().path("state").textValue()) + Criteria.where("query.serviceOrder__state").in(stateNode.textValue()) ); - else - return base.and("query.serviceOrder__state").exists(false); + break; case "ServiceOrderItemStateChangeNotification": Object[] states = getStates(event); @@ -46,8 +45,7 @@ public class CriteriaBuilderServiceOrder implements CriteriaBuilder { Criteria.where("query.serviceOrder__serviceOrderItem__state").exists(false), Criteria.where("query.serviceOrder__serviceOrderItem__state").in(states) ); - else - return base.and("query.serviceOrder__serviceOrderItem__state").exists(false); + break; } return base; diff --git a/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java b/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java new file mode 100644 index 0000000..65eaea3 --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2018 Orange + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.nbi.apis.hub.service; + +import org.onap.nbi.apis.hub.model.Subscriber; +import org.onap.nbi.apis.hub.model.Subscription; +import org.onap.nbi.apis.hub.repository.SubscriberRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class SubscriptionService { + + @Autowired + SubscriberRepository subscriberRepository; + + public Subscriber findSubscriptionById(String subscriptionId){ + return subscriberRepository.findOne(subscriptionId); + } + + public Subscriber createSubscription(Subscription subscription){ + Subscriber sub = Subscriber.createFromRequest(subscription); + return subscriberRepository.save(sub); + } + + public void deleteSubscription(String subscriptionId){ + subscriberRepository.delete(subscriptionId); + } + + public long countSubscription(){ + return subscriberRepository.count(); + } + +} diff --git a/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java b/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java index d439306..be28f1a 100644 --- a/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java +++ b/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java @@ -24,6 +24,7 @@ import org.onap.nbi.apis.serviceorder.workflow.CreateAAICustomerManager; import org.onap.nbi.apis.serviceorder.workflow.CreateAAIServiceTypeManager; import org.onap.nbi.apis.serviceorder.workflow.SOTaskManager; import org.onap.nbi.commons.JsonRepresentation; +import org.onap.nbi.commons.MultiCriteriaRequestBuilder; import org.onap.nbi.commons.ResourceManagement; import org.onap.nbi.exceptions.ValidationException; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/org/onap/nbi/apis/serviceorder/MultiCriteriaRequestBuilder.java b/src/main/java/org/onap/nbi/commons/MultiCriteriaRequestBuilder.java index 7ea2fd4..968f1dd 100644 --- a/src/main/java/org/onap/nbi/apis/serviceorder/MultiCriteriaRequestBuilder.java +++ b/src/main/java/org/onap/nbi/commons/MultiCriteriaRequestBuilder.java @@ -13,12 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.nbi.apis.serviceorder; +package org.onap.nbi.commons; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; import org.onap.nbi.apis.serviceorder.model.StateType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,10 +26,15 @@ import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.MultiValueMap; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + @Service public class MultiCriteriaRequestBuilder { - private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class); + private static final Logger LOGGER = LoggerFactory.getLogger(MultiCriteriaRequestBuilder.class); public Query buildRequest(MultiValueMap<String, String> params) { @@ -60,6 +61,20 @@ public class MultiCriteriaRequestBuilder { query.addCriteria(Criteria.where("description").is(description)); } + List<String> eventTypes = params.get("query.eventType"); + if (!CollectionUtils.isEmpty(eventTypes)) { + Object[] eventType = new String[]{eventTypes.get(0)}; + LOGGER.debug("add criterion query.eventType {}", eventType); + query.addCriteria(Criteria.where("query.eventType").in(eventType)); + + } + List<String> callbacks = params.get("callback"); + if (!CollectionUtils.isEmpty(callbacks)) { + String callback = callbacks.get(0); + LOGGER.debug("add criterion callback {}", callback); + query.addCriteria(Criteria.where("callback").is(callback)); + + } handleDate(params, query); diff --git a/src/main/java/org/onap/nbi/apis/hub/HubConfig.java b/src/main/java/org/onap/nbi/configuration/HubConfig.java index 693e628..9d86dd6 100644 --- a/src/main/java/org/onap/nbi/apis/hub/HubConfig.java +++ b/src/main/java/org/onap/nbi/configuration/HubConfig.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.nbi.apis.hub; +package org.onap.nbi.configuration; import org.onap.nbi.apis.hub.service.CriteriaBuilder; import org.onap.nbi.apis.hub.service.CriteriaBuilderServiceOrder; |