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