aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcaegen2/services/sonhms/MainThread.java
blob: 2f30a91125ff8374feaf9151bc5cdd618b92116e (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
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  son-handler
 *  ================================================================================
 *   Copyright (C) 2019 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.dcaegen2.services.sonhms;

import fj.data.Either;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.onap.dcaegen2.services.sonhms.model.FapServiceList;
import org.onap.dcaegen2.services.sonhms.model.Notification;
import org.onap.dcaegen2.services.sonhms.utils.ClusterUtils;
import org.onap.dcaegen2.services.sonhms.utils.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainThread implements Runnable {

    private static Logger log = LoggerFactory.getLogger(MainThread.class);

    private NewSdnrNotification newNotification;

    private NewFmNotification newFmNotification;

    private BlockingQueue<List<String>> childStatusQueue;

    private DmaapNotificationsComponent dmaapNotificationsComponent;

    private FaultNotificationComponent faultNotificationComponent;

    private EventHandler eventHandler;

    private Map<String, FaultEvent> bufferedFmNotificationCells;

    private List<String> sdnrNotificationCells;

    private Boolean isTimer;

    private Timestamp startTimer;

    List<FaultEvent> fmNotificationToBuffer;

    /**
     * parameterized constructor.
     */
    public MainThread(NewSdnrNotification newNotification, NewFmNotification newFmNotification) {
        super();
        this.newFmNotification = newFmNotification;
        this.newNotification = newNotification;
        childStatusQueue = new LinkedBlockingQueue<>();
        dmaapNotificationsComponent = new DmaapNotificationsComponent();
        faultNotificationComponent = new FaultNotificationComponent();
        sdnrNotificationCells = new ArrayList<>();
        fmNotificationToBuffer = new ArrayList<>();
        bufferedFmNotificationCells = new HashMap<>();
        eventHandler = new EventHandler(childStatusQueue,
                Executors.newFixedThreadPool(Configuration.getInstance().getMaximumClusters()), new HashMap<>(),
                new ClusterUtils(), new ThreadUtils());
        isTimer = false;
        startTimer = new Timestamp(System.currentTimeMillis());

    }

    @Override
    public void run() {
        log.info("Starting Main Thread");

        // Check for Notifications from Dmaap and Child thread
        Boolean done = false;

        while (!done) {

            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            if (isTimer) {
                Long difference = currentTime.getTime() - startTimer.getTime();
                if (difference > 5000) {
                    log.info("FM handling difference > 5000");

                    for (String sdnrCell : sdnrNotificationCells) {
                        bufferedFmNotificationCells.remove(sdnrCell);
                    }

                    log.info("FM bufferedFMNotificationCells {}", bufferedFmNotificationCells.values());
                    List<FaultEvent> fmNotificationsToHandle = new ArrayList<>(bufferedFmNotificationCells.values());
                    Boolean result = eventHandler.handleFaultNotification(fmNotificationsToHandle);
                    bufferedFmNotificationCells = new HashMap<>();
                    isTimer = false;
                    log.info("FM notification handling {}", result);
                }
            }

            try {
                if (!childStatusQueue.isEmpty()) {
                    List<String> childState = childStatusQueue.poll();
                    if (childState != null) {
                        eventHandler.handleChildStatusUpdate(childState);
                    }
                }

                if (newNotification.getNewNotif()) {
                    Either<Notification, Integer> notification = dmaapNotificationsComponent.getSdnrNotifications();
                    if (notification.isRight()) {
                        if (notification.right().value() == 400) {
                            log.error("Error parsing the notification from SDNR");
                        } else if (notification.right().value() == 404) {
                            newNotification.setNewNotif(false);
                        }
                    } else if (notification.isLeft()) {
                        List<FapServiceList> fapServiceLists = (notification.left().value()).getPayload()
                                .getRadioAccess().getFapServiceList();
                        for (FapServiceList fapServiceList : fapServiceLists) {
                            sdnrNotificationCells.add(fapServiceList.getAlias());

                        }

                        Boolean result = eventHandler.handleSdnrNotification(notification.left().value());
                        log.debug("SDNR notification handling {}", result);

                    }

                }
                if (newFmNotification.getNewNotif()) {
					log.info("newFmNotification has come");

					String faultCellId = "";
					Either<List<FaultEvent>, Integer> fmNotifications = faultNotificationComponent
							.getFaultNotifications();
					if (fmNotifications.isRight()) {
						if (fmNotifications.right().value() == 400) {
							log.info("Error parsing notifications");
						} else if (fmNotifications.right().value() == 404) {
							newFmNotification.setNewNotif(false);
						}
					} else {
						for (FaultEvent fmNotification : fmNotifications.left().value()) {
							if (fmNotification.getEvent().getFaultFields().getSpecificProblem()
									.equals("Optimised PCI")) {
								log.info("PCI problem cleared for :" + fmNotification);
							} else if ((fmNotification.getEvent().getFaultFields().getSpecificProblem()
									.equals("Collision"))
									|| (fmNotification.getEvent().getFaultFields().getSpecificProblem()
											.equals("Confusion"))) {
								faultCellId = fmNotification.getEvent().getCommonEventHeader().getSourceName();
								bufferedFmNotificationCells.put(faultCellId, fmNotification);
								log.info("Buffered FM cell {}", faultCellId);
								log.info("fmNotification{}", fmNotification);
							} else {
								log.info("Error resolving faultNotification for :" + fmNotification);
							}
						}

						if (!(bufferedFmNotificationCells.isEmpty())) {
							log.info("bufferedFMNotificationCells before staring timer {}",
									bufferedFmNotificationCells.keySet());

							for (String sdnrCell : sdnrNotificationCells) {
								bufferedFmNotificationCells.remove(sdnrCell);
							}

							startTimer = new Timestamp(System.currentTimeMillis());
							isTimer = true;
							log.info("Buffered FM cell {}", bufferedFmNotificationCells.keySet());
						}
					}

				}

            } catch (Exception e) {
                log.error("Exception in main Thread", e);
                done = true;
            }

        }

    }
}