changed the header license to new license
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / viewandinspect / task / PerformNodeSelfLinkProcessingTask.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.NodeProcessingTransaction;
32 import org.slf4j.MDC;
33
34 /**
35  * The Class PerformNodeSelfLinkProcessingTask.
36  */
37 public class PerformNodeSelfLinkProcessingTask implements Supplier<NodeProcessingTransaction> {
38
39   private static final Logger logger =
40       LoggerFactory.getInstance().getLogger(PerformNodeSelfLinkProcessingTask.class);
41
42   private NodeProcessingTransaction txn;
43   private ActiveInventoryAdapter aaiAdapter;
44   private Map<String, String> contextMap;
45
46   /**
47    * Instantiates a new perform node self link processing task.
48    *
49    * @param txn the txn
50    * @param aaiProvider the aai provider
51    * @param aaiConfig the aai config
52    */
53   /**
54    * 
55    * @param txn
56    * @param requestParameters
57    * @param aaiProvider
58    * @param aaiConfig
59    */
60   public PerformNodeSelfLinkProcessingTask(NodeProcessingTransaction txn, String requestParameters,
61       ActiveInventoryAdapter aaiAdapter) {
62     this.aaiAdapter = aaiAdapter;
63     this.txn = txn;
64     this.contextMap = MDC.getCopyOfContextMap();
65   }
66
67   /*
68    * (non-Javadoc)
69    * 
70    * @see java.util.function.Supplier#get()
71    */
72   @Override
73   public NodeProcessingTransaction get() {
74     MDC.setContextMap(contextMap);
75     OperationResult opResult = new OperationResult();
76     String link = txn.getSelfLink();
77
78     if (link == null) {
79       opResult.setResult(500, "Aborting self-link processing because self link is null");
80       txn.setOpResult(opResult);
81       return txn;
82     }
83
84     /**
85      * Rebuild the self link:
86      *  
87      * <li>build the base url with the configured scheme + authority (server:port)
88      * <li>recombine baseUrl + originalEncodedLink + queryStringParameters
89      * 
90      */
91
92     final String urlSchemeAndAuthority = aaiAdapter.repairSelfLink("");
93
94     String parameters = txn.getRequestParameters();
95     link = urlSchemeAndAuthority + link;
96     
97     if (parameters != null) {
98       link += parameters;
99     }
100
101
102
103     if (logger.isDebugEnabled()) {
104       logger.debug(AaiUiMsgs.DEBUG_GENERIC, "Collecting " + link);
105     }
106
107     try {
108       opResult = aaiAdapter.queryActiveInventoryWithRetries(link, "application/json",
109           aaiAdapter.getEndpointConfig().getNumRequestRetries());
110     } catch (Exception exc) {
111       opResult = new OperationResult();
112       opResult.setResult(500, "Querying AAI with retry failed due to an exception.");
113       logger.error(AaiUiMsgs.ERROR_AAI_QUERY_WITH_RETRY, exc.getMessage());
114     }
115
116     if (logger.isDebugEnabled()) {
117       logger.debug(AaiUiMsgs.DEBUG_GENERIC, "Operation result = " + opResult.toString());
118     }
119
120     txn.setOpResult(opResult);
121     return txn;
122
123   }
124
125 }