Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / 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.sync.task;
24
25 import java.util.Map;
26 import java.util.function.Supplier;
27
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.restclient.client.OperationResult;
31 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
32 import org.onap.aai.sparky.dal.NetworkTransaction;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
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 =
47       LoggerFactory.getInstance().getLogger(PerformActiveInventoryRetrieval.class);
48
49   private NetworkTransaction txn;
50   private ActiveInventoryAdapter aaiAdapter;
51   private Map<String, String> contextMap;
52
53   /**
54    * Instantiates a new perform active inventory retrieval.
55    *
56    * @param txn the txn
57    * @param aaiProvider the aai provider
58    */
59   public PerformActiveInventoryRetrieval(NetworkTransaction txn,
60       ActiveInventoryAdapter aaiAdapter) {
61     this.txn = txn;
62     this.aaiAdapter = aaiAdapter;
63     this.contextMap = MDC.getCopyOfContextMap();
64   }
65
66   /*
67    * (non-Javadoc)
68    * 
69    * @see java.util.function.Supplier#get()
70    */
71   @Override
72   public NetworkTransaction get() {
73
74     txn.setTaskAgeInMs();
75
76     long startTimeInMs = System.currentTimeMillis();
77     MDC.setContextMap(contextMap);
78     OperationResult result = null;
79     try {
80
81       final String absoluteSelfLink =
82           aaiAdapter.repairSelfLink(txn.getLink(), txn.getQueryParameters());
83       result = aaiAdapter.queryActiveInventoryWithRetries(absoluteSelfLink, "application/json", 5);
84     } catch (Exception exc) {
85       logger.error(AaiUiMsgs.ERROR_GENERIC,
86           "Failure to resolve self link from AAI.  Error = " + exc.getMessage());
87       result = new OperationResult(500,
88           "Caught an exception while trying to resolve link = " + exc.getMessage());
89     } finally {
90       txn.setOperationResult(result);
91       txn.setOpTimeInMs(System.currentTimeMillis() - startTimeInMs);
92     }
93
94     return txn;
95   }
96
97   protected void setContextMap(Map<String, String> contextMap) {
98     this.contextMap = contextMap;
99   }
100 }