Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / synchronizer / task / PerformActiveInventoryRetrieval.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.aai.ActiveInventoryDataProvider;
33 import org.openecomp.sparky.dal.rest.OperationResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37
38 /*
39  * Consider abstraction the tasks into common elemnts, because most of them repeat a generic call
40  * flow pattern
41  */
42
43 /**
44  * The Class PerformActiveInventoryRetrieval.
45  */
46 public class PerformActiveInventoryRetrieval implements Supplier<NetworkTransaction> {
47
48   private static Logger logger = LoggerFactory.getLogger(PerformActiveInventoryRetrieval.class);
49
50   private NetworkTransaction txn;
51   private ActiveInventoryDataProvider aaiProvider;
52   private Map<String, String> contextMap;
53
54   /**
55    * Instantiates a new perform active inventory retrieval.
56    *
57    * @param txn the txn
58    * @param aaiProvider the aai provider
59    */
60   public PerformActiveInventoryRetrieval(NetworkTransaction txn,
61       ActiveInventoryDataProvider aaiProvider) {
62     this.txn = txn;
63     this.aaiProvider = aaiProvider;
64     this.contextMap = MDC.getCopyOfContextMap();
65   }
66
67   /* (non-Javadoc)
68    * @see java.util.function.Supplier#get()
69    */
70   @Override
71   public NetworkTransaction get() {
72
73     txn.setTaskAgeInMs();
74
75     long startTimeInMs = System.currentTimeMillis();
76     MDC.setContextMap(contextMap);
77     OperationResult result = null;
78     try {
79       // todo: use proper config instead of hard-coding parameters
80       result = aaiProvider.queryActiveInventoryWithRetries(txn.getLink(), "application/json", 5);
81     } catch (Exception exc) {
82       logger.error("Failure to resolve self link from AAI.  Error = ", exc);
83       result = new OperationResult(500,
84           "Caught an exception while trying to resolve link = " + exc.getMessage());
85     } finally {
86       result.setResponseTimeInMs(System.currentTimeMillis() - startTimeInMs);
87       txn.setOperationResult(result);
88     }
89
90     return txn;
91   }
92
93 }