Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / task / PerformElasticSearchUpdate.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.synchronizer.task;
27
28 import java.util.Map;
29 import java.util.function.Supplier;
30
31 import org.openecomp.sparky.dal.NetworkTransaction;
32 import org.openecomp.sparky.dal.elasticsearch.ElasticSearchDataProvider;
33 import org.openecomp.sparky.dal.rest.OperationResult;
34 import org.slf4j.MDC;
35
36 /**
37  * The Class PerformElasticSearchUpdate.
38  */
39 public class PerformElasticSearchUpdate implements Supplier<NetworkTransaction> {
40
41   private ElasticSearchDataProvider esDataProvider;
42   private NetworkTransaction operationTracker;
43   private String updatePayload;
44   private String updateUrl;
45   private Map<String, String> contextMap;
46
47   /**
48    * Instantiates a new perform elastic search update.
49    *
50    * @param updateUrl the update url
51    * @param updatePayload the update payload
52    * @param esDataProvider the es data provider
53    * @param transactionTracker the transaction tracker
54    */
55   public PerformElasticSearchUpdate(String updateUrl, String updatePayload,
56       ElasticSearchDataProvider esDataProvider, NetworkTransaction transactionTracker) {
57     this.updateUrl = updateUrl;
58     this.updatePayload = updatePayload;
59     this.esDataProvider = esDataProvider;
60     this.contextMap = MDC.getCopyOfContextMap();
61     this.operationTracker = new NetworkTransaction();
62     operationTracker.setEntityType(transactionTracker.getEntityType());
63     operationTracker.setDescriptor(transactionTracker.getDescriptor());
64     operationTracker.setOperationType(transactionTracker.getOperationType());
65   }
66
67   /* (non-Javadoc)
68    * @see java.util.function.Supplier#get()
69    */
70   @Override
71   public NetworkTransaction get() {
72     operationTracker.setTaskAgeInMs();
73     long startTimeInMs = System.currentTimeMillis();
74     MDC.setContextMap(contextMap);
75     OperationResult or = esDataProvider.doBulkOperation(updateUrl, updatePayload);
76
77     or.setResponseTimeInMs(System.currentTimeMillis() - startTimeInMs);
78     operationTracker.setOperationResult(or);
79
80     return operationTracker;
81   }
82
83 }