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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.dmi.notifications.avc;
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;
28 import java.time.format.DateTimeFormatter;
29 import java.util.LinkedHashMap;
30 import java.util.List;
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;
45 * Helper to create AvcEvents.
48 @NoArgsConstructor(access = AccessLevel.PRIVATE)
49 public class DmiDataAvcCloudEventCreator {
51 private static final DateTimeFormatter dateTimeFormatter =
52 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
54 private static final ObjectMapper objectMapper = new ObjectMapper();
57 * Creates CloudEvent for DMI Data AVC.
59 * @param eventCorrelationId correlationid
62 public static CloudEvent createCloudEvent(final String eventCorrelationId) {
64 CloudEvent cloudEvent = null;
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());
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);
100 avcEvent.setData(data);