Merge "Fix sonar code smells"
[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.CmhandleState.READY;
24 import static org.onap.ncmp.cmhandle.lcm.event.Event.Operation.DELETE;
25
26 import java.time.ZonedDateTime;
27 import java.time.format.DateTimeFormatter;
28 import java.util.List;
29 import java.util.UUID;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
32 import org.onap.ncmp.cmhandle.lcm.event.Event;
33 import org.onap.ncmp.cmhandle.lcm.event.Event.Operation;
34 import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent;
35 import org.springframework.stereotype.Component;
36
37
38 /**
39  * NcmpEventsCreator to create NcmpEvent based on relevant operation.
40  */
41 @Slf4j
42 @Component
43 public class NcmpEventsCreator {
44
45
46     /**
47      * Populate NcmpEvent.
48      *
49      * @param cmHandleId          Cm Handle Identifier
50      * @param operation           Relevant Operation
51      * @param ncmpServiceCmHandle Ncmp CmHandle Data
52      * @return Populated NcmpEvent
53      */
54     public NcmpEvent populateNcmpEvent(final String cmHandleId, final Operation operation,
55             final NcmpServiceCmHandle ncmpServiceCmHandle) {
56         return createNcmpEvent(cmHandleId, operation, ncmpServiceCmHandle);
57     }
58
59     private NcmpEvent createNcmpEvent(final String cmHandleId, final Operation operation,
60             final NcmpServiceCmHandle ncmpServiceCmHandle) {
61         final NcmpEvent ncmpEvent = ncmpEventHeader(cmHandleId);
62         ncmpEvent.setEvent(ncmpEventPayload(cmHandleId, operation, ncmpServiceCmHandle));
63         return ncmpEvent;
64     }
65
66     private Event ncmpEventPayload(final String eventCorrelationId, final Operation operation,
67             final NcmpServiceCmHandle ncmpServiceCmHandle) {
68         final Event event = new Event();
69         event.setOperation(operation);
70         event.setCmHandleId(eventCorrelationId);
71
72         if (!DELETE.equals(operation)) {
73             event.setCmhandleState(READY);
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 }