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