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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* Copyright © 2017-2018 Amdocs
* ================================================================================
* 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.aai.sparky.aggregation.sync;
import static java.util.concurrent.CompletableFuture.supplyAsync;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import javax.json.Json;
import javax.ws.rs.core.MediaType;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.cl.mdc.MdcContext;
import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.sparky.config.oxm.SearchableEntityLookup;
import org.onap.aai.sparky.config.oxm.SearchableOxmEntityDescriptor;
import org.onap.aai.sparky.dal.rest.HttpMethod;
import org.onap.aai.sparky.logging.AaiUiMsgs;
import org.onap.aai.sparky.sync.AbstractEntitySynchronizer;
import org.onap.aai.sparky.sync.IndexSynchronizer;
import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
import org.onap.aai.sparky.sync.config.NetworkStatisticsConfig;
import org.onap.aai.sparky.sync.enumeration.OperationState;
import org.onap.aai.sparky.sync.enumeration.SynchronizerState;
import org.onap.aai.sparky.util.NodeUtils;
import org.slf4j.MDC;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
/**
* The Class HistoricalEntitySummarizer.
*/
public class HistoricalEntitySummarizer extends AbstractEntitySynchronizer
implements IndexSynchronizer {
private static final Logger LOG = LoggerFactory.getInstance().getLogger(HistoricalEntitySummarizer.class);
private static final String INSERTION_DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmssZ";
private boolean allWorkEnumerated;
private ConcurrentHashMap<String, AtomicInteger> entityCounters;
private boolean syncInProgress;
private Map<String, String> contextMap;
private ElasticSearchSchemaConfig schemaConfig;
private SearchableEntityLookup searchableEntityLookup;
/**
* Instantiates a new historical entity summarizer.
*
* @throws Exception the exception
*/
public HistoricalEntitySummarizer(ElasticSearchSchemaConfig schemaConfig, int internalSyncWorkers,
int aaiWorkers, int esWorkers, NetworkStatisticsConfig aaiStatConfig,
NetworkStatisticsConfig esStatConfig, SearchableEntityLookup searchableEntityLookup)
throws Exception {
super(LOG, "HES", internalSyncWorkers, aaiWorkers, esWorkers, schemaConfig.getIndexName(), aaiStatConfig, esStatConfig);
this.schemaConfig = schemaConfig;
this.allWorkEnumerated = false;
this.entityCounters = new ConcurrentHashMap<String, AtomicInteger>();
this.synchronizerName = "Historical Entity Summarizer";
this.enabledStatFlags = EnumSet.of(StatFlag.AAI_REST_STATS, StatFlag.ES_REST_STATS);
this.syncInProgress = false;
this.contextMap = MDC.getCopyOfContextMap();
this.syncDurationInMs = -1;
this.searchableEntityLookup = searchableEntityLookup;
}
/**
* Collect all the work.
*
* @return the operation state
*/
private OperationState collectAllTheWork() {
Map<String, SearchableOxmEntityDescriptor> descriptorMap =
searchableEntityLookup.getSearchableEntityDescriptors();
if (descriptorMap.isEmpty()) {
LOG.error(AaiUiMsgs.OXM_FAILED_RETRIEVAL, "historical entities");
return OperationState.ERROR;
}
Collection<String> entityTypes = descriptorMap.keySet();
AtomicInteger asyncWoH = new AtomicInteger(0);
asyncWoH.set(entityTypes.size());
try {
for (String entityType : entityTypes) {
supplyAsync(new Supplier<Void>() {
@Override
public Void get() {
MDC.setContextMap(contextMap);
try {
OperationResult typeLinksResult =
aaiAdapter.getSelfLinksByEntityType(entityType);
updateActiveInventoryCounters(HttpMethod.GET, entityType, typeLinksResult);
processEntityTypeSelfLinks(entityType, typeLinksResult);
} catch (Exception exc) {
LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc.getMessage());
}
return null;
}
}, aaiExecutor).whenComplete((result, error) -> {
asyncWoH.decrementAndGet();
if (error != null) {
LOG.error(AaiUiMsgs.HISTORICAL_COLLECT_ERROR, error.getMessage());
}
});
}
while (asyncWoH.get() > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug(AaiUiMsgs.DEBUG_GENERIC, indexName + " summarizer waiting for all the links to be processed.");
}
Thread.sleep(250);
}
esWorkOnHand.set(entityCounters.size());
// start doing the real work
allWorkEnumerated = true;
insertEntityTypeCounters();
if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder(128);
sb.append("\n\nHistorical Entity Counters:");
for (Entry<String, AtomicInteger> entry : entityCounters.entrySet()) {
sb.append("\n").append(entry.getKey()).append(" = ").append(entry.getValue().get());
}
LOG.debug(AaiUiMsgs.DEBUG_GENERIC, sb.toString());
}
} catch (Exception exc) {
LOG.error(AaiUiMsgs.HISTORICAL_COLLECT_ERROR, exc.getMessage());
esWorkOnHand.set(0);
allWorkEnumerated = true;
return OperationState.ERROR;
}
return OperationState.OK;
}
/* (non-Javadoc)
* @see org.openecomp.sparky.synchronizer.IndexSynchronizer#doSync()
*/
@Override
public OperationState doSync() {
this.syncDurationInMs = -1;
String txnID = NodeUtils.getRandomTxnId();
MdcContext.initialize(txnID, "HistoricalEntitySynchronizer", "", "Sync", "");
if (syncInProgress) {
LOG.info(AaiUiMsgs.HISTORICAL_SYNC_PENDING);
return OperationState.PENDING;
}
clearCache();
syncInProgress = true;
this.syncStartedTimeStampInMs = System.currentTimeMillis();
allWorkEnumerated = false;
return collectAllTheWork();
}
/**
* Process entity type self links.
*
* @param entityType the entity type
* @param operationResult the operation result
*/
private void processEntityTypeSelfLinks(String entityType, OperationResult operationResult) {
JsonNode rootNode = null;
final String jsonResult = operationResult.getResult();
if (jsonResult != null && jsonResult.length() > 0 && operationResult.wasSuccessful()) {
try {
rootNode = mapper.readTree(jsonResult);
} catch (IOException exc) {
LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc.getMessage());
return;
}
JsonNode resultData = rootNode.get("result-data");
ArrayNode resultDataArrayNode = null;
if (resultData != null && resultData.isArray()) {
resultDataArrayNode = (ArrayNode) resultData;
entityCounters.put(entityType, new AtomicInteger(resultDataArrayNode.size()));
}
}
}
/**
* Insert entity type counters.
*/
private void insertEntityTypeCounters() {
if (esWorkOnHand.get() <= 0) {
return;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(INSERTION_DATE_TIME_FORMAT);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String currentFormattedTimeStamp = dateFormat.format(timestamp);
Set<Entry<String, AtomicInteger>> entityCounterEntries = entityCounters.entrySet();
for (Entry<String, AtomicInteger> entityCounterEntry : entityCounterEntries) {
supplyAsync(new Supplier<Void>() {
@Override
public Void get() {
MDC.setContextMap(contextMap);
String jsonString = Json.createObjectBuilder().add(
"count", entityCounterEntry.getValue().get())
.add("entityType", entityCounterEntry.getKey())
.add("timestamp", currentFormattedTimeStamp).build().toString();
String link = null;
try {
link = elasticSearchAdapter.buildElasticSearchPostUrl(indexName);
OperationResult or = elasticSearchAdapter.doPost(link, jsonString, MediaType.APPLICATION_JSON_TYPE);
updateElasticSearchCounters(HttpMethod.POST, entityCounterEntry.getKey(), or);
} catch (Exception exc) {
LOG.error(AaiUiMsgs.ES_STORE_FAILURE, exc.getMessage() );
}
return null;
}
}, esExecutor).whenComplete((result, error) -> {
esWorkOnHand.decrementAndGet();
});
}
while (esWorkOnHand.get() > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException exc) {
LOG.error(AaiUiMsgs.INTERRUPTED, "historical Entities", exc.getMessage());
Thread.currentThread().interrupt();
}
}
}
@Override
public SynchronizerState getState() {
if (!isSyncDone()) {
return SynchronizerState.PERFORMING_SYNCHRONIZATION;
}
return SynchronizerState.IDLE;
}
/* (non-Javadoc)
* @see org.openecomp.sparky.synchronizer.IndexSynchronizer#getStatReport(boolean)
*/
@Override
public String getStatReport(boolean showFinalReport) {
syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
return this.getStatReport(syncDurationInMs, showFinalReport);
}
/* (non-Javadoc)
* @see org.openecomp.sparky.synchronizer.IndexSynchronizer#shutdown()
*/
@Override
public void shutdown() {
this.shutdownExecutors();
}
@Override
protected boolean isSyncDone() {
int totalWorkOnHand = aaiWorkOnHand.get() + esWorkOnHand.get();
if (LOG.isDebugEnabled()) {
LOG.debug(AaiUiMsgs.DEBUG_GENERIC,indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand
+ " all work enumerated = " + allWorkEnumerated);
}
if (totalWorkOnHand > 0 || !allWorkEnumerated) {
return false;
}
this.syncInProgress = false;
return true;
}
/* (non-Javadoc)
* @see org.openecomp.sparky.synchronizer.AbstractEntitySynchronizer#clearCache()
*/
@Override
public void clearCache() {
if (syncInProgress) {
LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "Historical Entity Summarizer in progress, request to clear cache ignored");
return;
}
super.clearCache();
this.resetCounters();
if (entityCounters != null) {
entityCounters.clear();
}
allWorkEnumerated = false;
}
}
|