diff options
author | NicolasLaplaud <nicolas.laplaud@orange.com> | 2018-07-20 10:16:49 +0200 |
---|---|---|
committer | NicolasLaplaud <nicolas.laplaud@orange.com> | 2018-07-20 10:27:48 +0200 |
commit | 048e7e70e305279fc9cdcba4ae70d116f1f1c8b6 (patch) | |
tree | e735cea12e55265d1f47e2b779527fde7e2be959 | |
parent | 9dc24ae6efc0d070ae446b87c6268bb3cd353034 (diff) |
HUB Resource2.1.0
- manage notification based on subscription
- manage event body based on subscriptionType
Change-Id: Ibaf3de69419ca20a5c5a53d97be107311237d7cc
Issue-ID: EXTAPI-96
Signed-off-by: NicolasLaplaud <nicolas.laplaud@orange.com>
7 files changed, 163 insertions, 25 deletions
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 4c8ae79..cf6c790 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 @@ -18,16 +18,16 @@ package org.onap.nbi.apis.hub.model; import com.fasterxml.jackson.databind.JsonNode; import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; +import java.util.Date; public class Event { - private String eventId; - private LocalDateTime eventDate; + private java.lang.String eventId; + private Date eventDate; @NotNull - private String eventType = "string"; + private String eventType; @NotNull private JsonNode event; @@ -40,22 +40,14 @@ public class Event { this.eventId = eventId; } - public LocalDateTime getEventDate() { + public Date getEventDate() { return eventDate; } - public void setEventDate(LocalDateTime eventDate) { + public void setEventDate(Date eventDate) { this.eventDate = eventDate; } - public String getEventType() { - return eventType; - } - - public void setEventType(String eventType) { - this.eventType = eventType; - } - public JsonNode getEvent() { return event; } @@ -63,4 +55,12 @@ public class Event { public void setEvent(JsonNode event) { this.event = event; } + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } } diff --git a/src/main/java/org/onap/nbi/apis/hub/model/EventType.java b/src/main/java/org/onap/nbi/apis/hub/model/EventType.java new file mode 100644 index 0000000..1702347 --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/model/EventType.java @@ -0,0 +1,55 @@ +/** + * 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.model; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +public enum EventType { + + SERVICE_ORDER_CREATION("ServiceOrderCreationNotification"), + + SERVICE_ORDER_STATE_CHANGE("ServiceOrderStateChangeNotification"), + + SERVICE_ORDER_ITEM_STATE_CHANGE("ServiceOrderItemStateChangeNotification"); + + private String value; + + EventType(String value) { + this.value = value; + } + + @Override + public String toString() { + return String.valueOf(value); + } + + @JsonCreator + public static EventType fromValue(String text) { + for (EventType b : EventType.values()) { + if (String.valueOf(b.value).equals(text)) { + return b; + } + } + return null; + } + + @JsonValue + public String value() { + return this.value; + } + +} 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 afb9472..df0cffd 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 @@ -63,7 +63,7 @@ public class Subscriber implements Resource { Stream.of(request.getQuery().split("&")) .map(q -> q.split("=")) .filter(q -> q.length == 2) - .forEach(q -> sub.getQuery().put(q[0], q[1].split(","))); + .forEach(q -> sub.getQuery().put(q[0].trim(), q[1].trim().split(","))); return sub; } diff --git a/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java b/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java new file mode 100644 index 0000000..8083fff --- /dev/null +++ b/src/main/java/org/onap/nbi/apis/hub/service/EventFactory.java @@ -0,0 +1,71 @@ +/** + * 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 com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.onap.nbi.apis.hub.model.Event; +import org.onap.nbi.apis.hub.model.EventType; +import org.onap.nbi.apis.serviceorder.model.ServiceOrder; +import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem; +import org.onap.nbi.commons.JacksonFilter; +import org.onap.nbi.commons.JsonRepresentation; + +import java.util.Date; +import java.util.UUID; + +public class EventFactory { + + private static final ObjectMapper mapper = new ObjectMapper(); + + public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem) { + Event event = new Event(); + event.setEventId(UUID.randomUUID().toString()); + event.setEventDate(new Date()); + event.setEventType(eventType.value()); + + JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder)); + + if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) { + JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem); + ((ObjectNode)serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson); + } + + event.setEvent(serviceOrderJson); + + return event; + } + + + /** + * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification + * @param serviceOrder + * @return + */ + private static Object filterServiceOrder(final ServiceOrder serviceOrder) { + + Object filteredServiceOrder; + + JsonRepresentation jsonRepresentation = new JsonRepresentation(); + jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate").add + ("completionDateTime").add("orderItem"); + + filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation); + + return filteredServiceOrder; + } +} diff --git a/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java b/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java index ad7ab81..cd242e8 100755 --- a/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/NotificationAspect.java @@ -18,8 +18,11 @@ package org.onap.nbi.apis.hub.service; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; +import org.onap.nbi.apis.hub.model.Event; +import org.onap.nbi.apis.hub.model.EventType; import org.onap.nbi.apis.hub.repository.SubscriberRepository; import org.onap.nbi.apis.serviceorder.model.ServiceOrder; +import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem; import org.onap.nbi.apis.serviceorder.model.StateType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Configurable; @@ -40,7 +43,7 @@ public class NotificationAspect { ".createServiceOrder(..))", returning = "serviceOrderCreated") public void whenCreateServiceOrder(ServiceOrder serviceOrderCreated) { if(StateType.ACKNOWLEDGED.equals(serviceOrderCreated.getState())) { - // Notif createServiceOrder + processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_CREATION, serviceOrderCreated, null)); } } @@ -49,7 +52,7 @@ public class NotificationAspect { public void whenUpdateServiceOrderState(ServiceOrder serviceOrderUpdated) { if(StateType.COMPLETED.equals(serviceOrderUpdated.getState())|| StateType.FAILED.equals(serviceOrderUpdated.getState())) { - // Notif updateServiceOrder + processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_STATE_CHANGE, serviceOrderUpdated, null)); } } @@ -59,9 +62,23 @@ public class NotificationAspect { Object[] signatureArgs = joinPoint.getArgs(); if(signatureArgs != null && signatureArgs.length == 3) { + ServiceOrder serviceOrder = (ServiceOrder) signatureArgs[0]; + ServiceOrderItem serviceOrderItem = (ServiceOrderItem) signatureArgs[1]; StateType serviceOrderItemState = (StateType) signatureArgs[2]; - // Notif updateServiceOrderItem + processEvent(EventFactory.getEvent(EventType.SERVICE_ORDER_ITEM_STATE_CHANGE, serviceOrder, + serviceOrderItem)); } } + + /** + * Retreive subscribers that match an event and fire notification + * asynchronously + * @param event + */ + private void processEvent(Event event) { + subscriberRepository + .findSubscribersUsingEvent(event) + .forEach(sub -> notifier.run(sub, event)); + } } 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 be28f1a..e3f4442 100644 --- a/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java +++ b/src/main/java/org/onap/nbi/apis/serviceorder/ServiceOrderResource.java @@ -118,7 +118,6 @@ public class ServiceOrderResource extends ResourceManagement { } ServiceOrder serviceOrderSaved =serviceOrderService.createServiceOrder(serviceOrder); - serviceOrderService.updateOrderHref(serviceOrderSaved); JsonRepresentation filter = new JsonRepresentation(params); return this.createResponse(serviceOrderSaved, filter); diff --git a/src/main/java/org/onap/nbi/apis/serviceorder/service/ServiceOrderService.java b/src/main/java/org/onap/nbi/apis/serviceorder/service/ServiceOrderService.java index 14b864a..bc8b854 100644 --- a/src/main/java/org/onap/nbi/apis/serviceorder/service/ServiceOrderService.java +++ b/src/main/java/org/onap/nbi/apis/serviceorder/service/ServiceOrderService.java @@ -58,15 +58,11 @@ public class ServiceOrderService { for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) { serviceOrderItem.setState(StateType.ACKNOWLEDGED); } - return serviceOrderRepository.save(serviceOrder); - } - - public void updateOrderHref(ServiceOrder serviceOrder){ + serviceOrder = serviceOrderRepository.save(serviceOrder); serviceOrder.setHref("serviceOrder/" + serviceOrder.getId()); - serviceOrderRepository.save(serviceOrder); + return serviceOrderRepository.save(serviceOrder); } - public void deleteServiceOrder(String serviceOrderId){ serviceOrderRepository.delete(serviceOrderId); } |