aboutsummaryrefslogtreecommitdiffstats
path: root/ransim/ransimctrlr/RANSIM-CTRLR/src/main/java/org/onap/ransim/rest/api/services/SlicingPMDataGenerator.java
blob: e61e14f6cacfa197805ecf4ee3e2e72efe5130ad (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*-
 * ============LICENSE_START=======================================================
 * Ran Simulator Controller
 * ================================================================================
 * Copyright (C) 2021 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.ransim.rest.api.services;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.Session;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

import org.apache.log4j.Logger;
import org.onap.ransim.rest.api.exceptions.RansimException;
import org.onap.ransim.rest.api.models.NSSAIConfig;
import org.onap.ransim.rest.web.mapper.GNBDUModel;
import org.onap.ransim.rest.web.mapper.NRCellDUModel;
import org.onap.ransim.rest.web.mapper.NSSAIData;
import org.onap.ransim.rest.web.mapper.PLMNInfoModel;
import org.onap.ransim.rest.xml.models.FileFooter;
import org.onap.ransim.rest.xml.models.FileHeader;
import org.onap.ransim.rest.xml.models.FileSender;
import org.onap.ransim.rest.xml.models.GranularityPeriod;
import org.onap.ransim.rest.xml.models.Job;
import org.onap.ransim.rest.xml.models.ManagedElement;
import org.onap.ransim.rest.xml.models.MeasCollec;
import org.onap.ransim.rest.xml.models.MeasCollecEnd;
import org.onap.ransim.rest.xml.models.MeasCollecFile;
import org.onap.ransim.rest.xml.models.MeasData;
import org.onap.ransim.rest.xml.models.MeasInfo;
import org.onap.ransim.rest.xml.models.MeasType;
import org.onap.ransim.rest.xml.models.MeasValue;
import org.onap.ransim.rest.xml.models.ReportingPeriod;
import org.onap.ransim.rest.xml.models.Result;
import org.onap.ransim.websocket.model.SlicingPmMessage;
import org.onap.ransim.websocket.server.RansimWebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class SlicingPMDataGenerator {

    static Logger logger = Logger.getLogger(SlicingPMDataGenerator.class.getName());

    @Autowired
    RANSliceConfigService ranSliceConfigService;

    double ricId11MaxVariation = 0.8;
    double ricId22MaxVariation = 0.2;

    /**
     * Generates closed loop PM data for all DU functions Generates PRBs for DUs in
     * the ratio of 80:20 for the same nssai in different RICs
     * 
     * @throws RansimException
     */
    public void generateClosedLoopPmData(long startTime) {
        try {
            String requestUrl = "http://" + "localhost" + ":" + "8081" + "/ransim/api/ransim-db/v4/du-list";
            List<GNBDUModel> duList = sendGetRequestToransimDb(requestUrl).getBody();
            for (GNBDUModel du : duList) {
                logger.info("Generating PM data for DU :  " + du.getgNBDUName() + " Id :" + du.getgNBDUId());
                Map<String, NSSAIConfig> activeNssaiDetails = new HashMap<String, NSSAIConfig>();
                List<NRCellDUModel> duCellList = du.getCellDUList();
                int ricId = du.getNearRTRICId();
                List<PLMNInfoModel> plmnInfoList = new ArrayList<>();
                duCellList.stream().filter(cell -> (!Objects.isNull(cell.getpLMNInfoList())));
                duCellList.forEach(cell -> plmnInfoList.addAll(cell.getpLMNInfoList()));
                List<NSSAIData> nssaiData = new ArrayList<>();
                plmnInfoList.forEach(plmnInfo -> nssaiData.add(plmnInfo.getsNSSAI()));
                nssaiData.stream().filter(
                        nssai -> (!(Objects.isNull(nssai.getStatus()))) && nssai.getStatus().equalsIgnoreCase("active"))
                        .forEach(x -> activeNssaiDetails.put(x.getsNSSAI(), x.getConfigData()));
                produceMeasurementCollectionFile(du, activeNssaiDetails, duCellList, ricId);
                logger.info("PM data generated for DU : " + du.getgNBDUName() + " Id: " + du.getgNBDUId());
            }
        } catch (RansimException e) {
            logger.debug("ERROR in closed lopp PM data generation : ");
            logger.debug(e);
        } catch (Exception exp) {
            logger.debug(exp);
        }
    }

    private void produceMeasurementCollectionFile(GNBDUModel du, Map<String, NSSAIConfig> activeNssaiDetails,
            List<NRCellDUModel> duCellList, int ricId) throws RansimException {
        logger.debug("produceing MeasurementCollectionFile ");
        SlicingPmMessage pmMessage = new SlicingPmMessage();
        pmMessage.setStartEpochMicrosec(System.currentTimeMillis() * 1000);
        pmMessage.setSourceName(du.getgNBDUName());
        LocalDateTime beginTime = LocalDateTime.now();
        String beginTimeString = beginTime.toString();
        MeasCollec measCollec = new MeasCollec(beginTimeString);
        FileSender fileSender = new FileSender(du.getgNBDUName());
        FileHeader fileHeader = new FileHeader("Prefix", "Acme Ltd", "32.435 V10.0", measCollec, fileSender);
        Random r = new Random();
        int jobId = r.nextInt((9999 - 1000) + 1) + 1000;
        Job job = new Job(String.valueOf(jobId));
        ReportingPeriod reportingPeriod = new ReportingPeriod("PT900S");

        List<MeasType> measTypeList = setMeasurementTypes(activeNssaiDetails);
        List<MeasValue> measValueList = setMeasurementValues(duCellList, getMaxVariation(ricId), measTypeList);

        ManagedElement managedElement = new ManagedElement("r0.1", du.getgNBDUName());
        LocalDateTime grabularityEndTime = LocalDateTime.now();
        String grabularityEndTimeString = grabularityEndTime.toString();
        GranularityPeriod granularityPeriod = new GranularityPeriod(grabularityEndTimeString, "PT900S");
        MeasInfo measInfo =
                new MeasInfo("measInfoIsVal", job, granularityPeriod, reportingPeriod, measTypeList, measValueList);
        List<MeasInfo> measInfoList = new ArrayList<MeasInfo>();
        measInfoList.add(measInfo);
        MeasData measData = new MeasData(managedElement, measInfoList);
        List<MeasData> measDataList = new ArrayList<MeasData>();
        measDataList.add(measData);
        LocalDateTime endTime = LocalDateTime.now();
        String endTimeString = endTime.toString();
        pmMessage.setLastEpochMicrosec(System.currentTimeMillis() * 1000);
        MeasCollecEnd measCollecEnd = new MeasCollecEnd(endTimeString);
        FileFooter fileFooter = new FileFooter(measCollecEnd);
        MeasCollecFile measCollecFile = new MeasCollecFile(fileHeader, measDataList, fileFooter,
                "http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec");

        try {
            String startDate = beginTimeString.replace(':', '-');
            String endDate = endTimeString.replace(':', '-');
            String fileName =
                    "A" + startDate + "-" + endDate + "-" + String.valueOf(jobId) + "-" + du.getgNBDUName() + ".xml";
            pmMessage.setFileName(fileName);
            Gson gson = new Gson();
            String pmData = gson.toJson(measCollecFile);
            pmMessage.setPmData(pmData);
            closedLoopPmData(pmMessage);
        } catch (Exception exp) {
            logger.debug(exp);
        }

    }

    private List<MeasValue> setMeasurementValues(List<NRCellDUModel> duCellList, double ricIdVariation,
            List<MeasType> measTypeList) {
        logger.debug("setting MeasurementValues");
        List<MeasValue> measValueList = new ArrayList<MeasValue>();
        duCellList.forEach(cell -> {
            AtomicInteger pValue = new AtomicInteger(1);
            List<Result> resultList = new ArrayList<>();
            int prbs = cell.getPrbs();
            measTypeList.forEach(meas -> {
                int prbsUsed = (int) (Math.random() * ricIdVariation * prbs);
                Result result = new Result(pValue.getAndIncrement(), prbsUsed);
                resultList.add(result);
            });
            MeasValue measValue = new MeasValue(cell.getCellLocalId(), resultList, false);
            measValueList.add(measValue);

        });
        return measValueList;
    }

    private double getMaxVariation(int ricId) throws RansimException {
        switch (ricId) {
            case 11:
                // ricId11MaxVariation =
                // Double.parseDouble(System.getProperty("RIC_11_VARIATION"));
                return ricId11MaxVariation;
            case 22:
                return ricId22MaxVariation;
            default:
                throw new RansimException("Invalid RIC ID : valid Ids : [11,22] ");
        }
    }

    private List<MeasType> setMeasurementTypes(Map<String, NSSAIConfig> activeNssaiDetails) {
        logger.debug("setting MeasurementTypes");
        List<MeasType> measTypeList = new ArrayList<MeasType>();
        AtomicInteger pValue = new AtomicInteger(1);
        activeNssaiDetails.forEach((nssai, configData) -> {
            MeasType mesType1 = new MeasType("SM.PrbUsedDl." + nssai, pValue.getAndIncrement());
            MeasType mesType2 = new MeasType("SM.PrbUsedUl." + nssai, pValue.getAndIncrement());
            measTypeList.add(mesType1);
            measTypeList.add(mesType2);
        });
        return measTypeList;
    }

    public static <T> ResponseEntity<List<GNBDUModel>> sendGetRequestToransimDb(String requestUrl) {
        HttpHeaders headers = new HttpHeaders();
        logger.info("sending request to ransimdb : " + requestUrl);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
        try {
            RestTemplate restTemplate = new RestTemplate();
            return restTemplate.exchange(requestUrl, HttpMethod.GET, requestEntity,
                    new ParameterizedTypeReference<List<GNBDUModel>>() {});
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    public void closedLoopPmData(SlicingPmMessage pmMessage) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(pmMessage);
        logger.info("ClosedPmData " + jsonStr);
        String ipPort = RansimControllerServices.serverIdIpPortMapping.get(pmMessage.getSourceName());
        if (ipPort != null && !ipPort.trim().equals("")) {
            logger.info("Connection estabilished with ip: " + ipPort);
            if (ipPort != null && !ipPort.trim().equals("")) {
                Session clSess = RansimControllerServices.webSocketSessions.get(ipPort);
                if (clSess != null) {
                    logger.info("PM Data message sent.");
                    RansimWebSocketServer.sendIntelligentSlicingPmData(jsonStr, clSess);
                } else {
                    logger.info("No client session for " + ipPort);
                }
            } else {
                logger.info("No client for this serverId");
            }
        } else {
            logger.info("No client for ");
        }
    }
}