2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 AT&T Intellectual Property. 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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger;
 
  24 import java.util.Optional;
 
  25 import java.util.Properties;
 
  26 import javax.servlet.http.HttpServletRequest;
 
  27 import javax.validation.Valid;
 
  28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  30 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
 
  31 import org.onap.ccsdk.sli.core.sliapi.model.ExecuteGraphInput;
 
  32 import org.onap.ccsdk.sli.core.sliapi.model.ResponseFields;
 
  33 import org.onap.ccsdk.sli.core.sliapi.springboot.controllers.swagger.RestconfApi;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  36 import org.springframework.beans.factory.annotation.Autowired;
 
  37 import org.springframework.http.HttpStatus;
 
  38 import org.springframework.http.ResponseEntity;
 
  39 import org.springframework.stereotype.Controller;
 
  40 import com.fasterxml.jackson.core.JsonProcessingException;
 
  41 import com.fasterxml.jackson.databind.ObjectMapper;
 
  42 import com.google.gson.Gson;
 
  43 import com.google.gson.JsonObject;
 
  45 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
 
  48 public class RestconfApiController implements RestconfApi {
 
  50         private final ObjectMapper objectMapper;
 
  51         private final HttpServletRequest request;
 
  54   protected SvcLogicServiceBase svc;
 
  55         private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
 
  57         @org.springframework.beans.factory.annotation.Autowired
 
  58         public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
 
  59                 this.objectMapper = objectMapper;
 
  60                 this.request = request;
 
  64         public ResponseEntity<ResponseFields> healthcheck() {
 
  65                 ResponseFields resp = new ResponseFields();
 
  68                         log.info("Calling SLI-API:healthcheck DG");
 
  69                         SvcLogicContext ctxIn = new SvcLogicContext();
 
  70                         SvcLogicContext ctxOut = svc.execute("sli", "healthcheck", null, "sync", ctxIn);
 
  71                         Properties respProps = ctxOut.toProperties();
 
  73                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
  74                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
  75                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
 
  76                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
  78                         return (new ResponseEntity<>(resp, HttpStatus.OK));
 
  79                 } catch (Exception e) {
 
  80                         resp.setAckFinalIndicator("true");
 
  81                         resp.setResponseCode("500");
 
  82                         resp.setResponseMessage(e.getMessage());
 
  83                         log.error("Error calling healthcheck directed graph", e);
 
  86                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
  90         public ResponseEntity<ResponseFields> vlbcheck() {
 
  91                 ResponseFields resp = new ResponseFields();
 
  94                         log.info("Calling SLI-API:vlbcheck DG");
 
  95                         SvcLogicContext ctxIn = new SvcLogicContext();
 
  96                         SvcLogicContext ctxOut = svc.execute("sli", "vlbcheck", null, "sync", ctxIn);
 
  97                         Properties respProps = ctxOut.toProperties();
 
  98                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
  99                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
 100                         resp.setResponseMessage(respProps.getProperty("error-message", "Success"));
 
 101                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
 103                         return (new ResponseEntity<>(resp, HttpStatus.OK));
 
 104                 } catch (Exception e) {
 
 105                         resp.setAckFinalIndicator("true");
 
 106                         resp.setResponseCode("500");
 
 107                         resp.setResponseMessage(e.getMessage());
 
 108                         log.error("Error calling vlbcheck directed graph", e);
 
 111                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 116         public Optional<ObjectMapper> getObjectMapper() {
 
 117                 return Optional.ofNullable(objectMapper);
 
 121         public Optional<HttpServletRequest> getRequest() {
 
 122                 return Optional.ofNullable(request);
 
 126         public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
 
 127                 SvcLogicContext ctxIn = new SvcLogicContext();
 
 128                 ResponseFields resp = new ResponseFields();
 
 129                 String executeGraphInputJson = null;
 
 132                          executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
 
 133                          log.info("Input as JSON is "+executeGraphInputJson);
 
 134                 } catch (JsonProcessingException e) {
 
 136                         resp.setAckFinalIndicator("true");
 
 137                         resp.setResponseCode("500");
 
 138                         resp.setResponseMessage(e.getMessage());
 
 139                         log.error("Cannot create JSON from input object", e);
 
 140                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 143                 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
 
 144                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
 
 146                 ctxIn.mergeJson("input", passthroughObj.toString());
 
 149                         // Any of these can throw a nullpointer exception
 
 150                         String calledModule = executeGraphInput.getInput().getModuleName();
 
 151                         String calledRpc = executeGraphInput.getInput().getRpcName();
 
 152                         String modeStr = executeGraphInput.getInput().getMode();
 
 153                         // execute should only throw a SvcLogicException
 
 154                         SvcLogicContext ctxOut = svc.execute(calledModule, calledRpc, null, modeStr, ctxIn);
 
 155                         Properties respProps = ctxOut.toProperties();
 
 157                         resp.setAckFinalIndicator(respProps.getProperty("ack-final-indicator", "Y"));
 
 158                         resp.setResponseCode(respProps.getProperty("error-code", "200"));
 
 159                         resp.setResponseMessage(respProps.getProperty("error-message", "SUCCESS"));
 
 160                         resp.setContextMemoryJson(propsToJson(respProps, "context-memory"));
 
 161                         return (new ResponseEntity<>(resp, HttpStatus.valueOf(Integer.parseInt(resp.getResponseCode()))));
 
 163                 } catch (NullPointerException npe) {
 
 164                         resp.setAckFinalIndicator("true");
 
 165                         resp.setResponseCode("500");
 
 166                         resp.setResponseMessage("Check that you populated module, rpc and or mode correctly.");
 
 168                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 169                 } catch (SvcLogicException e) {
 
 170                         resp.setAckFinalIndicator("true");
 
 171                         resp.setResponseCode("500");
 
 172                         resp.setResponseMessage(e.getMessage());
 
 174                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
 
 178         public static String propsToJson(Properties props, String root)
 
 180                 StringBuffer sbuff = new StringBuffer();
 
 182                 sbuff.append("{ \""+root+"\" : { ");
 
 183                 boolean needComma = false;
 
 184                 for (Map.Entry<Object, Object> prop : props.entrySet()) {
 
 185                         sbuff.append("\""+(String) prop.getKey()+"\" : \""+(String)prop.getValue()+"\"");
 
 192                 sbuff.append(" } }");
 
 194                 return(sbuff.toString());