aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/wipro/www/sonhms/WaitState.java
blob: a74f795cc091251021efa8224b034057e76b061f (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
/*******************************************************************************
 * ============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.wipro.www.sonhms.dao.DmaapNotificationsRepository;
import com.wipro.www.sonhms.utils.BeanUtil;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;



public class WaitState implements SonState {

    private static WaitState instance;
    private List<String> sdnrNotification = new ArrayList<>();
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(WaitState.class);

    protected WaitState() {

    }

    /**
     * singleton.
     */
    public static WaitState getInstance() {
        if (instance == null) {
            return new WaitState();
        }
        return instance;
    }

    public void putSdnrNotification(String notification) {
        sdnrNotification.add(notification);
        log.debug("sdnrNotification size: {}", sdnrNotification.size());
    }

    @Override
    public void stateChange(SonContext pciContext) {
        log.debug("inside state change of wait state");

        while (pciContext.getChildStatusUpdate().isEmpty() && !pciContext.getNewNotification().getNewNotif()) {
        }

        List<String> childStatus = pciContext.getChildStatusUpdate().poll();
        if (childStatus != null) {
            Long threadId = Long.parseLong(childStatus.get(0));
            log.debug("threadId: {}", threadId);
            log.debug("childStatus: {}", childStatus.get(1));
            pciContext.setChildThreadId(threadId);
            pciContext.addChildStatus(threadId, childStatus.get(1));
            pciContext.setPciState(new ChildStatusUpdateState());
            pciContext.stateChange(pciContext);
        }

        DmaapNotificationsRepository dmaapNotificationsRepository = BeanUtil
                .getBean(DmaapNotificationsRepository.class);
        String notification = dmaapNotificationsRepository.getNotificationFromQueue();
        if (notification != null) {
            log.debug("notification from sdnr:{}", notification);
            pciContext.setSdnrNotification(notification);
            pciContext.setPciState(new SdnrNotificationHandlingState());
            pciContext.stateChange(pciContext);
        } else {
            pciContext.getNewNotification().setNewNotif(false);
            log.debug("setting new notification to false");
            pciContext.setPciState(this);
            pciContext.stateChange(pciContext);
        }

    }
}