b96bffa58feba35feedb90ba6be474b0303ce120
[so.git] / adapters / etsi-sol002-adapter / src / main / java / org / onap / so / adapters / vevnfm / service / DmaapConditionalSender.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.apache.logging.log4j.util.Strings;
24 import org.onap.so.adapters.etsi.sol003.adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
25 import org.onap.so.adapters.vevnfm.aai.AaiConnection;
26 import org.onap.so.adapters.vevnfm.configuration.ConfigProperties;
27 import org.onap.so.adapters.vevnfm.constant.NotificationVnfFilterType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
31
32 @Service
33 public class DmaapConditionalSender {
34
35     private static final Logger logger = LoggerFactory.getLogger(DmaapConditionalSender.class);
36
37     private final NotificationVnfFilterType notificationVnfFilterType;
38     private final AaiConnection aaiConnection;
39     private final DmaapService dmaapService;
40
41     public DmaapConditionalSender(final ConfigProperties configProperties, final AaiConnection aaiConnection,
42             final DmaapService dmaapService) {
43         this.notificationVnfFilterType = configProperties.getNotificationVnfFilterType();
44         this.aaiConnection = aaiConnection;
45         this.dmaapService = dmaapService;
46     }
47
48     public void send(final VnfLcmOperationOccurrenceNotification notification) {
49         final String href = notification.getLinks().getVnfInstance().getHref();
50         boolean logSent = false;
51
52         switch (notificationVnfFilterType) {
53             case ALL:
54                 dmaapService.send(notification, aaiConnection.receiveGenericVnfId(href));
55                 logSent = true;
56                 break;
57             case AAI_CHECKED:
58                 final String genericId = aaiConnection.receiveGenericVnfId(href);
59                 if (Strings.isNotBlank(genericId)) {
60                     dmaapService.send(notification, genericId);
61                     logSent = true;
62                 }
63                 break;
64             case NONE:
65                 break;
66             default:
67                 throw new IllegalArgumentException(
68                         "The value of VnfNotificationFilterType is not supported: " + notificationVnfFilterType);
69         }
70
71         final String vnfInstanceId = notification.getVnfInstanceId();
72         final String not = logSent ? "" : "not ";
73         logger.info("The info with the VNF id '{}' is " + not + "sent to DMaaP", vnfInstanceId);
74     }
75 }