summaryrefslogtreecommitdiffstats
path: root/components/slice-analysis-ms/src/main/java/org/onap/slice/analysis/ms/service/PmDataQueue.java
blob: 4f1969796e4de6ba86eb28583fe06282b8132fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  slice-analysis-ms
 *  ================================================================================
 *   Copyright (C) 2020 Wipro Limited.
 *   ==============================================================================
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *     ============LICENSE_END=========================================================
 *
 *******************************************************************************/

package org.onap.slice.analysis.ms.service;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import org.onap.slice.analysis.ms.models.MeasurementObject;
import org.onap.slice.analysis.ms.models.SubCounter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/** 
 * This class represents the data structure for storing the pm events 
 */
@Component
public class PmDataQueue {
    private static Logger log = LoggerFactory.getLogger(PmDataQueue.class);

	private Map<SubCounter, Queue<List<MeasurementObject>>> subCounterMap = Collections.synchronizedMap(new LinkedHashMap<SubCounter, Queue<List<MeasurementObject>>>());
	private Queue<String> snssaiList = new LinkedBlockingQueue<>();

	/**
	 * put the measurement data for (an S-NSSAI from a network function) in the queue
	 */
	public void putDataToQueue(SubCounter subCounter, List<MeasurementObject> measurementObjectData) {
		Queue<List<MeasurementObject>> measQueue;
		if (subCounterMap.containsKey(subCounter)){
			subCounterMap.get(subCounter).add(measurementObjectData);
		}
		else {
			measQueue = new LinkedBlockingQueue<>();
			measQueue.add(measurementObjectData);
			subCounterMap.put(subCounter, measQueue);
		}
		log.debug("Queue: {}", subCounterMap);
	}

	/**
	 * get the measurement data for (an S-NSSAI from a network function) from the queue
	 * returns the specified number of samples
	 */
	public List<List<MeasurementObject>> getSamplesFromQueue(SubCounter subCounter, int samples) {
		List<List<MeasurementObject>> sampleList = null;
		if (subCounterMap.containsKey(subCounter)){
			Queue<List<MeasurementObject>> measQueue = subCounterMap.get(subCounter);
			if(measQueue.size() >= samples) {
				sampleList = new LinkedList<>();
				while(samples > 0) {
					sampleList.add(measQueue.remove());
					samples --;
				}
			}
		}
		return sampleList;
	}

	/**
	 * put S-NSSAI to the queue
	 */
	public void putSnssaiToQueue(String snssai) {
		if (!snssaiList.contains(snssai)) 
			snssaiList.add(snssai);
	}

	/**
	 * get S-NSSAI from the queue
	 */
	public String getSnnsaiFromQueue() {
		String snssai = "";
		try {
			if(!snssaiList.isEmpty()){
				snssai = snssaiList.remove();
			}
		}
		catch(Exception e) {
			log.error("Problem fetching from the Queue, {}", e.getMessage());
		}
		return snssai;
	}
}