Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / viewandinspect / task / PerformSelfLinkDeterminationTask.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.viewandinspect.task;
22
23 import java.util.Map;
24 import java.util.function.Supplier;
25
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.restclient.client.OperationResult;
29 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
30 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import org.onap.aai.sparky.viewandinspect.entity.SelfLinkDeterminationTransaction;
32 import org.slf4j.MDC;
33
34 public class PerformSelfLinkDeterminationTask implements Supplier<SelfLinkDeterminationTransaction> {
35
36   private static final Logger logger =
37       LoggerFactory.getInstance().getLogger(PerformSelfLinkDeterminationTask.class);
38
39   private SelfLinkDeterminationTransaction txn;
40   private ActiveInventoryAdapter aaiAdapter;
41   private Map<String, String> contextMap;
42
43
44   /**
45    * Instantiates a new perform node self link processing task.
46    *
47    * @param txn the txn
48    * @param requestParameters the request parameters
49    * @param aaiProvider the aai provider
50    */
51   public PerformSelfLinkDeterminationTask(SelfLinkDeterminationTransaction txn, String requestParameters,
52       ActiveInventoryAdapter aaiAdapter) {
53     
54     this.aaiAdapter = aaiAdapter;
55     this.txn = txn;
56     this.contextMap = MDC.getCopyOfContextMap();
57   }
58
59   /* (non-Javadoc)
60    * @see java.util.function.Supplier#get()
61    */
62   @Override
63   public SelfLinkDeterminationTransaction get() {
64     MDC.setContextMap(contextMap);
65     if (txn.getQueryString() == null) {
66       OperationResult opResult = new OperationResult();
67       opResult.setResult(500, "Aborting self-link determination because self link query is null.");
68       txn.setOpResult(opResult);
69       return txn;
70     }
71
72     OperationResult opResult = null;
73     try {
74       opResult = aaiAdapter.queryActiveInventoryWithRetries(txn.getQueryString(), "application/json",
75           aaiAdapter.getEndpointConfig().getNumRequestRetries());
76     } catch (Exception exc) {
77       opResult = new OperationResult();
78       opResult.setResult(500, "Querying AAI with retry failed due to an exception.");
79       logger.error(AaiUiMsgs.ERROR_AAI_QUERY_WITH_RETRY, exc.getMessage());
80     }
81
82     if (logger.isDebugEnabled()) {
83       logger.debug("Operation result = " + opResult.toString());
84     }
85
86     txn.setOpResult(opResult);
87     return txn;
88
89   }
90
91 }