Code Refactoring Ncmp* to Lcm* as per new scope
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / event / lcm / LcmEventsCreator.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.lcm;
22
23 import java.time.ZonedDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.util.List;
26 import java.util.UUID;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
29 import org.onap.ncmp.cmhandle.lcm.event.Event;
30 import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent;
31 import org.springframework.stereotype.Component;
32
33
34 /**
35  * LcmEventsCreator to create LcmEvent based on relevant operation.
36  */
37 @Slf4j
38 @Component
39 public class LcmEventsCreator {
40
41
42     /**
43      * Populate NcmpEvent.
44      *
45      * @param cmHandleId          Cm Handle Identifier
46      * @param ncmpServiceCmHandle Ncmp CmHandle Data
47      * @return Populated NcmpEvent
48      */
49     public NcmpEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
50         return createLcmEvent(cmHandleId, ncmpServiceCmHandle);
51     }
52
53     private NcmpEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
54         final NcmpEvent ncmpEvent = lcmEventHeader(cmHandleId);
55         ncmpEvent.setEvent(lcmEventPayload(cmHandleId, ncmpServiceCmHandle));
56         return ncmpEvent;
57     }
58
59     private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle ncmpServiceCmHandle) {
60         final Event event = new Event();
61         event.setCmHandleId(eventCorrelationId);
62         event.setCmhandleState(
63                 Event.CmhandleState.fromValue(ncmpServiceCmHandle.getCompositeState().getCmHandleState().toString()));
64         event.setCmhandleProperties(List.of(ncmpServiceCmHandle.getPublicProperties()));
65         return event;
66     }
67
68     private NcmpEvent lcmEventHeader(final String eventCorrelationId) {
69         final NcmpEvent ncmpEvent = new NcmpEvent();
70         ncmpEvent.setEventId(UUID.randomUUID().toString());
71         ncmpEvent.setEventCorrelationId(eventCorrelationId);
72         ncmpEvent.setEventTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
73         ncmpEvent.setEventSource("org.onap.ncmp");
74         ncmpEvent.setEventType("org.onap.ncmp.cmhandle-lcm-event");
75         ncmpEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event:v1");
76         return ncmpEvent;
77     }
78
79 }