From 56b683e6f6496b1c302da7edb7dd29d791e775c2 Mon Sep 17 00:00:00 2001 From: Arthur Martella Date: Fri, 15 Mar 2019 12:30:10 -0400 Subject: Initial upload of F-GPS seed code 13/21 Includes: API base code API controller code Change-Id: Icf2d9e80c2f360fdeef08287f620565c82546ae1 Issue-ID: OPTFRA-440 Signed-off-by: arthur.martella.1@att.com --- .../java/org/onap/fgps/api/ApplicationStartup.java | 92 +++++++ .../org/onap/fgps/api/ValetServiceApplication.java | 49 ++++ .../java/org/onap/fgps/api/WebConfiguration.java | 66 +++++ .../onap/fgps/api/controller/AdminController.java | 66 +++++ .../fgps/api/controller/ValetGroupsController.java | 144 ++++++++++ .../ValetServicePlacementController.java | 150 +++++++++++ .../api/controller/ValetUtilityController.java | 298 +++++++++++++++++++++ 7 files changed, 865 insertions(+) create mode 100644 valetapi/src/main/java/org/onap/fgps/api/ApplicationStartup.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/ValetServiceApplication.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/WebConfiguration.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/controller/AdminController.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/controller/ValetGroupsController.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/controller/ValetServicePlacementController.java create mode 100644 valetapi/src/main/java/org/onap/fgps/api/controller/ValetUtilityController.java diff --git a/valetapi/src/main/java/org/onap/fgps/api/ApplicationStartup.java b/valetapi/src/main/java/org/onap/fgps/api/ApplicationStartup.java new file mode 100644 index 0000000..a0f9a95 --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/ApplicationStartup.java @@ -0,0 +1,92 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api; + +import java.io.InputStream; +import java.util.Properties; + +import org.onap.fgps.api.dao.SchemaDAO; +import org.onap.fgps.api.logging.EELFLoggerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; +import org.onap.fgps.api.utils.UserUtils; + +@Component +public class ApplicationStartup implements ApplicationListener { + + private SchemaDAO schemaDAO; + //private static final Logger LOGGER = LoggerFactory.getLogger(ValetServiceApplication.class); + private EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ApplicationStartup.class); + + + @Autowired + public ApplicationStartup(SchemaDAO schemaDAO) { + super(); + this.schemaDAO = schemaDAO; + } + + /** + * This event is executed as late as conceivably possible to indicate that + * the application is ready to service requests. + */ + @Override + public void onApplicationEvent(final ApplicationReadyEvent event) { + Properties props = new Properties(); + String propFileName = "resources.properties"; + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); + try { + if (inputStream != null) { + props.load(inputStream); + } else { + LOGGER.info(EELFLoggerDelegate.applicationLogger,"DBProxy : inputstream is not"); + } + String dbCreate = UserUtils.htmlEscape(props.getProperty("db.create")); + System.out.println( dbCreate ); + if( dbCreate!=null && dbCreate.equals( "true")) { + schemaDAO.initializeDatabase(); + } + } catch (Exception e) { + e.printStackTrace(); + LOGGER.error(EELFLoggerDelegate.applicationLogger,"onApplicationEvent : Error details : "+ e.getMessage()); + LOGGER.error(EELFLoggerDelegate.errorLogger,"onApplicationEvent : Error details : "+ e.getMessage()); + } + return; + } +} diff --git a/valetapi/src/main/java/org/onap/fgps/api/ValetServiceApplication.java b/valetapi/src/main/java/org/onap/fgps/api/ValetServiceApplication.java new file mode 100644 index 0000000..0ec614d --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/ValetServiceApplication.java @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ValetServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(ValetServiceApplication.class, args); + } +} diff --git a/valetapi/src/main/java/org/onap/fgps/api/WebConfiguration.java b/valetapi/src/main/java/org/onap/fgps/api/WebConfiguration.java new file mode 100644 index 0000000..cdd103e --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/WebConfiguration.java @@ -0,0 +1,66 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class WebConfiguration { + + @Bean + public WebMvcConfigurerAdapter forwardToIndex() { + return new WebMvcConfigurerAdapter() { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + /* + registry.addViewController("/swagger").setViewName( + "redirect:/swagger/index.html"); + registry.addViewController("/swagger/").setViewName( + "redirect:/swagger/index.html"); + registry.addViewController("/docs").setViewName( + "redirect:/docs/html/index.html"); + registry.addViewController("/docs/").setViewName( + "redirect:/docs/html/index.html"); + */ + } + }; + } +} \ No newline at end of file diff --git a/valetapi/src/main/java/org/onap/fgps/api/controller/AdminController.java b/valetapi/src/main/java/org/onap/fgps/api/controller/AdminController.java new file mode 100644 index 0000000..0f49ae4 --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/controller/AdminController.java @@ -0,0 +1,66 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api.controller; + +import org.onap.fgps.api.dao.SchemaDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@CrossOrigin(origins = "*") +@RestController +@EnableAutoConfiguration +@RequestMapping("/admin/") +public class AdminController { + private SchemaDAO schemaDAO; + + @Autowired + public AdminController(SchemaDAO schemaDAO) { + super(); + this.schemaDAO = schemaDAO; + } + + @RequestMapping(value = "create", method = RequestMethod.POST) + public String getVmDetails() { + // SchemaDAO dao = new SchemaDAO(); + return schemaDAO.initializeDatabase(); + } +} diff --git a/valetapi/src/main/java/org/onap/fgps/api/controller/ValetGroupsController.java b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetGroupsController.java new file mode 100644 index 0000000..fc8d402 --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetGroupsController.java @@ -0,0 +1,144 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api.controller; + +import javax.servlet.http.HttpServletRequest; + +import org.json.simple.JSONObject; +import org.onap.fgps.api.annotation.PropertyBasedAuthorization; +import org.onap.fgps.api.logging.EELFLoggerDelegate; +import org.onap.fgps.api.logging.aspect.AuditLog; +import org.onap.fgps.api.logging.aspect.MetricsLog; +import org.onap.fgps.api.service.ValetGroupsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.onap.fgps.api.utils.UserUtils; + +@CrossOrigin(origins = "*") +@RestController +@EnableAutoConfiguration +@RequestMapping("/groups/v1/") +@EnableAspectJAutoProxy +@AuditLog +@MetricsLog +public class ValetGroupsController { + private ValetGroupsService valetGroupsService; + private EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ValetGroupsController.class); + @Autowired + public ValetGroupsController(ValetGroupsService valetGroupsService) { + super(); + this.valetGroupsService = valetGroupsService; + } + + + @PropertyBasedAuthorization("groups.query") + @SuppressWarnings("unchecked") + @RequestMapping(consumes = "application/json", method = RequestMethod.GET) + public ResponseEntity queryGroups(@RequestParam("requestId") String requestId, + @RequestParam(value = "name", required = false) String name, + @RequestParam(value = "datacenter_id", required = false) String datacenterId, + @RequestParam(value = "host", required = false) String host) { + requestId = UserUtils.htmlEscape(requestId); + name = UserUtils.htmlEscape(name); + datacenterId = UserUtils.htmlEscape(datacenterId); + host = UserUtils.htmlEscape(host); + + LOGGER.info(EELFLoggerDelegate.applicationLogger,"queryGroups controller - ", requestId); + JSONObject requestJson = new JSONObject(); + if(name !=null && datacenterId !=null) { + requestJson.put("name", name); + requestJson.put("datacenter_id", datacenterId); + } + LOGGER.debug(EELFLoggerDelegate.debugLogger,"queryGroups: Initiating request to query groups for requestJson: {}, requestId: {}", requestJson, requestId); + return valetGroupsService.saveGroupsRequest(requestJson, "group_query", requestId); + } + @PropertyBasedAuthorization("groups.create") + @RequestMapping(consumes = "application/json", method = RequestMethod.POST) + public ResponseEntity getCreateDetails(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.info(EELFLoggerDelegate.applicationLogger,"getCreateDetails controller - ", requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"getCreateDetails: Initiating request to get create groups details for requestJson: {}, requestId: {}", request, requestId); + return valetGroupsService.saveGroupsRequest(request, "group_create", requestId); + } + @PropertyBasedAuthorization("groups.update") + @RequestMapping(consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity getUpdateDetails(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.info(EELFLoggerDelegate.applicationLogger,"getUpdateDetails controller - ", requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"getCreateDetails: Initiating request to get update groups details for requestJson: {}, requestId: {}", request, requestId); + return valetGroupsService.saveGroupsRequest(request, "group_update", requestId); + } + @PropertyBasedAuthorization("groups.delete") + @RequestMapping(consumes = "application/json", method = RequestMethod.DELETE) + public ResponseEntity getDeleteDetails(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.info(EELFLoggerDelegate.applicationLogger,"getDeleteDetails controller - ", requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"getDeleteDetails: Initiating request to get delete groups details for requestJson: {}, requestId: {}", request, requestId); + return valetGroupsService.saveGroupsRequest(request, "group_delete", requestId); + } + + //j unit test cases controller + @SuppressWarnings("unchecked") + @RequestMapping(value = "portal", method = RequestMethod.GET) + public ResponseEntity queryGroups1(@RequestParam("requestId") String requestId, + @RequestParam(value = "name", required = false) String name, + @RequestParam(value = "datacenter_id", required = false) String datacenterId, + @RequestParam(value = "host", required = false) String host) { + requestId = UserUtils.htmlEscape(requestId); + name = UserUtils.htmlEscape(name); + datacenterId = UserUtils.htmlEscape(datacenterId); + host = UserUtils.htmlEscape(host); + + LOGGER.info(EELFLoggerDelegate.applicationLogger,"queryGroups controller - ", requestId); + JSONObject requestJson = new JSONObject(); + if(name !=null && datacenterId !=null) { + requestJson.put("name", name); + requestJson.put("datacenter_id", datacenterId); + } + + return valetGroupsService.saveGroupsRequest1(requestJson, "group_query", requestId); + } +} diff --git a/valetapi/src/main/java/org/onap/fgps/api/controller/ValetServicePlacementController.java b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetServicePlacementController.java new file mode 100644 index 0000000..465fdce --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetServicePlacementController.java @@ -0,0 +1,150 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api.controller; + +import javax.servlet.http.HttpServletRequest; + +import org.json.simple.JSONObject; +import org.onap.fgps.api.annotation.PropertyBasedAuthorization; +import org.onap.fgps.api.helpers.Helper; +import org.onap.fgps.api.logging.EELFLoggerDelegate; +import org.onap.fgps.api.logging.aspect.AuditLog; +import org.onap.fgps.api.logging.aspect.MetricsLog; +import org.onap.fgps.api.service.ValetPlacementService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.onap.fgps.api.utils.UserUtils; + +@CrossOrigin(origins = "*") +@RestController +@EnableAutoConfiguration +@RequestMapping("/placement/v1/") +@EnableAspectJAutoProxy +@AuditLog +@MetricsLog +public class ValetServicePlacementController { + + private ValetPlacementService valetPlacementService; + private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ValetServicePlacementController.class); + + @Autowired + public ValetServicePlacementController(ValetPlacementService valetPlacementService) { + super(); + this.valetPlacementService = valetPlacementService; + } + + @PropertyBasedAuthorization("placement.create") + @RequestMapping(consumes = "application/json", method = RequestMethod.POST) + public ResponseEntity createVm(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"createVm: Initiating request to create VM for request: {}, requestId: {}", request, requestId); + return valetPlacementService.processMSORequest1(request, requestId, "create"); + } + + @PropertyBasedAuthorization("placement.update") + @RequestMapping( consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity updateVm(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"updateVm: Initiating request to update VM for request: {}, requestId: {}", request, requestId); + return valetPlacementService.processMSORequest1(request,requestId,"update"); + } + @PropertyBasedAuthorization("placement.delete") + @RequestMapping(consumes = "application/json", method = RequestMethod.DELETE) + public ResponseEntity deleteVm(HttpServletRequest httpRequest, @RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"deleteVm: Initiating request to delete VM for request: {}, requestId: {}", request, requestId); + return valetPlacementService.saveRequest(Helper.formatDeleteRequest(request), "delete", requestId); + } + @PropertyBasedAuthorization("placement.confirm") + @RequestMapping(value = "/{priorRequestId}/confirm", consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity confirm(HttpServletRequest httpRequest, @PathVariable("priorRequestId") String priorRequestId, @RequestBody JSONObject request){ + priorRequestId = UserUtils.htmlEscape(priorRequestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"confirm: Initiating request to confirm VM for request: {}, priorRequestId: {}", request, priorRequestId); + return valetPlacementService.saveRequest(request, "confirm", priorRequestId); + } + @PropertyBasedAuthorization("placement.rollback") + @RequestMapping(value = "/{priorRequestId}/rollback", consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity rollback(HttpServletRequest httpRequest, @PathVariable("priorRequestId") String priorRequestId, @RequestBody JSONObject request) { + priorRequestId = UserUtils.htmlEscape(priorRequestId); + LOGGER.debug(EELFLoggerDelegate.debugLogger,"rollback: Initiating request to rollback VM for request: {}, priorRequestId: {}", request, priorRequestId); + return valetPlacementService.saveRequest(request, "rollback", priorRequestId); + } + + //Unit test cases mocked controllers + @RequestMapping(value = "/createVM2", consumes = "application/json", method = RequestMethod.POST) + public ResponseEntity createVm2(@RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + ResponseEntity response = valetPlacementService.processMSORequest2(request, requestId); + return valetPlacementService.processMSORequest2(request, requestId); + } + + @RequestMapping(value = "/updateVm1", consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity updateVm1(@RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + return valetPlacementService.processMSORequest2(request,requestId); + } + + @RequestMapping(value = "/deleteVm1",consumes = "application/json", method = RequestMethod.DELETE) + public ResponseEntity deleteVm1(@RequestBody JSONObject request, @RequestParam("requestId") String requestId) { + requestId = UserUtils.htmlEscape(requestId); + return valetPlacementService.saveRequesttest(Helper.formatDeleteRequest(request), "delete", requestId); + } + + @RequestMapping(value = "/{priorRequestId}/confirm1", consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity confirm1(@PathVariable("priorRequestId") String priorRequestId, @RequestBody JSONObject request) { + priorRequestId = UserUtils.htmlEscape(priorRequestId); + return valetPlacementService.saveRequesttest(request, "confirm", priorRequestId); + } + + @RequestMapping(value = "/{priorRequestId}/rollback1", consumes = "application/json", method = RequestMethod.PUT) + public ResponseEntity rollback1(@PathVariable("priorRequestId") String priorRequestId, @RequestBody JSONObject request) { + priorRequestId = UserUtils.htmlEscape(priorRequestId); + return valetPlacementService.saveRequesttest(request, "rollback", priorRequestId); + } + + +} diff --git a/valetapi/src/main/java/org/onap/fgps/api/controller/ValetUtilityController.java b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetUtilityController.java new file mode 100644 index 0000000..ef5d673 --- /dev/null +++ b/valetapi/src/main/java/org/onap/fgps/api/controller/ValetUtilityController.java @@ -0,0 +1,298 @@ +/* + * ============LICENSE_START========================================== + * ONAP - F-GPS API + * =================================================================== + * Copyright © 2019 ATT Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.fgps.api.controller; + +import java.io.InputStream; +import java.util.Properties; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.onap.fgps.api.annotation.AafRoleRequired; +import org.onap.fgps.api.annotation.BasicAuthRequired; +import org.onap.fgps.api.annotation.PropertyBasedAuthorization; +import org.onap.fgps.api.beans.Status; +import org.onap.fgps.api.beans.schema.Schema; +import org.onap.fgps.api.dao.ValetServicePlacementDAO; +import org.onap.fgps.api.logging.EELFLoggerDelegate; +import org.onap.fgps.api.utils.Constants; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.onap.fgps.api.utils.UserUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +@CrossOrigin(origins = "*") +@RestController +@EnableAutoConfiguration +@RequestMapping("/") +public class ValetUtilityController { + + //static final Logger LOGGER = LoggerFactory.getLogger(ValetServiceApplication.class); + private static EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ValetGroupsController.class); + + @Value("${logging.ping:false}") + private boolean pingLogFlag; + + @RequestMapping(value = "/alive", produces = "text/plain") + public String alive() { + return "ok"; + } + + @SuppressWarnings("unchecked") + @RequestMapping(value = "/ping", method = RequestMethod.GET) + public ResponseEntity ping() { + JSONObject pingResponse = new JSONObject(); + JSONObject valetStatus = new JSONObject(); + boolean allOk = true; + + valetStatus.put("valet_service", "ok"); + try { + ValetServicePlacementDAO valetServicePlacementDAO = new ValetServicePlacementDAO(pingLogFlag); + String response = valetServicePlacementDAO.getRow("pingRequest"); + if(response.contains("DBRequest Failed")) + valetStatus.put("db_service", "Failed"); + else + valetStatus.put("db_service", "OK"); + } catch (Exception e) { + valetStatus.put("DB_Service", "failed"); + allOk = false; + LOGGER.error(EELFLoggerDelegate.applicationLogger,"Ping failed!, Error details : "+e.getMessage()); + } + + pingResponse.put("status", valetStatus); + if (allOk) { + if(pingLogFlag) { + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Ping ok"); + } + return ResponseEntity.ok(pingResponse.toJSONString()); + } + LOGGER.error(EELFLoggerDelegate.errorLogger,"Ping failed!"); + return ResponseEntity.status(503).body(pingResponse.toJSONString()); + + } + + @SuppressWarnings("unchecked") + + @RequestMapping(value = "/healthcheck", method = RequestMethod.GET) + + public ResponseEntity healthcheck() { + + ValetServicePlacementDAO valetServicePlacementDAO = new ValetServicePlacementDAO(); + + Schema schema = new Schema(); + + JSONObject pingResponse = new JSONObject(); + + JSONObject valetStatus = new JSONObject(); + + valetStatus.put("valet_service", "ok"); + boolean allOk = true; + + try { + + JSONObject jObj = new JSONObject(); + + Properties props = new Properties(); + + String propFileName = "resources.properties"; + + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName); + if (inputStream != null) { + + props.load(inputStream); + + } else { + + LOGGER.info(EELFLoggerDelegate.applicationLogger,"DBProxy : inputstream is not"); + + } + String timeStamp = System.currentTimeMillis() + ""; + jObj.put("id", UserUtils.htmlEscape(props.getProperty("instanceId")) ); + String dbRequest = schema.formHealthCheckRequest(timeStamp, "ping", jObj.toJSONString()); + + String insertRow = valetServicePlacementDAO.insertRow(dbRequest); + boolean status = pollForResult(jObj, "ping-" + timeStamp, Constants.WAIT_UNITL_SECONDS, + + Constants.POLL_EVERY_SECONDS); + if (!status) allOk = false; + + valetStatus.put("DB_Service", "ok"); + + valetStatus.put("valet_engine", status ? "ok" : "not ok"); + + } catch (Exception e) { + + valetStatus.put("DB_Service", "not ok"); + allOk = false; + } + + pingResponse.put("status", valetStatus); + + if (allOk) return ResponseEntity.ok(pingResponse.toJSONString()); + return ResponseEntity.status(503).body(pingResponse.toJSONString()); + } + + public static JSONObject parseToJSON(String jsonString) { + LOGGER.info(EELFLoggerDelegate.applicationLogger,"parseToJSON : parsing json"); + JSONParser parser = new JSONParser(); + try { + JSONObject json = (JSONObject) parser.parse(jsonString); + return json; + } catch (ParseException e) { + e.printStackTrace(); + LOGGER.error(EELFLoggerDelegate.applicationLogger,"parseToJSON: Error details: "+ e.getMessage()); + LOGGER.error(EELFLoggerDelegate.errorLogger,"parseToJSON: Error details: "+ e.getMessage()); + return null; + } + } + + public boolean pollForResult(JSONObject values, String requestId, int waitUntilSeconds, int pollEverySeconds) { + LOGGER.info("pollForResult : called", requestId); + ValetServicePlacementDAO valetServicePlacementDAO = new ValetServicePlacementDAO(); + Schema schema = new Schema(); + + String result = null; + long waitUntil = System.currentTimeMillis() + (1000 * waitUntilSeconds); + int counter = 1; + + JSONObject response = new JSONObject(); + while (true) { + LOGGER.info(EELFLoggerDelegate.applicationLogger,"pollForResult : polling database - ", counter++); + + result = valetServicePlacementDAO.getRowFromResults(requestId); + System.out.println("getRowFromResults called count:" + counter); + response = result != null ? parseToJSON(result) : null; + + if (response != null && ((JSONObject) response.get("result")).get("row 0") != null) { + LOGGER.debug(EELFLoggerDelegate.debugLogger,"pollForResult : response recieved", result); + System.out.println("deleteRowFromResults called"); + valetServicePlacementDAO.deleteRowFromResults(requestId, schema.formMsoDeleteRequest()); + + } + if (System.currentTimeMillis() < waitUntil && response == null + || ((JSONObject) response.get("result")).get("row 0") == null) { + try { + Thread.sleep(1000 * pollEverySeconds); + } catch (InterruptedException e) { + e.printStackTrace(); + LOGGER.error(EELFLoggerDelegate.errorLogger,"pollForResult: Error details: "+ e.getMessage()); + } + } else { + break; + } + } + if (System.currentTimeMillis() > waitUntil) { + return false; + } + + return true; + } + + @AafRoleRequired(roleRequired = "org.onap.portal.valet.admin") + @RequestMapping(value = "/sample", produces = "application/json") + public String sample(HttpServletRequest request, HttpServletResponse response) { + return okMessage("Sample page doesn't do anything."); + } + + @AafRoleRequired(roleProperty = "portal.admin.role") + @RequestMapping(value = "/sample1", produces = "application/json") + public String sample1(HttpServletRequest request, HttpServletResponse response) { + return okMessage("Sample page does not do anything."); + } + + @BasicAuthRequired(authRequired = "portal") + @RequestMapping(value = "/sample2", produces = "application/json") + public String sample2(HttpServletRequest request, HttpServletResponse response) { + return okMessage("Sample page doesn't do a thing."); + } + + @PropertyBasedAuthorization("sample3") + @RequestMapping(value = "/sample3", produces = "application/json") + public String sample3(HttpServletRequest request, HttpServletResponse response) { + return okMessage("Sample page does nothing."); + } + + @RequestMapping(value = "/dark", produces = "application/json") + public String darkMessage(HttpServletRequest request, HttpServletResponse response) { + response.setStatus(400); + return failureMessage("Valet is running dark."); + } + + @RequestMapping(value = "/authfail", produces = "application/json") + public String authFail(HttpServletRequest request, HttpServletResponse response) { + response.setStatus(401); + return failureMessage(request.getAttribute("fail")); + } + + private String okMessage(Object message) { + return returnMessage("ok", message); + } + + private String failureMessage(Object message) { + return returnMessage("failed", message); + } + + private String returnMessage(String status, Object message) { + Status s = null; + if (message == null) { + s = new Status(status, "No failure message."); + } else { + s = new Status(status, message.toString()); + } + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); + try { + return mapper.writeValueAsString(s); + } catch (JsonProcessingException e) { + return "{\"status\": {\"status_code\": \"failed\", \"status_message\": \"Failed to generate failure string?!?\"} }"; + } + } + +} -- cgit 1.2.3-korg