aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/wipro/www/sonhms/SdnrNotificationHandlingState.java
blob: 26377906180bae0105c15c5a1bf41a583414f79b (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
/*******************************************************************************
 * ============LICENSE_START=======================================================
 * pcims
 *  ================================================================================
 *  Copyright (C) 2018 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 com.wipro.www.sonhms;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wipro.www.sonhms.child.ChildThread;
import com.wipro.www.sonhms.child.Graph;
import com.wipro.www.sonhms.dao.CellInfoRepository;
import com.wipro.www.sonhms.dao.ClusterDetailsRepository;
import com.wipro.www.sonhms.entity.CellInfo;
import com.wipro.www.sonhms.entity.ClusterDetails;
import com.wipro.www.sonhms.exceptions.ConfigDbNotFoundException;
import com.wipro.www.sonhms.model.CellPciPair;
import com.wipro.www.sonhms.model.FapServiceList;
import com.wipro.www.sonhms.model.LteNeighborListInUseLteCell;
import com.wipro.www.sonhms.model.Notification;
import com.wipro.www.sonhms.model.ThreadId;
import com.wipro.www.sonhms.restclient.SdnrRestClient;
import com.wipro.www.sonhms.utils.BeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.Logger;

public class SdnrNotificationHandlingState implements SonState {
    private static Map<Long, ChildThread> childThreadMap = new HashMap<>();
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(SdnrNotificationHandlingState.class);

    @Override
    public void stateChange(SonContext sonContext) {
        // logic to determine if notif to be processed
        log.debug("inside statechange of sdnr notif state");
        String notification = sonContext.getSdnrNotification();
        Notification notificationObject;
        try {

            ObjectMapper mapper = new ObjectMapper();
            notificationObject = mapper.readValue(notification, Notification.class);
            log.debug("notificationObject{}", notificationObject);

            List<FapServiceList> serviceList = notificationObject.getPayload().getRadioAccess().getFapServiceList();
            for (FapServiceList fapService : serviceList) {
                String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();
                log.debug("cellId:{}", cellId);
                log.debug("inside for loop");

                List<ClusterDetails> clusterDetails = getAllClusters();

                ClusterDetails clusterDetail = getClusterForNotification(fapService, clusterDetails);

                if (clusterDetail == null) {
                    // form the cluster
                    Graph cluster = createCluster(fapService);
                    // save to db
                    UUID clusterId = UUID.randomUUID();
                    cluster.setGraphId(clusterId);
                    // create the child thread
                    log.debug("creating new child");
                    BlockingQueue<FapServiceList> queue = new LinkedBlockingQueue<>();
                    ThreadId threadId = new ThreadId();
                    threadId.setChildThreadId(0);
                    ChildThread child = new ChildThread(sonContext.getChildStatusUpdate(), cluster, queue, threadId);
                    queue.put(fapService);
                    MainThreadComponent mainThreadComponent = BeanUtil.getBean(MainThreadComponent.class);
                    mainThreadComponent.getPool().execute(child);

                    waitForThreadId(threadId);

                    saveCluster(cluster, clusterId, threadId.getChildThreadId());
                    addChildThreadMap(threadId.getChildThreadId(), child);
                    sonContext.addChildStatus(threadId.getChildThreadId(), "processingNotifications");

                }

                else {
                    if (isOofTriggeredForCluster(sonContext, clusterDetail)) {
                        sonContext.setNotifToBeProcessed(false);
                        bufferNotification(fapService, clusterDetail.getClusterId());
                    } else {
                        sonContext.setNotifToBeProcessed(true);
                        log.debug("childThreadId:{}", clusterDetail.getChildThreadId());
                        childThreadMap.get(clusterDetail.getChildThreadId()).putInQueue(fapService);
                    }
                }
            }
        } catch (Exception e) {
            log.error("caught in sdnr notif handling state{}", e);
        }

        WaitState waitState = WaitState.getInstance();
        sonContext.setPciState(waitState);
        sonContext.stateChange(sonContext);
    }

    private void waitForThreadId(ThreadId threadId) {
        try {
            synchronized (threadId) {
                while (threadId.getChildThreadId() == 0) {
                    threadId.wait();
                }
            }
        } catch (InterruptedException e) {

            log.error("ChildThread queue error {}", e);
            Thread.currentThread().interrupt();
        }
    }

    private String saveCluster(Graph cluster, UUID clusterId, Long threadId) {

        String cellPciNeighbourString = cluster.getPciNeighbourJson();

        log.debug("cluster hahsmap to string : {}", cellPciNeighbourString);
        cluster.setGraphId(clusterId);

        ClusterDetails details = new ClusterDetails();
        details.setClusterId(clusterId.toString());
        details.setClusterInfo(cellPciNeighbourString);
        details.setChildThreadId(threadId);

        ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class);
        clusterDetailsRepository.save(details);

        return clusterId.toString();
    }

    private Graph createCluster(FapServiceList fapService) throws ConfigDbNotFoundException {

        Graph cluster = new Graph();
        log.debug("cluster formation started");
        int phycellId = fapService.getX0005b9Lte().getPhyCellIdInUse();
        String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();

        CellInfoRepository cellInfoRepository = BeanUtil.getBean(CellInfoRepository.class);
        cellInfoRepository.save(new CellInfo(cellId, fapService.getX0005b9Lte().getPnfName()));

        CellPciPair val = new CellPciPair();
        val.setCellId(cellId);
        val.setPhysicalCellId(phycellId);
        List<LteNeighborListInUseLteCell> neighbourlist;
        neighbourlist = fapService.getCellConfig().getLte().getRan().getNeighborListInUse()
                .getLteNeighborListInUseLteCell();
        log.debug("Neighbor list size: {}", neighbourlist.size());

        for (int i = 0; i < neighbourlist.size(); i++) {
            String cell = neighbourlist.get(i).getAlias();
            int phy = neighbourlist.get(i).getPhyCellId();

            cellInfoRepository.save(new CellInfo(cell, neighbourlist.get(i).getPnfName()));

            log.debug("cellID: {}", cell);
            log.debug("PCI: {}", phy);
            CellPciPair val1 = new CellPciPair();
            val1.setCellId(cell);
            val1.setPhysicalCellId(phy);
            cluster.addEdge(val, val1);
            log.debug("cluster: {}", cluster);

            List<CellPciPair> nbrList = SdnrRestClient.getNbrList(neighbourlist.get(i).getAlias());

            for (CellPciPair nbr : nbrList) {
                String cid = nbr.getCellId();
                int pci = nbr.getPhysicalCellId();
                CellPciPair val3 = new CellPciPair();
                val3.setCellId(cid);
                val3.setPhysicalCellId(pci);

                cluster.addEdge(val1, val3);
            }
        }

        log.debug("final cluster: {}", cluster);
        return cluster;
    }

    private void bufferNotification(FapServiceList fapService, String clusterId) {

        ObjectMapper mapper = new ObjectMapper();
        BufferNotificationComponent bufferNotifComponent = new BufferNotificationComponent();
        String serviceListString = "";
        try {
            serviceListString = mapper.writeValueAsString(fapService);
        } catch (JsonProcessingException e) {
            log.debug("JSON processing exception: {}", e);
        }
        bufferNotifComponent.bufferNotification(serviceListString, clusterId);

    }

    private boolean isOofTriggeredForCluster(SonContext pciContext, ClusterDetails clusterDetail) {
        Long childThreadId = clusterDetail.getChildThreadId();
        String childStatus = pciContext.getChildStatus(childThreadId);
        return childStatus.equals("triggeredOof");

    }

    private ClusterDetails getClusterForNotification(FapServiceList fapService, List<ClusterDetails> clusterDetails) {

        String cellId = fapService.getCellConfig().getLte().getRan().getCellIdentity();
        Map<CellPciPair, ArrayList<CellPciPair>> cellPciNeighbourMap;

        for (ClusterDetails clusterDetail : clusterDetails) {
            Graph cluster = new Graph(clusterDetail.getClusterInfo());
            cellPciNeighbourMap = cluster.getCellPciNeighbourMap();
            Set<CellPciPair> keys = cellPciNeighbourMap.keySet();
            Iterator<CellPciPair> traverse = keys.iterator();
            while (traverse.hasNext()) {
                CellPciPair key = traverse.next();
                String currentCellId = key.getCellId();
                if (cellId.equals(currentCellId)) {
                    return clusterDetail;
                }
            }
        }

        return null;
    }

    private List<ClusterDetails> getAllClusters() {
        ClusterDetailsComponent clusterDetailsComponent = new ClusterDetailsComponent();
        return clusterDetailsComponent.getClusterDetails();
    }

    public static void addChildThreadMap(Long childThreadId, ChildThread child) {
        childThreadMap.put(childThreadId, child);
    }

    public static Map<Long, ChildThread> getChildThreadMap() {
        return childThreadMap;
    }

}