Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / controller / VidController.java
index e22448a..f21036e 100755 (executable)
 \r
 package org.openecomp.vid.controller;\r
 \r
-import java.io.InputStream;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-import java.text.DateFormat;\r
-import java.text.SimpleDateFormat;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Date;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Map.Entry;\r
-import java.util.UUID;\r
-\r
-import javax.net.ssl.SSLContext;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.ws.rs.client.ClientBuilder;\r
-import javax.ws.rs.client.Client;\r
-\r
-import org.json.JSONObject;\r
-import org.json.JSONTokener;\r
+import org.openecomp.portalsdk.core.controller.RestrictedBaseController;\r
+import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
+import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;\r
+import org.openecomp.vid.asdc.AsdcCatalogException;\r
+import org.openecomp.vid.asdc.beans.SecureServices;\r
 import org.openecomp.vid.exceptions.VidServiceUnavailableException;\r
-import org.openecomp.vid.model.ModelConstants;\r
-import org.openecomp.vid.model.Network;\r
 import org.openecomp.vid.model.ServiceModel;\r
-import org.openecomp.vid.model.VNF;\r
-import org.openecomp.vid.model.VfModule;\r
-import org.openecomp.vid.model.VolumeGroup;\r
-//import org.openecomp.vid.model.Service;\r
+import org.openecomp.vid.roles.Role;\r
+import org.openecomp.vid.roles.RoleProvider;\r
+import org.openecomp.vid.roles.RoleValidator;\r
+import org.openecomp.vid.services.VidService;\r
 import org.springframework.beans.factory.annotation.Autowired;\r
-import org.springframework.context.ApplicationContext;\r
-import org.springframework.context.annotation.Bean;\r
 import org.springframework.web.bind.annotation.PathVariable;\r
 import org.springframework.web.bind.annotation.RequestMapping;\r
 import org.springframework.web.bind.annotation.RequestMethod;\r
 import org.springframework.web.bind.annotation.RestController;\r
 import org.springframework.web.servlet.ModelAndView;\r
 \r
-import org.openecomp.portalsdk.core.controller.RestrictedBaseController;\r
-import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
-import org.openecomp.vid.asdc.AsdcCatalogException;\r
-import org.openecomp.vid.asdc.AsdcClient;\r
-import org.openecomp.vid.asdc.beans.Resource;\r
-import org.openecomp.vid.asdc.beans.Service;\r
-import org.openecomp.vid.asdc.beans.tosca.Group;\r
-import org.openecomp.vid.asdc.beans.tosca.NodeTemplate;\r
-import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;\r
-import org.openecomp.vid.asdc.beans.tosca.ToscaModel;\r
-import org.openecomp.vid.asdc.memory.InMemoryAsdcClient;\r
-import org.openecomp.vid.asdc.rest.RestfulAsdcClient;\r
-import org.openecomp.vid.properties.AsdcClientConfiguration;\r
-import org.openecomp.vid.properties.AsdcClientConfiguration.AsdcClientType;\r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
-import org.openecomp.vid.properties.VidProperties;\r
-import java.security.KeyManagementException;\r
-import java.security.NoSuchAlgorithmException;\r
+import javax.servlet.http.HttpServletRequest;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+//import org.openecomp.vid.model.Service;\r
 \r
-/**\r
- * The Class VidController.\r
- */\r
 @RestController\r
 public class VidController extends RestrictedBaseController {\r
        \r
-       /** The Constant LOG. */\r
        private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(VidController.class);\r
-       \r
-       /** The Constant dateFormat. */\r
-       final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
 \r
-       /** The app context. */\r
-       @Autowired\r
-       private ApplicationContext appContext;\r
-       \r
-       /**\r
-        * Gets the object mapper.\r
-        *\r
-        * @return the object mapper\r
-        */\r
-       @Bean\r
-       public ObjectMapper getObjectMapper() {\r
-               return new ObjectMapper();\r
-       }\r
-       \r
-       /**\r
-        * Gets the asdc client.\r
-        *\r
-        * @return the asdc client\r
-        */\r
-       @Bean\r
-       public AsdcClient getAsdcClient() {\r
-               \r
-               final AsdcClientConfiguration asdcClientConfig = appContext.getBean(AsdcClientConfiguration.class);\r
+       private final VidService service;\r
 \r
-               switch (asdcClientConfig.getAsdcClientType()) {\r
-               case IN_MEMORY:\r
-                       final InputStream asdcCatalogFile = VidController.class.getClassLoader().getResourceAsStream("catalog.json");\r
-                       final JSONTokener tokener = new JSONTokener(asdcCatalogFile);\r
-                       final JSONObject catalog = new JSONObject(tokener);\r
-\r
-                       return new InMemoryAsdcClient.Builder().catalog(catalog).build();\r
-               case REST:\r
+       @Autowired\r
+       public VidController(VidService vidService) throws SdcToscaParserException{\r
 \r
-                       final String protocol = asdcClientConfig.getAsdcClientProtocol();\r
-                       final String host = asdcClientConfig.getAsdcClientHost();\r
-                       final int port = asdcClientConfig.getAsdcClientPort();\r
-                       final String auth = asdcClientConfig.getAsdcClientAuth();\r
-                       Client cl = null;\r
-                       if ( protocol.equalsIgnoreCase("https") ) {\r
-                               try {\r
-                                       SSLContext ctx = SSLContext.getInstance("TLSv1.2");\r
-                                       ctx.init(null, null, null);\r
-                                       cl = ClientBuilder.newBuilder().sslContext(ctx).build();\r
-                               }\r
-                               catch ( NoSuchAlgorithmException n ) {\r
-                                       throw new RuntimeException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n);\r
-                               }\r
-                               catch ( KeyManagementException k ) {\r
-                                       throw new RuntimeException("SDC Client could not be instantiated due to a key management exception", k);\r
-                               }\r
-                       }\r
-                       else {\r
-                               cl = ClientBuilder.newBuilder().build();\r
-                       }\r
-                       \r
-                       try {\r
-                               final URI uri = new URI(protocol + "://" + host + ":" + port + "/");\r
-                               return new RestfulAsdcClient.Builder(cl, uri)\r
-                                                               .auth(auth)\r
-                                                               .build();\r
-                       } catch (URISyntaxException e) {\r
-                               throw new RuntimeException("SDC Client could not be instantiated due to a syntax error in the URI", e);\r
-                       }\r
-                       \r
-               default:\r
-                       throw new RuntimeException(asdcClientConfig.getAsdcClientType() + " is invalid; must be one of " + Arrays.toString(AsdcClientType.values()));\r
-               }\r
+               service = vidService;\r
        }\r
-       \r
+//     \r
        /**\r
         * Gets the services.\r
         *\r
@@ -165,9 +65,17 @@ public class VidController extends RestrictedBaseController {
         * @throws VidServiceUnavailableException the vid service unavailable exception\r
         */\r
        @RequestMapping(value={"/rest/models/services"}, method = RequestMethod.GET)\r
-       public Collection<org.openecomp.vid.asdc.beans.Service> getServices(HttpServletRequest request) throws VidServiceUnavailableException {\r
+       public SecureServices getServices(HttpServletRequest request) throws VidServiceUnavailableException {\r
                try {\r
-                       return getAsdcClient().getServices(request.getParameterMap());\r
+                       LOG.info("Start API for browse ASDC was called");\r
+                       SecureServices secureServices = new SecureServices();\r
+                       RoleProvider roleProvider = new RoleProvider();\r
+                       Map<String, String[]> requestParams = request.getParameterMap();\r
+                       List<Role> roles = new RoleProvider().getUserRoles(request);\r
+                       secureServices.setServices(service.getServices(requestParams));\r
+                       //Disable roles until AAF integration finishes\r
+                       //secureServices.setReadOnly(roleProvider.userPermissionIsReadOnly(roles));\r
+                       return secureServices;\r
                } catch (AsdcCatalogException e) {\r
                        LOG.error("Failed to retrieve service definitions from SDC", e);\r
                        throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);\r
@@ -186,156 +94,16 @@ public class VidController extends RestrictedBaseController {
         * @throws VidServiceUnavailableException the vid service unavailable exception\r
         */\r
        @RequestMapping(value={"/rest/models/services/{uuid}"}, method = RequestMethod.GET)\r
-       public ServiceModel getServices(@PathVariable("uuid") String uuid) throws VidServiceUnavailableException {\r
-               String methodName = "getServices";\r
-        LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");\r
-        boolean isNewFlow = false;\r
-        \r
-        String asdcModelNamespace = VidProperties.getAsdcModelNamespace();\r
-        \r
-        String vnfTag = asdcModelNamespace + ModelConstants.VNF;\r
-       String networkTag = asdcModelNamespace + ModelConstants.NETWORK;\r
-       String vfModuleTag = asdcModelNamespace + ModelConstants.VF_MODULE;\r
-       \r
+       public ServiceModel getServices(@PathVariable("uuid") String uuid, HttpServletRequest request) throws VidServiceUnavailableException {\r
                try {\r
-                       final ServiceModel serviceModel = new ServiceModel();\r
-                       final Map<String, VNF> vnfs = new HashMap<String, VNF> ();\r
-                       final Map<String, Network> networks = new HashMap<String, Network> ();\r
-                       \r
-                       final ToscaCsar serviceCsar = getAsdcClient().getServiceToscaModel(UUID.fromString(uuid));\r
-                       final Service asdcServiceMetadata = getAsdcClient().getService(UUID.fromString(uuid));\r
-                       final ToscaModel asdcServiceToscaModel = serviceCsar.getParent();\r
-                       \r
-                       serviceModel.setService(ServiceModel.extractService(asdcServiceToscaModel, asdcServiceMetadata));\r
-                       \r
-                       for (Entry<String, NodeTemplate> component: asdcServiceToscaModel.gettopology_template().getnode_templates().entrySet()) {\r
-                               final String modelCustomizationName = component.getKey();\r
-                               LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " model customization name: " + modelCustomizationName);\r
-                               final NodeTemplate nodeTemplate = component.getValue();\r
-                               final String type = nodeTemplate.getType();\r
-                               \r
-                               if (type.startsWith(vnfTag)) {\r
-                    LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " found node template type: " + type);\r
-\r
-                                       final UUID vnfUuid = UUID.fromString(nodeTemplate.getMetadata().getUUID());\r
-                                       final VNF vnf = new VNF();\r
-                                       vnf.extractVnf(modelCustomizationName, nodeTemplate);\r
-                                       \r
-                                       if (vnf.getVersion() == null) {\r
-                                               // vnf version should always be populated. The call below may not return the correct metadata since\r
-                                               // uuid is not unique\r
-                                               final Resource vnfMetadata = getAsdcClient().getResource(UUID.fromString(nodeTemplate.getMetadata().getUUID()));\r
-                                               vnf.setVersion(vnfMetadata.getVersion());\r
-                                       }\r
-              \r
-                    LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " VNF commands: " + vnf.getCommands());\r
-                                       vnfs.put(modelCustomizationName, vnf);\r
-                                       if ( (vnf.getCustomizationUuid() != null) && (vnf.getCustomizationUuid().length() > 0 ) ) {\r
-                                               isNewFlow = true;\r
-                                       }\r
-                               }\r
-                               // Networks\r
-                               if (type.startsWith(networkTag)) {\r
-                                       LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " found node template type: " + type);\r
-                                       final UUID networkUuid = UUID.fromString(nodeTemplate.getMetadata().getUUID());\r
-                                       final Network network = new Network();\r
-                                       network.extractNetwork(modelCustomizationName, nodeTemplate);\r
-                                       \r
-                                       if (network.getVersion() == null) {\r
-                                               // network version should always be populated. The call below may not return the correct metadata since\r
-                                               // uuid is not unique\r
-                                               final Resource networkMetadata = getAsdcClient().getResource(UUID.fromString(nodeTemplate.getMetadata().getUUID()));\r
-                                               network.setVersion(networkMetadata.getVersion());\r
-                                       }\r
-                                       if ( (network.getCustomizationUuid() != null) && (network.getCustomizationUuid().length() > 0 ) ) {\r
-                                               isNewFlow = true;\r
-                                       }\r
-                                       networks.put(modelCustomizationName, network);\r
-                                       \r
-                               }               \r
-                       }\r
-                       serviceModel.setVnfs(vnfs);\r
-                       serviceModel.setNetworks(networks);\r
-                       // If we see customization uuid under vnf or network, follow 1702 flow\r
-                       if ( isNewFlow ) {\r
-                               return ( getCustomizedServices(asdcServiceToscaModel, serviceModel) );\r
-                       }\r
-                       VNF vnf = null;\r
-                       for (ToscaModel vnfModel : serviceCsar.getChildren()) {\r
-                               \r
-                               // using uuid to match should only be valid for 1610 models\r
-                               \r
-                               final String vnfUuid = (vnfModel.getMetadata().getUUID());\r
-                               // find the VNF with that uuid, uuid is not the key anymore\r
-                               for ( Entry<String, VNF> vnfComp : vnfs.entrySet() ) {\r
-                                       if ( ( ( vnfComp.getValue().getUuid() ).equalsIgnoreCase(vnfUuid) ) ) {\r
-                                               // found the vnf\r
-                                               vnf = vnfComp.getValue();\r
-                                       }\r
-                               }\r
-                               final Map<String, VfModule> vfModules = new HashMap<String, VfModule> ();\r
-                               final Map<String, VolumeGroup> volumeGroups = new HashMap<String, VolumeGroup> ();\r
-                               \r
-                               if (vnf == null) {\r
-                                       LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");\r
-                                       continue;\r
-                               }\r
-\r
-                               vnf.setInputs(vnfModel.gettopology_template().getInputs());\r
-\r
-                               for (Entry<String, Group> component1 : vnfModel.gettopology_template().getGroups().entrySet()) {\r
-                                       final Group group = component1.getValue();\r
-                                       final String type = group.getType();\r
-                                       final String modelCustomizationName = component1.getKey();\r
-                                       \r
-                                       // VF Module Customization UUID: We may have the complete set of all VF Modules for all VNFs under service and VF Modules under each VNF.\r
-                                       // Keep using the VF Modules under VNFs but we need to get the customization uuid from the service level and put them\r
-                                       // under each VF module at the VNF level\r
-                                       \r
-                                       if (type.startsWith(vfModuleTag)) {\r
-                                               \r
-                                               VfModule vfMod = VfModule.extractVfModule(modelCustomizationName, group);\r
-                                               \r
-                                               // Add the vf module customization uuid from the service model\r
-                                               // The key of the VF Module in the service level will be the VF instance name appended to the VF Module name: \r
-                                               // <VF instance name>..<VF Module name>\r
-                                               /* String normalizedVnfCustomizationName = VNF.normalizeName (vnf.getModelCustomizationName());\r
-                                               org.openecomp.vid.model.Service.extractVfModuleCustomizationUUID (serviceModel.getService(), normalizedVnfCustomizationName, vfMod);*/\r
-                                                               \r
-                                               vfModules.put(modelCustomizationName, vfMod);\r
-                                               \r
-                                               if ( vfMod.isVolumeGroupAllowed() ) {\r
-                                                               volumeGroups.put(modelCustomizationName, VolumeGroup.extractVolumeGroup(modelCustomizationName, group));\r
-                                               }\r
-                                               \r
-                                       }\r
-                               }\r
-                               \r
-                               vnf.setVfModules(vfModules);\r
-                               vnf.setVolumeGroups(volumeGroups);\r
-                       }\r
-                       \r
-                       serviceModel.setVnfs(vnfs);\r
-                       serviceModel.setNetworks(networks);\r
-                       \r
-                       return serviceModel;\r
+//                     RoleValidator roleValidator = new RoleValidator(new RoleProvider().getUserRoles(request));\r
+                       return service.getService(uuid);\r
                } catch (AsdcCatalogException e) {\r
                        LOG.error("Failed to retrieve service definitions from SDC", e);\r
                        throw new VidServiceUnavailableException("Failed to retrieve service definitions from SDC", e);\r
                }\r
        }\r
 \r
-       public ServiceModel getCustomizedServices(ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {\r
-               String methodName = "asdcServiceToscaModel";\r
-        LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");\r
-        \r
-        // asdcServiceToscaModel should have vf modules and vol groups populated at this point but \r
-        // they are not associated with the VNFs\r
-               serviceModel.extractGroups(asdcServiceToscaModel);\r
-               // Now put the vf modules and volume groups under the VNF they belong too\r
-               serviceModel.associateGroups();\r
-               return (serviceModel);\r
-       }\r
 \r
        /**\r
         * Gets the services view.\r