2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.performancemanager.impl;
20 import java.util.Iterator;
21 import java.util.Optional;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.ScheduledExecutorService;
25 import java.util.concurrent.ScheduledFuture;
26 import java.util.concurrent.TimeUnit;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.PerformanceDataProvider;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NetconfNetworkElementService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.PerformanceDataLtp;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class PerformanceManagerTask implements Runnable {
37 private static final Logger LOG = LoggerFactory.getLogger(PerformanceManagerTask.class);
38 private static final String LOGMARKER = "PMTick";
40 private int tickCounter = 0;
42 private final ConcurrentHashMap<String, PerformanceDataProvider> queue = new ConcurrentHashMap<>();
43 private final DataProvider databaseService;
44 private final ScheduledExecutorService scheduler;
45 private final long seconds;
47 private ScheduledFuture<?> taskHandle = null;
48 private Iterator<PerformanceDataProvider> neIterator = null;
49 private PerformanceDataProvider actualNE = null;
50 private final NetconfNetworkElementService netconfNetworkElementService;
53 * Constructor of PM Task
55 * @param seconds seconds to call PM Task
56 * @param microwaveHistoricalPerformanceWriterService DB Service to load PM data to
57 * @param netconfNetworkElementService to write into log
60 public PerformanceManagerTask(long seconds, DataProvider microwaveHistoricalPerformanceWriterService,
61 NetconfNetworkElementService netconfNetworkElementService) {
63 LOG.info("Init task {} handling time {} seconds", PerformanceManagerTask.class.getSimpleName(), seconds);
64 this.seconds = seconds;
65 this.databaseService = microwaveHistoricalPerformanceWriterService;
66 this.scheduler = Executors.newSingleThreadScheduledExecutor();
67 this.netconfNetworkElementService = netconfNetworkElementService;
75 LOG.info("PM task created");
76 taskHandle = this.scheduler.scheduleAtFixedRate(this, 0, seconds, TimeUnit.SECONDS);
77 LOG.info("PM task scheduled");
84 LOG.info("Stop {}", PerformanceManagerImpl.class.getSimpleName());
85 if (taskHandle != null) {
86 taskHandle.cancel(true);
88 scheduler.awaitTermination(10, TimeUnit.SECONDS);
89 } catch (InterruptedException e) {
90 LOG.debug("Scheduler stopped.", e);
91 // Restore interrupted state...
92 Thread.currentThread().interrupt();
98 * Add NE/Mountpoint to PM Processig
100 * @param mountPointNodeName to be added
101 * @param ne that is connected to the mountpoint
103 public void registration(String mountPointNodeName, NetworkElement ne) {
105 Optional<PerformanceDataProvider> oPmNe = ne.getService(PerformanceDataProvider.class);
106 if (oPmNe.isPresent()) {
107 queue.put(mountPointNodeName, oPmNe.get());
112 * Remove mountpoint/NE from PM process
114 * @param mountPointNodeName that has to be removed
116 public void deRegistration(String mountPointNodeName) {
117 LOG.debug("Deregister {}", mountPointNodeName);
118 PerformanceDataProvider removedNE = queue.remove(mountPointNodeName);
120 if (removedNE == null) {
121 LOG.warn("Couldn't delete {}", mountPointNodeName);
125 /*--------------------------------------------------------------
126 * Task to read PM data from NE
130 * Task runner to read all performance data from Network Elements. Catch exceptions to make sure, that the Task is
136 String mountpointName = "No NE";
137 if (actualNE != null && actualNE.getAcessor().isPresent()) {
138 mountpointName = actualNE.getAcessor().get().getNodeId().getValue();
140 LOG.debug("{} start {} Start with mountpoint {}", LOGMARKER, tickCounter, mountpointName);
142 // Proceed to next NE/Interface
143 getNextInterface(mountpointName);
145 LOG.debug("{} {} Next interface to handle {}", LOGMARKER, tickCounter,
146 actualNE == null ? "No NE/IF" : actualNE.pmStatusToString());
148 if (actualNE != null) {
150 LOG.debug("{} Start to read PM from NE ({})", LOGMARKER, tickCounter);
151 Optional<PerformanceDataLtp> allPm = actualNE.getLtpHistoricalPerformanceData();
152 if (allPm.isPresent()) {
153 LOG.debug("{} {} Got PM list. Start write to DB", LOGMARKER, tickCounter);
154 databaseService.doWritePerformanceData(allPm.get().getList());
156 LOG.debug("{} {} PM List end.", LOGMARKER, tickCounter);
157 } catch (Throwable e) {
158 LOG.debug("{} {} PM Exception", LOGMARKER, tickCounter);
159 String msg = new StringBuffer().append(e.getMessage()).toString();
160 LOG.warn("{} {} PM read/write failed. Write log entry {}", LOGMARKER, tickCounter, msg);
161 netconfNetworkElementService.writeToEventLog(mountpointName, "PM Problem", msg);
165 LOG.debug("{} end {}", LOGMARKER, tickCounter);
170 * Reset queue to start from beginning
172 private void resetQueue() {
178 * Get then next interface in the list. First try to find a next on the actual NE. If not available search next
179 * interface at a NE Special Situations to handle: Empty queue, NEs, but no interfaces
181 private void getNextInterface(String mountpointName) {
182 boolean started = false;
185 LOG.debug("{} {} getNextInterface enter. Queue size {} ", LOGMARKER, tickCounter, queue.size());
187 if (actualNE != null && !queue.containsValue(actualNE)) {
188 LOG.debug("{} {} NE Removed duringprocessing A", LOGMARKER, tickCounter);
194 if (loopCounter++ >= 1000) {
195 LOG.error("{} {} Problem in PM iteration. endless condition reached", LOGMARKER, tickCounter);
200 LOG.debug("{} {} Loop ne {}:neiterator {}:Interfaceiterator:{} Loop:{}", LOGMARKER, tickCounter,
201 actualNE == null ? "null" : mountpointName, neIterator == null ? "null" : neIterator.hasNext(),
202 actualNE == null ? "null" : actualNE.hasNext(), loopCounter);
204 if (actualNE != null && actualNE.hasNext()) {
205 // Yes, there is an interface, deliver back
206 LOG.debug("{} {} getNextInterface yes A", LOGMARKER, tickCounter);
211 // No element in neInterfaceInterator .. get next NE and try
212 if (neIterator != null && neIterator.hasNext()) {
214 LOG.debug("{} {} Next NE A", LOGMARKER, tickCounter);
215 actualNE = neIterator.next();
216 actualNE.resetPMIterator();
219 // Goto start condition 1) first entry 2) end of queue reached
220 LOG.debug("{} {} Reset", LOGMARKER, tickCounter);
223 if (queue.isEmpty()) {
224 LOG.debug("{} {} no nextInterfac. queue empty", LOGMARKER, tickCounter);
226 } else if (!started) {
227 LOG.debug("{} {} getNextInterface start condition. Get interator.", LOGMARKER, tickCounter);
228 neIterator = queue.values().iterator();
231 LOG.debug("{} {} no nextInterface", LOGMARKER, tickCounter);
238 if (actualNE != null && !queue.containsValue(actualNE)) {
239 LOG.debug("{} {} NE Removed duringprocessing B", LOGMARKER, tickCounter);