Adding interfaces in documentation
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / viewandinspect / task / PerformGizmoNodeSelfLinkProcessingTask.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.GizmoAdapter;
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 PerformGizmoNodeSelfLinkProcessingTask implements Supplier<NodeProcessingTransaction> {
38
39   private static final Logger logger =
40       LoggerFactory.getInstance().getLogger(PerformGizmoNodeSelfLinkProcessingTask.class);
41
42   private NodeProcessingTransaction txn;
43   private GizmoAdapter gizmoAdapter;
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 PerformGizmoNodeSelfLinkProcessingTask(NodeProcessingTransaction txn, String requestParameters,
61       GizmoAdapter gizmoAdapter) {
62     this.gizmoAdapter = gizmoAdapter;
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 = gizmoAdapter.repairInventorySelfLink("", null);
93
94     String parameters = txn.getRequestParameters();
95     link = urlSchemeAndAuthority + link;
96     
97     if (parameters != null) {
98       link += parameters;
99     }
100
101     if (logger.isDebugEnabled()) {
102       logger.debug(AaiUiMsgs.DEBUG_GENERIC, "Collecting " + link);
103     }
104
105     try {
106         
107       opResult = gizmoAdapter.queryGizmoWithRetries(link, "application/json",
108           gizmoAdapter.getEndpointConfig().getNumRequestRetries());
109     } catch (Exception exc) {
110       opResult = new OperationResult();
111       opResult.setResult(500, "Querying AAI with retry failed due to an exception.");
112       logger.error(AaiUiMsgs.ERROR_AAI_QUERY_WITH_RETRY, exc.getMessage());
113     }
114
115     if (logger.isDebugEnabled()) {
116       logger.debug(AaiUiMsgs.DEBUG_GENERIC, "Operation result = " + opResult.toString());
117     }
118
119     txn.setOpResult(opResult);
120     return txn;
121
122   }
123
124 }