Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / viewandinspect / task / PerformSelfLinkDeterminationTask.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.viewandinspect.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.logging.AaiUiMsgs;
35 import org.onap.aai.sparky.viewandinspect.entity.SelfLinkDeterminationTransaction;
36 import org.slf4j.MDC;
37
38 public class PerformSelfLinkDeterminationTask implements Supplier<SelfLinkDeterminationTransaction> {
39
40   private static final Logger logger =
41       LoggerFactory.getInstance().getLogger(PerformSelfLinkDeterminationTask.class);
42
43   private SelfLinkDeterminationTransaction txn;
44   private ActiveInventoryAdapter aaiAdapter;
45   private Map<String, String> contextMap;
46
47
48   /**
49    * Instantiates a new perform node self link processing task.
50    *
51    * @param txn the txn
52    * @param requestParameters the request parameters
53    * @param aaiProvider the aai provider
54    */
55   public PerformSelfLinkDeterminationTask(SelfLinkDeterminationTransaction txn, String requestParameters,
56       ActiveInventoryAdapter aaiAdapter) {
57     
58     this.aaiAdapter = aaiAdapter;
59     this.txn = txn;
60     this.contextMap = MDC.getCopyOfContextMap();
61   }
62
63   /* (non-Javadoc)
64    * @see java.util.function.Supplier#get()
65    */
66   @Override
67   public SelfLinkDeterminationTransaction get() {
68     MDC.setContextMap(contextMap);
69     if (txn.getQueryString() == null) {
70       OperationResult opResult = new OperationResult();
71       opResult.setResult(500, "Aborting self-link determination because self link query is null.");
72       txn.setOpResult(opResult);
73       return txn;
74     }
75
76     OperationResult opResult = null;
77     try {
78       opResult = aaiAdapter.queryActiveInventoryWithRetries(txn.getQueryString(), "application/json",
79           aaiAdapter.getEndpointConfig().getNumRequestRetries());
80     } catch (Exception exc) {
81       opResult = new OperationResult();
82       opResult.setResult(500, "Querying AAI with retry failed due to an exception.");
83       logger.error(AaiUiMsgs.ERROR_AAI_QUERY_WITH_RETRY, exc.getMessage());
84     }
85
86     if (logger.isDebugEnabled()) {
87       logger.debug("Operation result = " + opResult.toString());
88     }
89
90     txn.setOpResult(opResult);
91     return txn;
92
93   }
94
95 }