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