2972cd791a97b3708873e94bc4b816db05c3184e
[ccsdk/sli/core.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
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
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.core.sliapi.springboot.controllers.swagger;
22
23 import java.util.Map;
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;
44
45 @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-02-20T12:50:11.207-05:00")
46
47 @Controller
48 public class RestconfApiController implements RestconfApi {
49
50         private final ObjectMapper objectMapper;
51         private final HttpServletRequest request;
52
53   @Autowired
54   protected SvcLogicServiceBase svc;
55         private static final Logger log = LoggerFactory.getLogger(RestconfApiController.class);
56
57         @org.springframework.beans.factory.annotation.Autowired
58         public RestconfApiController(ObjectMapper objectMapper, HttpServletRequest request) {
59                 this.objectMapper = objectMapper;
60                 this.request = request;
61         }
62
63         @Override
64         public ResponseEntity<ResponseFields> healthcheck() {
65                 ResponseFields resp = new ResponseFields();
66
67                 try {
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();
72
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"));
77
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);
84
85                 }
86                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
87         }
88
89         @Override
90         public ResponseEntity<ResponseFields> vlbcheck() {
91                 ResponseFields resp = new ResponseFields();
92
93                 try {
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"));
102
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);
109
110                 }
111                 return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
112         }
113
114
115         @Override
116         public Optional<ObjectMapper> getObjectMapper() {
117                 return Optional.ofNullable(objectMapper);
118         }
119
120         @Override
121         public Optional<HttpServletRequest> getRequest() {
122                 return Optional.ofNullable(request);
123         }
124
125         @Override
126         public ResponseEntity<ResponseFields> executeGraph(@Valid ExecuteGraphInput executeGraphInput) {
127                 SvcLogicContext ctxIn = new SvcLogicContext();
128                 ResponseFields resp = new ResponseFields();
129                 String executeGraphInputJson = null;
130
131                 try {
132                          executeGraphInputJson = objectMapper.writeValueAsString(executeGraphInput);
133                          log.info("Input as JSON is "+executeGraphInputJson);
134                 } catch (JsonProcessingException e) {
135
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));
141
142                 }
143                 JsonObject jsonInput = new Gson().fromJson(executeGraphInputJson, JsonObject.class);
144                 JsonObject passthroughObj = jsonInput.get("input").getAsJsonObject();
145
146                 ctxIn.mergeJson("input", passthroughObj.toString());
147
148                 try {
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();
156
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()))));
162
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.");
167
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());
173
174                         return (new ResponseEntity<>(resp, HttpStatus.INTERNAL_SERVER_ERROR));
175                 }
176         }
177
178         public static String propsToJson(Properties props, String root)
179         {
180                 StringBuffer sbuff = new StringBuffer();
181
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()+"\"");
186                         if (needComma) {
187                                 sbuff.append(" , ");
188                         } else {
189                                 needComma = true;
190                         }
191                 }
192                 sbuff.append(" } }");
193
194                 return(sbuff.toString());
195         }
196
197 }