Fixing the startup after junit increase
[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   /* (non-Javadoc)
66    * @see java.util.function.Supplier#get()
67    */
68   @Override
69   public NetworkTransaction get() {
70
71     txn.setTaskAgeInMs();
72
73     long startTimeInMs = System.currentTimeMillis();
74     MDC.setContextMap(contextMap);
75     OperationResult result = null;
76     try {
77       // todo: use proper config instead of hard-coding parameters
78       final String absoluteSelfLink = ActiveInventoryConfig.getConfig().repairSelfLink(txn.getLink());
79       result = aaiProvider.queryActiveInventoryWithRetries(absoluteSelfLink, "application/json", 5);
80     } catch (Exception exc) {
81       logger.error("Failure to resolve self link from AAI.  Error = ", exc);
82       result = new OperationResult(500,
83           "Caught an exception while trying to resolve link = " + exc.getMessage());
84     } finally {
85       result.setResponseTimeInMs(System.currentTimeMillis() - startTimeInMs);
86       txn.setOperationResult(result);
87     }
88
89     return txn;
90   }
91
92   /**
93    * @return the logger
94    */
95   public static Logger getLogger() {
96     return logger;
97   }
98
99   /**
100    * @param logger the logger to set
101    */
102   public static void setLogger(Logger logger) {
103     PerformActiveInventoryRetrieval.logger = logger;
104   }
105
106   /**
107    * @return the txn
108    */
109   public NetworkTransaction getTxn() {
110     return txn;
111   }
112
113   /**
114    * @param txn the txn to set
115    */
116   public void setTxn(NetworkTransaction txn) {
117     this.txn = txn;
118   }
119
120   /**
121    * @return the aaiProvider
122    */
123   public ActiveInventoryDataProvider getAaiProvider() {
124     return aaiProvider;
125   }
126
127   /**
128    * @param aaiProvider the aaiProvider to set
129    */
130   public void setAaiProvider(ActiveInventoryDataProvider aaiProvider) {
131     this.aaiProvider = aaiProvider;
132   }
133
134   /**
135    * @return the contextMap
136    */
137   public Map<String, String> getContextMap() {
138     return contextMap;
139   }
140
141   /**
142    * @param contextMap the contextMap to set
143    */
144   public void setContextMap(Map<String, String> contextMap) {
145     this.contextMap = contextMap;
146   }
147
148 }