Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / 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 package org.onap.aai.sparky.sync.task;
26
27 import java.util.Map;
28 import java.util.function.Supplier;
29
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.restclient.client.OperationResult;
33 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
34 import org.onap.aai.sparky.dal.NetworkTransaction;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
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.getInstance().getLogger(PerformActiveInventoryRetrieval.class);
49
50   private NetworkTransaction txn;
51   private ActiveInventoryAdapter aaiAdapter;
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       ActiveInventoryAdapter aaiAdapter) {
62     this.txn = txn;
63     this.aaiAdapter = aaiAdapter;
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
80       final String absoluteSelfLink = aaiAdapter.repairSelfLink(txn.getLink(), txn.getQueryParameters());
81       result = aaiAdapter.queryActiveInventoryWithRetries(absoluteSelfLink, "application/json", 5);
82     } catch (Exception exc) {
83       logger.error(AaiUiMsgs.ERROR_GENERIC,"Failure to resolve self link from AAI.  Error = " + exc.getMessage());
84       result = new OperationResult(500,
85           "Caught an exception while trying to resolve link = " + exc.getMessage());
86     } finally {
87       txn.setOperationResult(result);
88       txn.setOpTimeInMs(System.currentTimeMillis() - startTimeInMs);
89     }
90
91     return txn;
92   }
93
94   protected void setContextMap(Map<String, String> contextMap) {
95     this.contextMap = contextMap;
96   }
97 }