aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager-core/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/performancemanager/impl/PerformanceManagerTask.java
blob: 8fe6b9565edf7226e7ebf8574ab6072e0477d8f6 (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
/*
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * 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.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl;

import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.PerformanceDataProvider;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.PerformanceDataLtp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PerformanceManagerTask implements Runnable {

    private static final Logger LOG = LoggerFactory.getLogger(PerformanceManagerTask.class);
    private static final String LOGMARKER = "PMTick";

    private int tickCounter = 0;

    private final ConcurrentHashMap<String, PerformanceDataProvider> queue = new ConcurrentHashMap<>();
    private final DataProvider databaseService;
    private final ScheduledExecutorService scheduler;
    private final long seconds;

    private ScheduledFuture<?> taskHandle = null;
    private Iterator<PerformanceDataProvider> neIterator = null;
    private PerformanceDataProvider actualNE = null;
    private final NetconfNetworkElementService netconfNetworkElementService;

    /**
     * Constructor of PM Task
     * 
     * @param seconds seconds to call PM Task
     * @param microwaveHistoricalPerformanceWriterService DB Service to load PM data to
     * @param netconfNetworkElementService to write into log
     */

    public PerformanceManagerTask(long seconds, DataProvider microwaveHistoricalPerformanceWriterService,
            NetconfNetworkElementService netconfNetworkElementService) {

        LOG.debug("Init task {}", PerformanceManagerTask.class.getSimpleName());
        this.seconds = seconds;
        this.databaseService = microwaveHistoricalPerformanceWriterService;
        this.scheduler = Executors.newSingleThreadScheduledExecutor();
        this.netconfNetworkElementService = netconfNetworkElementService;

    }

    /**
     * Start PM Task
     */
    public void start() {
        LOG.info("PM task created");
        taskHandle = this.scheduler.scheduleAtFixedRate(this, 0, seconds, TimeUnit.SECONDS);
        LOG.info("PM task scheduled");
    }

    /**
     * Stop everything
     */
    public void stop() {
        LOG.info("Stop {}", PerformanceManagerImpl.class.getSimpleName());
        if (taskHandle != null) {
            taskHandle.cancel(true);
            try {
                scheduler.awaitTermination(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                LOG.debug("Schdule stopped.", e);
                // Restore interrupted state...
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * Add NE/Mountpoint to PM Processig
     * 
     * @param mountPointNodeName to be added
     * @param ne that is connected to the mountpoint
     */
    public void registration(String mountPointNodeName, NetworkElement ne) {

        Optional<PerformanceDataProvider> oPmNe = ne.getService(PerformanceDataProvider.class);
        if (oPmNe.isPresent()) {
            queue.put(mountPointNodeName, oPmNe.get());
        }
    }

    /**
     * Remove mountpoint/NE from PM process
     * 
     * @param mountPointNodeName that has to be removed
     */
    public void deRegistration(String mountPointNodeName) {
        LOG.debug("Deregister {}", mountPointNodeName);
        PerformanceDataProvider removedNE = queue.remove(mountPointNodeName);

        if (removedNE == null) {
            LOG.warn("Couldn't delete {}", mountPointNodeName);
        }
    }


    /*--------------------------------------------------------------
     * Task to read PM data from NE
     */

    /**
     * Task runner to read all performance data from Network Elements. Catch exceptions to make sure, that the Task is
     * not stopped.
     */
    @Override
    public void run() {

        String mountpointName = "No NE";
        if (actualNE != null && actualNE.getAcessor().isPresent()) {
            mountpointName = actualNE.getAcessor().get().getNodeId().getValue();
        }
        LOG.debug("{} start {} Start with mountpoint {}", LOGMARKER, tickCounter, mountpointName);

        //Proceed to next NE/Interface
        getNextInterface(mountpointName);

        LOG.debug("{} {} Next interface to handle {}", LOGMARKER, tickCounter,
                actualNE == null ? "No NE/IF" : actualNE.pmStatusToString());

        if (actualNE != null) {
            try {
                LOG.debug("{} Start to read PM from NE ({})", LOGMARKER, tickCounter);
                Optional<PerformanceDataLtp> allPm = actualNE.getLtpHistoricalPerformanceData();
                if (allPm.isPresent()) {
                    LOG.debug("{} {} Got PM list. Start write to DB", LOGMARKER, tickCounter);
                    databaseService.doWritePerformanceData(allPm.get().getList());
                }
                LOG.debug("{} {} PM List end.", LOGMARKER, tickCounter);
            } catch (Exception e) {
                StringBuffer msg = new StringBuffer();
                msg.append(e.getMessage());
                msg.append(" - ");
                msg.append(e.getCause().toString());
                msg.append(" - ");
                msg.append(actualNE.pmStatusToString());
                String msgString = msg.toString();
                LOG.warn("{} {} PM read/write failed. Write log entry {}", LOGMARKER, tickCounter, msgString);
                netconfNetworkElementService.writeToEventLog(mountpointName, "PM Problem", msgString);
            }
        }

        LOG.debug("{} end {}", LOGMARKER, tickCounter);
        tickCounter++;
    }

    /**
     * Reset queue to start from beginning
     */
    private void resetQueue() {
        actualNE = null;
        neIterator = null;
    }

    /**
     * Get then next interface in the list. First try to find a next on the actual NE. If not available search next
     * interface at a NE Special Situations to handle: Empty queue, NEs, but no interfaces
     */
    private void getNextInterface(String mountpointName) {
        boolean started = false;
        int loopCounter = 0;

        LOG.debug("{} {} getNextInterface enter. Queue size {} ", LOGMARKER, tickCounter, queue.size());

        if (actualNE != null && !queue.containsValue(actualNE)) {
            LOG.debug("{} {} NE Removed duringprocessing A", LOGMARKER, tickCounter);
            resetQueue();
        }

        while (true) {

            if (loopCounter++ >= 1000) {
                LOG.error("{} {} Problem in PM iteration. endless condition reached", LOGMARKER, tickCounter);
                resetQueue();
                break;
            }

            LOG.debug("{} {} Loop ne {}:neiterator {}:Interfaceiterator:{} Loop:{}", LOGMARKER, tickCounter,
                    actualNE == null ? "null" : mountpointName, neIterator == null ? "null" : neIterator.hasNext(),
                    actualNE == null ? "null" : actualNE.hasNext(), loopCounter);

            if (actualNE != null && actualNE.hasNext()) {
                // Yes, there is an interface, deliver back
                LOG.debug("{} {} getNextInterface yes A", LOGMARKER, tickCounter);
                actualNE.next();
                break;

            } else {
                // No element in neInterfaceInterator .. get next NE and try
                if (neIterator != null && neIterator.hasNext()) {
                    // Set a new NE
                    LOG.debug("{} {} Next NE A", LOGMARKER, tickCounter);
                    actualNE = neIterator.next();
                    actualNE.resetPMIterator();

                } else {
                    // Goto start condition 1) first entry 2) end of queue reached
                    LOG.debug("{} {} Reset", LOGMARKER, tickCounter);
                    resetQueue();

                    if (queue.isEmpty()) {
                        LOG.debug("{} {} no nextInterfac. queue empty", LOGMARKER, tickCounter);
                        break;
                    } else if (!started) {
                        LOG.debug("{} {} getNextInterface start condition. Get interator.", LOGMARKER, tickCounter);
                        neIterator = queue.values().iterator();
                        started = true;
                    } else {
                        LOG.debug("{} {} no nextInterface", LOGMARKER, tickCounter);
                        break;
                    }
                }
            }
        } //while

        if (actualNE != null && !queue.containsValue(actualNE)) {
            LOG.debug("{} {} NE Removed duringprocessing B", LOGMARKER, tickCounter);
            resetQueue();
        }

    }
}