9bd1119588ee5181ed3716d1b6d0c84b43b7c8d1
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / avc / ncmptoclient / AvcEventPublisher.java
1 /*
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
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.events.avc.ncmptoclient;
22
23 import io.cloudevents.CloudEvent;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.events.EventsPublisher;
29 import org.onap.cps.ncmp.api.impl.events.NcmpCloudEventBuilder;
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;
35
36 @Service
37 @RequiredArgsConstructor
38 public class AvcEventPublisher {
39
40     private final EventsPublisher<CloudEvent> eventsPublisher;
41
42     @Value("${app.ncmp.avc.cm-events-topic}")
43     private String avcTopic;
44
45     /**
46      * Publish attribute value change event.
47      *
48      * @param eventKey id of the cmHandle being registered
49      */
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);
53
54         final Map<String, String> extensions = createAvcEventExtensions(eventKey);
55         final CloudEvent avcCloudEvent =
56             NcmpCloudEventBuilder.builder().type(AvcEvent.class.getTypeName())
57             .event(avcEvent).extensions(extensions).setCloudEvent().build();
58
59         eventsPublisher.publishCloudEvent(avcTopic, eventKey, avcCloudEvent);
60     }
61
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);
69
70         final Data payload = new Data();
71         payload.setAttributeValueChange(Collections.singletonList(avc));
72         final AvcEvent avcEvent = new AvcEvent();
73         avcEvent.setData(payload);
74         return avcEvent;
75     }
76
77     private Map<String, String> createAvcEventExtensions(final String eventKey) {
78         final Map<String, String> extensions = new HashMap<>();
79         extensions.put("correlationid", eventKey);
80         return extensions;
81     }
82 }