1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2022 Wipro Limited.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 *******************************************************************************/
22 package org.onap.slice.analysis.ms.service;
24 import java.util.List;
25 import java.util.Objects;
27 import org.onap.slice.analysis.ms.configdb.IConfigDbService;
28 import org.onap.slice.analysis.ms.cps.CpsInterface;
29 import org.onap.slice.analysis.ms.models.Configuration;
30 import org.onap.slice.analysis.ms.models.SubCounter;
31 import org.onap.slice.analysis.ms.utils.BeanUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This Thread class consumes message from pm data queue and sends onset message to policy
38 public class ConsumerThread extends Thread {
39 private static Logger log = LoggerFactory.getLogger(ConsumerThread.class);
40 private PmDataQueue pmDataQueue;
41 private IConfigDbService configDbService;
42 private SnssaiSamplesProcessor snssaiSamplesProcessor;
43 private CpsInterface cpsInterface;
44 private long initialDelaySec;
48 * Default constructor.
50 public ConsumerThread() {
52 this.pmDataQueue = BeanUtil.getBean(PmDataQueue.class);
53 this.configDbService = BeanUtil.getBean(IConfigDbService.class);
54 this.initialDelaySec = Configuration.getInstance().getInitialDelaySeconds();
55 this.samples = Configuration.getInstance().getSamples();
56 this.cpsInterface = BeanUtil.getBean(CpsInterface.class);
60 * Consumes data from PM data queue, process the data and sends onset message to policy if needed
64 Boolean isConfigDbEnabled = (Objects.isNull(Configuration.getInstance().getConfigDbEnabled())) ? true
65 : Configuration.getInstance().getConfigDbEnabled();
67 boolean result = false;
69 List<String> nfs = null;
72 Thread.sleep(initialDelaySec);
73 log.info("Starting Consumer Thread");
74 snssai = pmDataQueue.getSnnsaiFromQueue();
75 if (!snssai.equals("")) {
76 log.info("Consumer thread processing data for s-nssai {}", snssai);
78 if (isConfigDbEnabled) {
79 nfs = configDbService.fetchNetworkFunctionsOfSnssai(snssai);
81 nfs = cpsInterface.fetchNetworkFunctionsOfSnssai(snssai);
83 } catch (Exception e) {
84 pmDataQueue.putSnssaiToQueue(snssai);
85 log.error("Exception caught while fetching nfs of snssai {}, {}", snssai, e.getMessage());
87 if (nfs != null && checkForEnoughSamples(nfs, snssai)) {
88 this.snssaiSamplesProcessor = BeanUtil.getBean(SnssaiSamplesProcessor.class);
89 result = snssaiSamplesProcessor.processSamplesOfSnnsai(snssai, nfs);
91 log.info("Not enough samples to process for {}", snssai);
92 pmDataQueue.putSnssaiToQueue(snssai);
96 } catch (Exception e) {
97 log.error("Exception in Consumer Thread, {}", e.getMessage());
104 * Checks whether enough samples are available for the network functions
106 public boolean checkForEnoughSamples(List<String> nfs, String snssai) {
107 for (String nf : nfs) {
108 if (!pmDataQueue.checkSamplesInQueue(new SubCounter(nf, snssai), samples)) {
109 log.info("Not enough samples to process for network function {} of snssai {}", nf, snssai);
110 pmDataQueue.putSnssaiToQueue(snssai);