Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol002-adapter / src / main / java / org / onap / so / adapters / vevnfm / service / DmaapService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 Samsung. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vevnfm.service;
22
23 import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
24 import org.onap.so.adapters.vevnfm.configuration.ConfigProperties;
25 import org.onap.so.adapters.vevnfm.event.DmaapEvent;
26 import org.onap.so.rest.service.HttpRestServiceProvider;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 public class DmaapService {
36
37     private static final Logger logger = LoggerFactory.getLogger(DmaapService.class);
38
39     private final String endpoint;
40     private final String topic;
41     private final String closedLoopControlName;
42     private final String version;
43     private final HttpRestServiceProvider restProvider;
44
45     @Autowired
46     public DmaapService(final ConfigProperties configProperties, final HttpRestServiceProvider restProvider) {
47         this.endpoint = configProperties.getDmaapEndpoint();
48         this.topic = configProperties.getDmaapTopic();
49         this.closedLoopControlName = configProperties.getDmaapClosedLoopControlName();
50         this.version = configProperties.getDmaapVersion();
51         this.restProvider = restProvider;
52     }
53
54     public void send(final VnfLcmOperationOccurrenceNotification notification, final String genericId) {
55         try {
56             final DmaapEvent event = new DmaapEvent(closedLoopControlName, version, notification, genericId);
57             final ResponseEntity<String> response = restProvider.postHttpRequest(event, getUrl(), String.class);
58             final HttpStatus statusCode = response.getStatusCode();
59             final String body = response.getBody();
60
61             logger.info("The DMaaP replied with the code {} and the body {}", statusCode, body);
62         } catch (Exception e) {
63             logger.warn("An issue connecting to DMaaP", e);
64         }
65     }
66
67     private String getUrl() {
68         return endpoint + topic;
69     }
70 }