feat:Enhance sliceanalysis MS to use DCAE SDK dmaap-client lib
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / dmaap / PolicyDmaapClient.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020 Wipro Limited.
6  *   Copyright (C) 2022 CTC, Inc.
7  *   ==============================================================================
8  *     Licensed under the Apache License, Version 2.0 (the "License");
9  *     you may not use this file except in compliance with the License.
10  *     You may obtain a copy of the License at
11  *
12  *          http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *     Unless required by applicable law or agreed to in writing, software
15  *     distributed under the License is distributed on an "AS IS" BASIS,
16  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *     See the License for the specific language governing permissions and
18  *     limitations under the License.
19  *     ============LICENSE_END=========================================================
20  *
21  *******************************************************************************/
22
23 package org.onap.slice.analysis.ms.dmaap;
24
25 import java.io.IOException;
26 import java.util.Map;
27
28 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterPublisher;
29 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest;
30 import org.onap.slice.analysis.ms.models.Configuration;
31 import org.onap.slice.analysis.ms.utils.DcaeDmaapUtil;
32
33 /**
34  * Client class to handle Policy interactions 
35  */
36 public class PolicyDmaapClient {
37
38     private Configuration configuration;
39
40     public PolicyDmaapClient(Configuration configuration) {
41         this.configuration = configuration;
42     }
43
44     /**
45      * Method stub for sending notification to policy.
46      */
47     @SuppressWarnings("unchecked")
48     public boolean sendNotificationToPolicy(String msg) {
49         Map<String, Object> streamsPublishes = configuration.getStreamsPublishes();
50         String policyTopicUrl = ((Map<String, String>) ((Map<String, Object>) streamsPublishes.get("CL_topic"))
51                 .get("dmaap_info")).get("topic_url");
52         try {
53             MessageRouterPublisher publisher = DcaeDmaapUtil.buildPublisher();
54             MessageRouterPublishRequest request = DcaeDmaapUtil.buildPublisherRequest("CL_topic", policyTopicUrl);
55
56             NotificationProducer notificationProducer = new NotificationProducer(publisher, request);
57             notificationProducer.sendNotification(msg);
58         } catch (IOException e) {
59             return false;
60         }
61         return true;
62     }
63
64 }