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