8083fff7412251198256476d6eeb7d424fe42dc2
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / EventFactory.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
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
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nbi.apis.hub.service;
17
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.node.ObjectNode;
21 import org.onap.nbi.apis.hub.model.Event;
22 import org.onap.nbi.apis.hub.model.EventType;
23 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
24 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
25 import org.onap.nbi.commons.JacksonFilter;
26 import org.onap.nbi.commons.JsonRepresentation;
27
28 import java.util.Date;
29 import java.util.UUID;
30
31 public class EventFactory {
32
33     private static final ObjectMapper mapper = new ObjectMapper();
34
35     public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem) {
36         Event event = new Event();
37         event.setEventId(UUID.randomUUID().toString());
38         event.setEventDate(new Date());
39         event.setEventType(eventType.value());
40
41         JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder));
42
43         if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) {
44             JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem);
45             ((ObjectNode)serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson);
46         }
47
48         event.setEvent(serviceOrderJson);
49
50         return event;
51     }
52
53
54     /**
55      * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification
56      * @param serviceOrder
57      * @return
58      */
59     private static Object filterServiceOrder(final ServiceOrder serviceOrder) {
60
61         Object filteredServiceOrder;
62
63         JsonRepresentation jsonRepresentation = new JsonRepresentation();
64         jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate").add
65                 ("completionDateTime").add("orderItem");
66
67         filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation);
68
69         return filteredServiceOrder;
70     }
71 }