b8bd277dbc157897ed1a40ad8fee4054e8029fc1
[cps/ncmp-dmi-plugin.git] / src / main / java / org / onap / cps / ncmp / dmi / notifications / avc / DmiDataAvcCloudEventCreator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 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.dmi.notifications.avc;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import io.cloudevents.CloudEvent;
26 import io.cloudevents.core.builder.CloudEventBuilder;
27 import java.net.URI;
28 import java.time.format.DateTimeFormatter;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import lombok.AccessLevel;
34 import lombok.NoArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.onap.cps.ncmp.events.avc1_0_0.AvcEvent;
37 import org.onap.cps.ncmp.events.avc1_0_0.Data;
38 import org.onap.cps.ncmp.events.avc1_0_0.DatastoreChanges;
39 import org.onap.cps.ncmp.events.avc1_0_0.Edit;
40 import org.onap.cps.ncmp.events.avc1_0_0.IetfYangPatchYangPatch;
41 import org.onap.cps.ncmp.events.avc1_0_0.PushChangeUpdate;
42 import org.onap.cps.ncmp.events.avc1_0_0.Value;
43
44 /**
45  * Helper to create AvcEvents.
46  */
47 @Slf4j
48 @NoArgsConstructor(access = AccessLevel.PRIVATE)
49 public class DmiDataAvcCloudEventCreator {
50
51     private static final DateTimeFormatter dateTimeFormatter =
52             DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
53
54     private static final ObjectMapper objectMapper = new ObjectMapper();
55
56     /**
57      * Creates CloudEvent for DMI Data AVC.
58      *
59      * @param eventCorrelationId correlationid
60      * @return Cloud Event
61      */
62     public static CloudEvent createCloudEvent(final String eventCorrelationId) {
63
64         CloudEvent cloudEvent = null;
65
66         try {
67             cloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create("NCMP"))
68                     .withType(AvcEvent.class.getName())
69                     .withDataSchema(URI.create("urn:cps:" + AvcEvent.class.getName() + ":1.0.0"))
70                     .withExtension("correlationid", eventCorrelationId)
71                     .withData(objectMapper.writeValueAsBytes(createDmiDataAvcEvent())).build();
72         } catch (final JsonProcessingException jsonProcessingException) {
73             log.error("Unable to convert object to json : {}", jsonProcessingException.getMessage());
74         }
75
76         return cloudEvent;
77     }
78
79     private static AvcEvent createDmiDataAvcEvent() {
80         final AvcEvent avcEvent = new AvcEvent();
81         final Data data = new Data();
82         final PushChangeUpdate pushChangeUpdate = new PushChangeUpdate();
83         final DatastoreChanges datastoreChanges = new DatastoreChanges();
84         final IetfYangPatchYangPatch ietfYangPatchYangPatch = new IetfYangPatchYangPatch();
85         ietfYangPatchYangPatch.setPatchId("abcd");
86         final Edit edit1 = new Edit();
87         final Value value = new Value();
88         final Map<String, Object> attributeMap = new LinkedHashMap<>();
89         attributeMap.put("isHoAllowed", false);
90         value.setAttributes(List.of(attributeMap));
91         edit1.setEditId("editId");
92         edit1.setOperation("replace");
93         edit1.setTarget("target_xpath");
94         edit1.setValue(value);
95         ietfYangPatchYangPatch.setEdit(List.of(edit1));
96         datastoreChanges.setIetfYangPatchYangPatch(ietfYangPatchYangPatch);
97         pushChangeUpdate.setDatastoreChanges(datastoreChanges);
98         data.setPushChangeUpdate(pushChangeUpdate);
99
100         avcEvent.setData(data);
101         return avcEvent;
102     }
103
104 }