*/
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
SpringApplication.run(Application.class, args);
}
- @Bean
- CriteriaBuilder criteriaBuilder() {
- return new CriteriaBuilderServiceOrder();
- }
-
}
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);
+ }
}
private LocalDateTime eventDate;
@NotNull
- private String eventType;
+ private String eventType = "string";
@NotNull
private JsonNode event;
*/
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;
@Document
-public class Subscriber {
+public class Subscriber implements Resource {
private static final Logger logger = LoggerFactory.getLogger(Subscriber.class);
@Id
*/
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;
}
- 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;
}
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;
}
}
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);
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;
--- /dev/null
+/**
+ * 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();
+ }
+
+}
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;
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) {
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);
* 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;