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