Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / viewandinspect / task / PerformSelfLinkDeterminationTask.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.viewandinspect.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.aai.config.ActiveInventoryConfig;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
34 import org.onap.aai.sparky.viewandinspect.entity.SelfLinkDeterminationTransaction;
35 import org.slf4j.MDC;
36
37 public class PerformSelfLinkDeterminationTask
38     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,
56       String requestParameters, ActiveInventoryAdapter aaiAdapter) {
57
58     this.aaiAdapter = aaiAdapter;
59     this.txn = txn;
60     this.contextMap = MDC.getCopyOfContextMap();
61   }
62
63   /*
64    * (non-Javadoc)
65    * 
66    * @see java.util.function.Supplier#get()
67    */
68   @Override
69   public SelfLinkDeterminationTransaction get() {
70     MDC.setContextMap(contextMap);
71     if (txn.getQueryString() == null) {
72       OperationResult opResult = new OperationResult();
73       opResult.setResult(500, "Aborting self-link determination because self link query is null.");
74       txn.setOpResult(opResult);
75       return txn;
76     }
77
78     OperationResult opResult = null;
79     try {
80       opResult =
81           aaiAdapter.queryActiveInventoryWithRetries(txn.getQueryString(), "application/json",
82               ActiveInventoryConfig.getConfig().getAaiRestConfig().getNumRequestRetries());
83     } catch (Exception exc) {
84       opResult = new OperationResult();
85       opResult.setResult(500, "Querying AAI with retry failed due to an exception.");
86       logger.error(AaiUiMsgs.ERROR_AAI_QUERY_WITH_RETRY, exc.getMessage());
87     }
88
89     if (logger.isDebugEnabled()) {
90       logger.debug("Operation result = " + opResult.toString());
91     }
92
93     txn.setOpResult(opResult);
94     return txn;
95
96   }
97
98 }