2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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.api.impl.events.avc.ncmptoclient;
23 import io.cloudevents.CloudEvent;
24 import java.util.Collections;
25 import java.util.HashMap;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.events.EventsPublisher;
29 import org.onap.cps.ncmp.api.impl.events.NcmpEvent;
30 import org.onap.cps.ncmp.events.avc.ncmp_to_client.Avc;
31 import org.onap.cps.ncmp.events.avc.ncmp_to_client.AvcEvent;
32 import org.onap.cps.ncmp.events.avc.ncmp_to_client.Data;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Service;
37 @RequiredArgsConstructor
38 public class AvcEventPublisher {
40 private final EventsPublisher<CloudEvent> eventsPublisher;
42 @Value("${app.ncmp.avc.cm-events-topic}")
43 private String avcTopic;
46 * Publish attribute value change event.
48 * @param eventKey id of the cmHandle being registered
50 public void publishAvcEvent(final String eventKey, final String attributeName,
51 final String oldAttributeValue, final String newAttributeValue) {
52 final AvcEvent avcEvent = buildAvcEvent(attributeName, oldAttributeValue, newAttributeValue);
54 final Map<String, String> extensions = createAvcEventExtensions(eventKey);
55 final CloudEvent avcCloudEvent =
56 NcmpEvent.builder().type(AvcEvent.class.getTypeName())
57 .data(avcEvent).extensions(extensions).build().asCloudEvent();
59 eventsPublisher.publishCloudEvent(avcTopic, eventKey, avcCloudEvent);
62 private AvcEvent buildAvcEvent(final String attributeName,
63 final String oldAttributeValue,
64 final String newAttributeValue) {
65 final Avc avc = new Avc();
66 avc.setAttributeName(attributeName);
67 avc.setOldAttributeValue(oldAttributeValue);
68 avc.setNewAttributeValue(newAttributeValue);
70 final Data payload = new Data();
71 payload.setAttributeValueChange(Collections.singletonList(avc));
72 final AvcEvent avcEvent = new AvcEvent();
73 avcEvent.setData(payload);
77 private Map<String, String> createAvcEventExtensions(final String eventKey) {
78 final Map<String, String> extensions = new HashMap<>();
79 extensions.put("correlationid", eventKey);