HUB Resource 89/56589/3
authorNicolasLaplaud <nicolas.laplaud@orange.com>
Tue, 17 Jul 2018 08:32:42 +0000 (10:32 +0200)
committerQuoc-Nghia Nguyen <quocnghia.nguyen@orange.com>
Wed, 18 Jul 2018 14:49:44 +0000 (16:49 +0200)
- Adding DELETE, GET, FIND operations on subscriptions

Change-Id: If0c326ffc6a3d0ec456e5258a04b3110bb8df583
Issue-ID: EXTAPI-96
Signed-off-by: NicolasLaplaud <nicolas.laplaud@orange.com>
src/main/java/org/onap/nbi/Application.java
src/main/java/org/onap/nbi/apis/hub/HubResource.java
src/main/java/org/onap/nbi/apis/hub/model/Event.java
src/main/java/org/onap/nbi/apis/hub/model/Subscriber.java
src/main/java/org/onap/nbi/apis/hub/model/Subscription.java
src/main/java/org/onap/nbi/apis/hub/service/CriteriaBuilderServiceOrder.java
src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java [new file with mode: 0644]
src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java
src/main/java/org/onap/nbi/commons/MultiCriteriaRequestBuilder.java [moved from src/main/java/org/onap/nbi/apis/serviceorder/MultiCriteriaRequestBuilder.java with 87% similarity]
src/main/java/org/onap/nbi/configuration/HubConfig.java [moved from src/main/java/org/onap/nbi/apis/hub/HubConfig.java with 96% similarity]

index e9fcc2d..05588b4 100644 (file)
  */
 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();
-    }
-
 }
index 54e9001..811dd51 100755 (executable)
@@ -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);
+    }
 }
index e68d322..4c8ae79 100755 (executable)
@@ -27,7 +27,7 @@ public class Event {
     private LocalDateTime eventDate;
 
     @NotNull
-    private String eventType;
+    private String eventType = "string";
 
     @NotNull
     private JsonNode event;
index a18833e..afb9472 100755 (executable)
@@ -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
index 70b50b4..53f33ab 100755 (executable)
  */
 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;
     }
 }
index 31f0839..4a780a5 100755 (executable)
@@ -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 (file)
index 0000000..65eaea3
--- /dev/null
@@ -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();
+    }
+
+}
index d439306..be28f1a 100644 (file)
@@ -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;
  *     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) {
@@ -59,6 +60,20 @@ public class MultiCriteriaRequestBuilder {
             LOGGER.debug("add criterion description {}", description);
             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);
@@ -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;