Merge "Publish LCM Events"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / event / NcmpEventsCreator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Nordix Foundation
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.event;
22
23 import static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.DELETE;
24
25 import java.time.ZonedDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.util.List;
28 import java.util.UUID;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
31 import org.onap.ncmp.cmhandle.lcm.event.Event;
32 import org.onap.ncmp.cmhandle.lcm.event.Event.Operation;
33 import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent;
34 import org.springframework.stereotype.Component;
35
36
37 /**
38  * NcmpEventsCreator to create NcmpEvent based on relevant operation.
39  */
40 @Slf4j
41 @Component
42 public class NcmpEventsCreator {
43
44
45     /**
46      * Populate NcmpEvent.
47      *
48      * @param cmHandleId          Cm Handle Identifier
49      * @param operation           Relevant Operation
50      * @param ncmpServiceCmHandle Ncmp CmHandle Data
51      * @return Populated NcmpEvent
52      */
53     public NcmpEvent populateNcmpEvent(final String cmHandleId, final Operation operation,
54             final NcmpServiceCmHandle ncmpServiceCmHandle) {
55         return createNcmpEvent(cmHandleId, operation, ncmpServiceCmHandle);
56     }
57
58     private NcmpEvent createNcmpEvent(final String cmHandleId, final Operation operation,
59             final NcmpServiceCmHandle ncmpServiceCmHandle) {
60         final NcmpEvent ncmpEvent = ncmpEventHeader(cmHandleId);
61         ncmpEvent.setEvent(ncmpEventPayload(cmHandleId, operation, ncmpServiceCmHandle));
62         return ncmpEvent;
63     }
64
65     private Event ncmpEventPayload(final String eventCorrelationId, final Operation operation,
66             final NcmpServiceCmHandle ncmpServiceCmHandle) {
67         final Event event = new Event();
68         event.setOperation(operation);
69         event.setCmHandleId(eventCorrelationId);
70
71         if (!DELETE.equals(operation)) {
72             event.setCmhandleState(Event.CmhandleState.fromValue(
73                     ncmpServiceCmHandle.getCompositeState().getCmHandleState().toString()));
74             event.setCmhandleProperties(List.of(ncmpServiceCmHandle.getPublicProperties()));
75         }
76         return event;
77     }
78
79     private NcmpEvent ncmpEventHeader(final String eventCorrelationId) {
80         final NcmpEvent ncmpEvent = new NcmpEvent();
81         ncmpEvent.setEventId(UUID.randomUUID().toString());
82         ncmpEvent.setEventCorrelationId(eventCorrelationId);
83         ncmpEvent.setEventTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
84         ncmpEvent.setEventSource("org.onap.ncmp");
85         ncmpEvent.setEventType("org.onap.ncmp.cmhandle-lcm-event");
86         ncmpEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event:v1");
87         return ncmpEvent;
88     }
89
90 }