aef1ed9bd0fbd4f745a2365f6737d740af8310eb
[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.util.UUID;
24 import lombok.Getter;
25 import lombok.NoArgsConstructor;
26 import lombok.Setter;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.ncmp.api.impl.utils.EventDateTimeFormatter;
29 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
30 import org.onap.ncmp.cmhandle.event.lcm.Event;
31 import org.onap.ncmp.cmhandle.event.lcm.LcmEvent;
32 import org.onap.ncmp.cmhandle.event.lcm.Values;
33 import org.springframework.stereotype.Component;
34
35
36 /**
37  * LcmEventsCreator to create LcmEvent based on relevant operation.
38  */
39 @Slf4j
40 @Component
41 public class LcmEventsCreator {
42
43     /**
44      * Populate Lifecycle Management Event.
45      *
46      * @param cmHandleId                  cm handle identifier
47      * @param targetNcmpServiceCmHandle   target ncmp service cmhandle
48      * @param existingNcmpServiceCmHandle existing ncmp service cmhandle
49      * @return Populated LcmEvent
50      */
51     public LcmEvent populateLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
52             final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
53         return createLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
54     }
55
56     private LcmEvent createLcmEvent(final String cmHandleId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
57             final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
58         final LcmEventType lcmEventType =
59                 LcmEventsCreatorHelper.determineEventType(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
60         final LcmEvent lcmEvent = lcmEventHeader(cmHandleId, lcmEventType);
61         lcmEvent.setEvent(
62                 lcmEventPayload(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle, lcmEventType));
63         return lcmEvent;
64     }
65
66     private Event lcmEventPayload(final String eventCorrelationId, final NcmpServiceCmHandle targetNcmpServiceCmHandle,
67             final NcmpServiceCmHandle existingNcmpServiceCmHandle, final LcmEventType lcmEventType) {
68         final Event event = new Event();
69         event.setCmHandleId(eventCorrelationId);
70         final CmHandleValuesHolder cmHandleValuesHolder =
71                 LcmEventsCreatorHelper.determineEventValues(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle,
72                         lcmEventType);
73         event.setOldValues(cmHandleValuesHolder.getOldValues());
74         event.setNewValues(cmHandleValuesHolder.getNewValues());
75
76         return event;
77     }
78
79     private LcmEvent lcmEventHeader(final String eventCorrelationId, final LcmEventType lcmEventType) {
80         final LcmEvent lcmEvent = new LcmEvent();
81         lcmEvent.setEventId(UUID.randomUUID().toString());
82         lcmEvent.setEventCorrelationId(eventCorrelationId);
83         lcmEvent.setEventTime(EventDateTimeFormatter.getCurrentDateTime());
84         lcmEvent.setEventSource("org.onap.ncmp");
85         lcmEvent.setEventType(lcmEventType.getEventType());
86         lcmEvent.setEventSchema("org.onap.ncmp:cmhandle-lcm-event");
87         lcmEvent.setEventSchemaVersion("1.0");
88         return lcmEvent;
89     }
90
91     @NoArgsConstructor
92     @Getter
93     @Setter
94     static class CmHandleValuesHolder {
95
96         private Values oldValues;
97         private Values newValues;
98     }
99
100 }