Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / 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.onap.aai.sparky.synchronizer.task;
24
25 import java.util.Map;
26 import java.util.function.Supplier;
27
28 import org.onap.aai.sparky.dal.NetworkTransaction;
29 import org.onap.aai.sparky.dal.aai.ActiveInventoryDataProvider;
30 import org.onap.aai.sparky.dal.aai.config.ActiveInventoryConfig;
31 import org.onap.aai.sparky.dal.rest.OperationResult;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.slf4j.MDC;
35
36 /*
37  * Consider abstraction the tasks into common elemnts, because most of them repeat a generic call
38  * flow pattern
39  */
40
41 /**
42  * The Class PerformActiveInventoryRetrieval.
43  */
44 public class PerformActiveInventoryRetrieval implements Supplier<NetworkTransaction> {
45
46   private static Logger logger = LoggerFactory.getLogger(PerformActiveInventoryRetrieval.class);
47
48   private NetworkTransaction txn;
49   private ActiveInventoryDataProvider aaiProvider;
50   private Map<String, String> contextMap;
51
52   /**
53    * Instantiates a new perform active inventory retrieval.
54    *
55    * @param txn the txn
56    * @param aaiProvider the aai provider
57    */
58   public PerformActiveInventoryRetrieval(NetworkTransaction txn,
59       ActiveInventoryDataProvider aaiProvider) {
60     this.txn = txn;
61     this.aaiProvider = aaiProvider;
62     this.contextMap = MDC.getCopyOfContextMap();
63   }
64
65   /*
66    * (non-Javadoc)
67    * 
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       final String absoluteSelfLink =
81           ActiveInventoryConfig.getConfig().repairSelfLink(txn.getLink());
82       result = aaiProvider.queryActiveInventoryWithRetries(absoluteSelfLink, "application/json", 5);
83     } catch (Exception exc) {
84       logger.error("Failure to resolve self link from AAI.  Error = ", exc);
85       result = new OperationResult(500,
86           "Caught an exception while trying to resolve link = " + exc.getMessage());
87     } finally {
88       result.setResponseTimeInMs(System.currentTimeMillis() - startTimeInMs);
89       txn.setOperationResult(result);
90     }
91
92     return txn;
93   }
94
95   /**
96    * @return the logger
97    */
98   public static Logger getLogger() {
99     return logger;
100   }
101
102   /**
103    * @param logger the logger to set
104    */
105   public static void setLogger(Logger logger) {
106     PerformActiveInventoryRetrieval.logger = logger;
107   }
108
109   /**
110    * @return the txn
111    */
112   public NetworkTransaction getTxn() {
113     return txn;
114   }
115
116   /**
117    * @param txn the txn to set
118    */
119   public void setTxn(NetworkTransaction txn) {
120     this.txn = txn;
121   }
122
123   /**
124    * @return the aaiProvider
125    */
126   public ActiveInventoryDataProvider getAaiProvider() {
127     return aaiProvider;
128   }
129
130   /**
131    * @param aaiProvider the aaiProvider to set
132    */
133   public void setAaiProvider(ActiveInventoryDataProvider aaiProvider) {
134     this.aaiProvider = aaiProvider;
135   }
136
137   /**
138    * @return the contextMap
139    */
140   public Map<String, String> getContextMap() {
141     return contextMap;
142   }
143
144   /**
145    * @param contextMap the contextMap to set
146    */
147   public void setContextMap(Map<String, String> contextMap) {
148     this.contextMap = contextMap;
149   }
150
151 }