2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.nbi.apis.hub.service;
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.MappingJsonFactory;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import org.onap.nbi.apis.hub.model.Event;
23 import org.onap.nbi.apis.hub.model.EventType;
24 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
25 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
26 import org.onap.nbi.commons.JacksonFilter;
27 import org.onap.nbi.commons.JsonRepresentation;
29 import java.util.Date;
30 import java.util.UUID;
32 public class EventFactory {
34 private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory());
36 public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem) {
37 Event event = new Event();
38 event.setEventId(UUID.randomUUID().toString());
39 event.setEventDate(new Date());
40 event.setEventType(eventType.value());
42 JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder));
44 if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) {
45 JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem);
46 ((ObjectNode)serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson);
49 event.setEvent(serviceOrderJson);
56 * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification
60 private static Object filterServiceOrder(final ServiceOrder serviceOrder) {
62 Object filteredServiceOrder = null;
64 if (serviceOrder != null) {
65 JsonRepresentation jsonRepresentation = new JsonRepresentation();
66 jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate").add
67 ("completionDateTime").add("orderItem");
69 filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation);
72 return filteredServiceOrder;