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