handle swagger part
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / vnfmdriver / controller / VnfmDriverController.java
index 2336c94..e55c867 100644 (file)
@@ -21,6 +21,7 @@ import java.io.IOException;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.http.HttpStatus;
+import org.apache.commons.io.IOUtils;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
@@ -30,6 +31,7 @@ import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.SwaggerInfo;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
@@ -56,6 +58,21 @@ public class VnfmDriverController {
        
        private Gson gson = new Gson();
        
+       @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
+       @ResponseBody
+       public SwaggerInfo apidoc() {
+        ClassLoader classLoader = getClass().getClassLoader();
+        SwaggerInfo info = null;
+               try {
+                       String json = IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
+                       info = gson.fromJson(json, SwaggerInfo.class);
+               } catch (IOException e) {
+                       logger.error("", e);
+               }
+        
+        return info;
+    }
+       
        @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
     @ResponseBody
     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
@@ -84,12 +101,7 @@ public class VnfmDriverController {
                }
                catch(VnfmDriverException e)
                {
-                       try {
-                               httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
-                               httpResponse.sendError(e.getHttpStatus(), e.getMessage());
-                       } catch (IOException e1) {
-                               
-                       }
+                       processControllerException(httpResponse, e);
                }
                
                return null;
@@ -109,12 +121,7 @@ public class VnfmDriverController {
                }
                catch(VnfmDriverException e)
                {
-                       try {
-                               httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
-                               httpResponse.sendError(e.getHttpStatus(), e.getMessage());
-                       } catch (IOException e1) {
-                               
-                       }
+                       processControllerException(httpResponse, e);
                }
                
                return null;
@@ -132,11 +139,7 @@ public class VnfmDriverController {
                }
                catch(VnfmDriverException e)
                {
-                       try {
-                               httpResponse.sendError(e.getHttpStatus(), e.getMessage());
-                       } catch (IOException e1) {
-                               
-                       }
+                       processControllerException(httpResponse, e);
                }
                
                return null;
@@ -157,11 +160,7 @@ public class VnfmDriverController {
                }
                catch(VnfmDriverException e)
                {
-                       try {
-                               httpResponse.sendError(e.getHttpStatus(), e.getMessage());
-                       } catch (IOException e1) {
-                               
-                       }
+                       processControllerException(httpResponse, e);
                }
                
                return null;
@@ -182,15 +181,21 @@ public class VnfmDriverController {
                }
                catch(VnfmDriverException e)
                {
-                       try {
-                               httpResponse.sendError(e.getHttpStatus(), e.getMessage());
-                       } catch (IOException e1) {
-                               
-                       }
+                       processControllerException(httpResponse, e);
                }
                
                return null;
     }
 
+       private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
+               try {
+                       logger.error(" VnfmDriverController --> processControllerException", e);
+                       httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
+                       httpResponse.sendError(e.getHttpStatus(), e.getMessage());
+               } catch (IOException e1) {
+                       logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);
+               }
+       }
+
 
 }