Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / task / PerformActiveInventoryRetrieval.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.synchronizer.task;
24
25 import java.util.Map;
26 import java.util.function.Supplier;
27
28 import org.openecomp.sparky.dal.NetworkTransaction;
29 import org.openecomp.sparky.dal.aai.ActiveInventoryDataProvider;
30 import org.openecomp.sparky.dal.rest.OperationResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.slf4j.MDC;
34
35 /*
36  * Consider abstraction the tasks into common elemnts, because most of them repeat a generic call
37  * flow pattern
38  */
39
40 /**
41  * The Class PerformActiveInventoryRetrieval.
42  */
43 public class PerformActiveInventoryRetrieval implements Supplier<NetworkTransaction> {
44
45   private static Logger logger = LoggerFactory.getLogger(PerformActiveInventoryRetrieval.class);
46
47   private NetworkTransaction txn;
48   private ActiveInventoryDataProvider aaiProvider;
49   private Map<String, String> contextMap;
50
51   /**
52    * Instantiates a new perform active inventory retrieval.
53    *
54    * @param txn the txn
55    * @param aaiProvider the aai provider
56    */
57   public PerformActiveInventoryRetrieval(NetworkTransaction txn,
58       ActiveInventoryDataProvider aaiProvider) {
59     this.txn = txn;
60     this.aaiProvider = aaiProvider;
61     this.contextMap = MDC.getCopyOfContextMap();
62   }
63
64   /* (non-Javadoc)
65    * @see java.util.function.Supplier#get()
66    */
67   @Override
68   public NetworkTransaction get() {
69
70     txn.setTaskAgeInMs();
71
72     long startTimeInMs = System.currentTimeMillis();
73     MDC.setContextMap(contextMap);
74     OperationResult result = null;
75     try {
76       // todo: use proper config instead of hard-coding parameters
77       result = aaiProvider.queryActiveInventoryWithRetries(txn.getLink(), "application/json", 5);
78     } catch (Exception exc) {
79       logger.error("Failure to resolve self link from AAI.  Error = ", exc);
80       result = new OperationResult(500,
81           "Caught an exception while trying to resolve link = " + exc.getMessage());
82     } finally {
83       result.setResponseTimeInMs(System.currentTimeMillis() - startTimeInMs);
84       txn.setOperationResult(result);
85     }
86
87     return txn;
88   }
89
90 }