ac0897e8f3377d54711e2ad44430e71dc15b4239
[ccsdk/sli/plugins.git] / restconf-client / provider / src / main / java / org / onap / ccsdk / sli / plugins / restconfapicall / RestconfApiCallNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.restconfapicall;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.stream.JsonWriter;
28
29 import javax.ws.rs.core.UriBuilder;
30 import java.io.StringWriter;
31 import java.io.Writer;
32 import java.net.SocketException;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Map;
39
40 import org.dom4j.Document;
41 import org.dom4j.DocumentException;
42 import org.dom4j.DocumentHelper;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
46 import org.onap.ccsdk.sli.plugins.restapicall.Format;
47 import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse;
48 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
49 import org.onap.ccsdk.sli.plugins.restapicall.RetryException;
50 import org.onap.ccsdk.sli.plugins.restapicall.RetryPolicy;
51 import org.onap.ccsdk.sli.plugins.restapicall.XmlParser;
52 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializer;
53 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializerContext;
54 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerFactory;
55 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.Listener;
56 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.MdsalSerializerHelper;
57 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.SerializerHelper;
58 import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters;
59 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeSerializer;
60 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace;
61 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeSerializer;
62 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
63 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
65 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
66 import org.osgi.framework.BundleContext;
67 import org.osgi.framework.ServiceReference;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70
71 import static com.google.common.base.Strings.repeat;
72 import static java.lang.String.format;
73 import static java.lang.String.valueOf;
74 import static org.apache.commons.lang3.StringUtils.join;
75 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.DELETE;
76 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET;
77 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH;
78 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT;
79 import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam;
80 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.ATTEMPTS_MSG;
81 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COLON;
82 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMMA;
83 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMM_FAIL;
84 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.HEADER;
85 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.HTTP_REQ;
86 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.HTTP_RES;
87 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.MAX_RETRY_ERR;
88 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.NO_MORE_RETRY;
89 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.REQ_ERR;
90 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.REST_API_URL;
91 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_CODE;
92 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_MSG;
93 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_PRE;
94 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_COUNT;
95 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_FAIL;
96 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.UPDATED_URL;
97 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getSchemaCtxFromDir;
98 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getUpdatedXmlReq;
99 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getYangParameters;
100 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl;
101 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfListenerFactory.instance;
102 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.FORMAT_ERR;
103 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER;
104 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_TREE_ERR;
105 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter;
106 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getModuleNamespace;
107 import static org.osgi.framework.FrameworkUtil.getBundle;
108
109 /**
110  * Representation of a plugin to enable RESTCONF based CRUD operations from DG.
111  */
112 public class RestconfApiCallNode implements SvcLogicJavaPlugin {
113
114     /**
115      * Logger for the restconf api call node class.
116      */
117     private static final Logger log = LoggerFactory.getLogger(
118             RestconfApiCallNode.class);
119
120     /**
121      * Rest api call node service instance
122      */
123     private RestapiCallNode restapiCallNode;
124
125     /**
126      * Creates an instance of restconf api call node with restapi call node.
127      *
128      * @param r restapi call node
129      */
130     public RestconfApiCallNode(RestapiCallNode r) {
131         this.restapiCallNode = r;
132     }
133
134     /**
135      * Returns the restapi call node instance.
136      * @return
137      */
138     public RestapiCallNode getRestapiCallNode() {
139         return restapiCallNode;
140     }
141
142     /**
143      * Sends the restconf request using the parameters map and the memory
144      * context. And this method allows the directed graphs to interact with
145      * the restconf api call node
146      *
147      * @param paramMap parameters map
148      * @param ctx      service logic context
149      * @throws SvcLogicException when svc logic exception occurs
150      */
151     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx)
152             throws SvcLogicException {
153         sendRequest(paramMap, ctx, 0);
154     }
155
156     /**
157      * Sends the restconf request using the parameters map and the memory
158      * context along with the retry count.
159      *
160      * @param paramMap   parameters map
161      * @param ctx        service logic context
162      * @param retryCount number of retry counts
163      * @throws SvcLogicException when svc logic exception occurs
164      */
165     public void sendRequest(Map<String, String> paramMap, SvcLogicContext ctx,
166                             Integer retryCount) throws SvcLogicException {
167         RestapiCallNode rest = getRestapiCallNode();
168         RetryPolicy retryPolicy = null;
169         HttpResponse r = new HttpResponse();
170         try {
171             YangParameters p = getYangParameters(paramMap);
172             if (p.partner != null) {
173                 retryPolicy = rest.getRetryPolicyStore()
174                         .getRetryPolicy(p.partner);
175             }
176
177             String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
178             Map<String, String> props = new HashMap<>((Map)ctx.toProperties());
179             String uri = parseUrl(p.restapiUrl, p.httpMethod);
180             InstanceIdentifierContext<?> insIdCtx = getInsIdCtx(p, uri);
181
182             String req = null;
183             if (p.httpMethod != GET && p.httpMethod != DELETE) {
184                 req = serializeRequest(props, p, uri, insIdCtx);
185                 if (p.httpMethod == PUT || p.httpMethod == PATCH) {
186                     updateReq(req, p, insIdCtx);
187                 }
188             }
189             if (req == null && p.requestBody != null) {
190                 req = p.requestBody;
191             }
192
193             r = rest.sendHttpRequest(req, p);
194             if (p.returnRequestPayload && req != null) {
195                 ctx.setAttribute(pp + HTTP_REQ, req);
196             }
197
198             String response = getResponse(ctx, p, pp, r);
199             if (response != null) {
200                 try {
201                     Map<String, String> resProp = serializeResponse(
202                             p, uri, response, insIdCtx);
203                     for (Map.Entry<String, String> pro : resProp.entrySet()) {
204                         ctx.setAttribute(pro.getKey(), pro.getValue());
205                     }
206                 } catch (SvcLogicException e) {
207                     convertToNormalRes(ctx, p, pp, response);
208                 }
209             }
210         } catch (SvcLogicException e) {
211             boolean shouldRetry = false;
212             if (e.getCause().getCause() instanceof SocketException) {
213                 shouldRetry = true;
214             }
215
216             log.error(REQ_ERR + e.getMessage(), e);
217             String prefix = parseParam(paramMap, RES_PRE, false, null);
218             if (retryPolicy == null || !shouldRetry) {
219                 setFailureResponseStatus(ctx, prefix, e.getMessage());
220             } else {
221                 if (retryCount == null) {
222                     retryCount = 0;
223                 }
224                 log.debug(format(ATTEMPTS_MSG, retryCount,
225                                  retryPolicy.getMaximumRetries()));
226                 try {
227                     retryCount = retryCount + 1;
228                     if (retryCount < retryPolicy.getMaximumRetries() + 1) {
229                         setRetryUri(paramMap, retryPolicy);
230                         log.debug(format(RETRY_COUNT, retryCount, retryPolicy
231                                 .getMaximumRetries()));
232                         sendRequest(paramMap, ctx, retryCount);
233                     } else {
234                         log.debug(MAX_RETRY_ERR);
235                         setFailureResponseStatus(ctx, prefix, e.getMessage());
236                     }
237                 } catch (Exception ex) {
238                     log.error(NO_MORE_RETRY, ex);
239                     setFailureResponseStatus(ctx, prefix, RETRY_FAIL);
240                 }
241             }
242         }
243
244         if (r != null && r.code >= 300) {
245             throw new SvcLogicException(valueOf(r.code) +
246                                                 COLON + " " + r.message);
247         }
248     }
249
250     private void convertToNormalRes(SvcLogicContext ctx ,
251                                     YangParameters p, String pp, String body)
252             throws SvcLogicException {
253         if (p.convertResponse) {
254             Map<String, String> mm = null;
255             if (p.format == Format.XML) {
256                 mm = XmlParser.convertToProperties(body, p.listNameList);
257             } else if (p.format == Format.JSON) {
258                 mm = org.onap.ccsdk.sli.plugins.restapicall.JsonParser
259                         .convertToProperties(body);
260             }
261
262             if (mm != null) {
263                 for (Map.Entry<String, String> entry : mm.entrySet()) {
264                     ctx.setAttribute(pp + entry.getKey(),
265                                      entry.getValue());
266                 }
267             }
268         }
269     }
270
271     /**
272      * Serializes the request message to JSON or XML from the properties.
273      *
274      * @param properties properties
275      * @param params     YANG parameters
276      * @param uri        URI
277      * @param insIdCtx   instance identifier context
278      * @return JSON or XML message to be sent
279      * @throws SvcLogicException when serializing the request fails
280      */
281      public String serializeRequest(Map<String, String> properties,
282                                     YangParameters params, String uri,
283                                     InstanceIdentifierContext insIdCtx)
284              throws SvcLogicException {
285         PropertiesNodeSerializer propSer = new MdsalPropertiesNodeSerializer(
286                 insIdCtx.getSchemaNode(), insIdCtx.getSchemaContext(), uri);
287         DataFormatSerializerContext serCtx = new DataFormatSerializerContext(
288                 null, uri, null, propSer);
289         DataFormatSerializer ser = DfSerializerFactory.instance()
290                 .getSerializer(serCtx, params);
291          //TODO: Handling of XML annotations
292         return ser.encode(properties, null);
293     }
294
295     /**
296      * Serializes the response message from JSON or XML to the properties.
297      *
298      * @param params   YANG parameters
299      * @param uri      URI
300      * @param response response message
301      * @param insIdCtx instance identifier context
302      * @return response message as properties
303      * @throws SvcLogicException when serializing the response fails
304      */
305     public Map<String, String> serializeResponse(YangParameters params,
306                                                  String uri, String response,
307                                                  InstanceIdentifierContext insIdCtx)
308             throws SvcLogicException {
309         PropertiesNodeSerializer propSer = new MdsalPropertiesNodeSerializer(
310                 insIdCtx.getSchemaNode(), insIdCtx.getSchemaContext(), uri);
311         SerializerHelper helper = new MdsalSerializerHelper(
312                 insIdCtx.getSchemaNode(), insIdCtx.getSchemaContext(), uri);
313         Listener listener = instance().getListener(helper, params);
314         DataFormatSerializerContext serCtx = new DataFormatSerializerContext(
315                 listener, uri, null, propSer);
316         DataFormatSerializer ser = DfSerializerFactory.instance()
317                 .getSerializer(serCtx, params);
318         return ser.decode(response);
319     }
320
321     /**
322      * Returns instance identifier context for a uri using the schema context.
323      *
324      * @param params YANG parameters
325      * @param uri    URI
326      * @return instance identifier context
327      * @throws SvcLogicException when getting schema context fails
328      */
329     private InstanceIdentifierContext<?> getInsIdCtx(YangParameters params,
330                                                      String uri)
331             throws SvcLogicException {
332         SchemaContext context = getSchemaContext(params);
333         return ParserIdentifier.toInstanceIdentifier(uri, context, null);
334     }
335
336     /**
337      * Returns the global schema context or schema context of particular YANG
338      * files present in a directory path.
339      *
340      * @param params YANG parameters
341      * @return schema context
342      * @throws SvcLogicException when schema context fetching fails
343      */
344     private SchemaContext getSchemaContext(YangParameters params)
345             throws SvcLogicException {
346         if (params.dirPath != null) {
347             return getSchemaCtxFromDir(params.dirPath);
348         }
349         BundleContext bc = getBundle(SchemaContext.class).getBundleContext();
350         SchemaContext schemaContext = null;
351         if (bc != null) {
352             ServiceReference reference = bc.getServiceReference(
353                     SchemaContext.class);
354             if (reference != null) {
355                 schemaContext = (SchemaContext) bc.getService(reference);
356             }
357         }
358         return schemaContext;
359     }
360
361     /**
362      * Returns the response message body of a http response message.
363      *
364      * @param ctx    svc logic context
365      * @param params parameters
366      * @param pre    prefix to be appended
367      * @param res    http response
368      * @return response message body
369      */
370     public String getResponse(SvcLogicContext ctx, YangParameters params,
371                                String pre, HttpResponse res) {
372         ctx.setAttribute(pre + RES_CODE, valueOf(res.code));
373         ctx.setAttribute(pre + RES_MSG, res.message);
374
375         if (params.dumpHeaders && res.headers != null) {
376             for (Map.Entry<String, List<String>> a : res.headers.entrySet()) {
377                 ctx.setAttribute(pre + HEADER + a.getKey(),
378                                  join(a.getValue(), COMMA));
379             }
380         }
381
382         if (res.body != null && res.body.trim().length() > 0) {
383             ctx.setAttribute(pre + HTTP_RES, res.body);
384             return res.body;
385         }
386         return null;
387     }
388
389     /**
390      * Sets the failure response status in the context memory.
391      *
392      * @param ctx    service logic context
393      * @param prefix prefix to be added
394      * @param errMsg error message
395      */
396     private void setFailureResponseStatus(SvcLogicContext ctx, String prefix,
397                                           String errMsg) {
398         HttpResponse res = new HttpResponse();
399         res.code = 500;
400         res.message = errMsg;
401         ctx.setAttribute(prefix + RES_CODE, valueOf(res.code));
402         ctx.setAttribute(prefix + RES_MSG, res.message);
403     }
404
405     /**
406      * Sets the retry URI to the param map from the retry policies different
407      * host.
408      *
409      * @param paramMap            parameter map
410      * @param retryPolicy         retry policy
411      * @throws URISyntaxException when new URI creation fails
412      * @throws RetryException     when retry policy cannot give another host
413      */
414     private void setRetryUri(Map<String, String> paramMap,
415                              RetryPolicy retryPolicy)
416             throws URISyntaxException, RetryException {
417         URI uri = new URI(paramMap.get(REST_API_URL));
418         String hostName = uri.getHost();
419         String retryString = retryPolicy.getNextHostName(uri.toString());
420
421         URI uriTwo = new URI(retryString);
422         URI retryUri = UriBuilder.fromUri(uri).host(uriTwo.getHost()).port(
423                 uriTwo.getPort()).scheme(uriTwo.getScheme()).build();
424
425         paramMap.put(REST_API_URL, retryUri.toString());
426         log.debug(UPDATED_URL + retryUri.toString());
427         log.debug(format(COMM_FAIL, hostName, retryString));
428     }
429
430     /**
431      * Updates request message for JSON and XML data format, when the HTTP
432      * method points it as PUT or PATCH.
433      *
434      * @param req      current request message
435      * @param p        YANG parameters
436      * @param insIdCtx instance identifier context
437      * @return update request message
438      * @throws SvcLogicException when the data format type is wrong
439      */
440     public String updateReq(String req, YangParameters p,
441                              InstanceIdentifierContext<?> insIdCtx)
442             throws SvcLogicException {
443
444         SchemaNode schemaNode = insIdCtx.getSchemaNode();
445         Namespace modNs = getModuleNamespace(schemaNode.getQName(),
446                                              insIdCtx.getSchemaContext());
447         String nodeName = schemaNode.getQName().getLocalName();
448
449         switch (p.format) {
450             case JSON:
451                 return getUpdatedJsonReq(req, nodeName, modNs.moduleName());
452
453             case XML:
454                 return getXmlReqForPutOp(req, nodeName, modNs.moduleNs());
455
456             default:
457                 throw new SvcLogicException(format(FORMAT_ERR, p.format));
458         }
459     }
460
461     /**
462      * Returns the updated JSON request message, when the HTTP method
463      * points to PUT or PATCH.
464      *
465      * @param req      current JSON request message
466      * @param nodeName root node name
467      * @param modName  module name of the root node
468      * @return update JSON request message
469      */
470     private String getUpdatedJsonReq(String req, String nodeName,
471                                      String modName) {
472         Writer writer = new StringWriter();
473         JsonWriter jsonWriter = new JsonWriter(writer);
474         jsonWriter.setIndent(repeat(" ", 4));
475
476         JsonParser jsonParser = new JsonParser();
477         JsonObject oldJson = (JsonObject)jsonParser.parse(req);
478         oldJson = remChildModName(oldJson, modName);
479         JsonObject newJson = new JsonObject();
480         newJson.add(modName + COLON + nodeName, oldJson.deepCopy());
481
482         Gson gson= new Gson();
483         gson.toJson(newJson, jsonWriter);
484         return writer.toString();
485     }
486
487     /**
488      * Removes module name from all the updated first level child node, if it
489      * is same as the root node added.
490      *
491      * @param oldJson JSON object for old request
492      * @param modName module name of root node
493      * @return JSON object for old request with updated child module name
494      */
495     private JsonObject remChildModName(JsonObject oldJson, String modName) {
496         Iterator<Map.Entry<String, JsonElement>> it = oldJson.entrySet().iterator();
497         Map<String, JsonElement> m = new HashMap<>();
498         while (it.hasNext()) {
499             Map.Entry<String, JsonElement> jNode = it.next();
500             if (jNode.getKey().contains(COLON)) {
501                 String[] modArr = jNode.getKey().split(COLON);
502                 if (modArr[0].equals(modName)) {
503                     it.remove();
504                     m.put(modArr[1], jNode.getValue());
505                 }
506             }
507         }
508         if (!m.isEmpty()) {
509             for (Map.Entry<String, JsonElement> element : m.entrySet()) {
510                 oldJson.add(element.getKey(), element.getValue());
511             }
512         }
513         return oldJson;
514     }
515
516     /**
517      * Returns the updated XML request message, when the HTTP method points
518      * to PUT or PATCH.
519      *
520      * @param req      current JSON request message
521      * @param nodeName root node name
522      * @param modNs    module namespace of the root node
523      * @return update JSON request message
524      * @throws SvcLogicException when XML parsing fails
525      */
526     private String getXmlReqForPutOp(String req, String nodeName,
527                                      URI modNs) throws SvcLogicException {
528         req = getUpdatedXmlReq(req, nodeName, modNs.toString());
529         Document oldDoc;
530         try {
531             oldDoc = DocumentHelper.parseText(req);
532         } catch (DocumentException e) {
533             throw new SvcLogicException(XML_TREE_ERR, e);
534         }
535         Writer writer = getXmlWriter(
536                 UTF_HEADER + oldDoc.getRootElement().asXML(), "4");
537         return writer.toString();
538     }
539 }