<commit_before>/*
 * Copyright (c) 2018 Swisscom (Switzerland) Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file 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.
 */

package com.swisscom.cloud.sb.broker.servicedefinition

import com.google.common.base.Preconditions
import com.google.common.base.Strings
import com.swisscom.cloud.sb.broker.backup.BackupRestoreProvider
import com.swisscom.cloud.sb.broker.error.ErrorCode
import com.swisscom.cloud.sb.broker.metrics.PlanBasedMetricsService
import com.swisscom.cloud.sb.broker.model.CFService
import com.swisscom.cloud.sb.broker.model.CFServiceMetadata
import com.swisscom.cloud.sb.broker.model.CFServicePermission
import com.swisscom.cloud.sb.broker.model.Parameter
import com.swisscom.cloud.sb.broker.model.Plan
import com.swisscom.cloud.sb.broker.model.PlanMetadata
import com.swisscom.cloud.sb.broker.model.Tag
import com.swisscom.cloud.sb.broker.model.repository.CFServiceMetaDataRepository
import com.swisscom.cloud.sb.broker.model.repository.CFServicePermissionRepository
import com.swisscom.cloud.sb.broker.model.repository.CFServiceRepository
import com.swisscom.cloud.sb.broker.model.repository.ParameterRepository
import com.swisscom.cloud.sb.broker.model.repository.PlanMetadataRepository
import com.swisscom.cloud.sb.broker.model.repository.PlanRepository
import com.swisscom.cloud.sb.broker.model.repository.ServiceInstanceRepository
import com.swisscom.cloud.sb.broker.model.repository.TagRepository
import com.swisscom.cloud.sb.broker.servicedefinition.converter.ServiceDtoConverter
import com.swisscom.cloud.sb.broker.servicedefinition.dto.ServiceDto
import com.swisscom.cloud.sb.broker.services.common.ServiceProviderLookup
import com.swisscom.cloud.sb.broker.util.JsonHelper
import com.swisscom.cloud.sb.broker.util.JsonSchemaHelper
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Transactional
@Slf4j
class ServiceDefinitionProcessor {
    @Autowired
    ServiceProviderLookup serviceProviderLookup

    @Autowired
    ServiceDtoConverter serviceDtoConverter

    @Autowired
    private CFServiceRepository cfServiceRepository

    @Autowired
    private PlanRepository planRepository
    @Autowired
    private ServiceInstanceRepository serviceInstanceRepository
    @Autowired
    private TagRepository tagRepository
    @Autowired
    private PlanMetadataRepository planMetadataRepository
    @Autowired
    private CFServiceMetaDataRepository cfServiceMetaDataRepository
    @Autowired
    private CFServicePermissionRepository servicePermissionRepository
    @Autowired
    private ParameterRepository parameterRepository

    @Autowired
    private List<PlanBasedMetricsService> planBasedMetricServices

    def createOrUpdateServiceDefinition(String content) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(content),"Service Definition can't be empty")
        // Preconditions.checkArgument() // TODO: check content type is application/json

        def serviceJson
        try {
          serviceJson = new JsonSlurper().parseText(content)
        } catch(Exception e) {
          ErrorCode.INVALID_JSON.throwNew()
        }

        CFService service = processServiceBasicDefiniton(serviceJson)
        processServiceTags(service, serviceJson)
        processServiceMetadata(service, serviceJson)
        processServicePermissions(service, serviceJson)
        processPlans(service, serviceJson)
    }

    def createOrUpdateServiceDefinitionFromYaml(ServiceDto serviceDto) {
        CFService service = processServiceBasicDefiniton(serviceDto)
        processServiceTags(service, serviceDto)
        processServiceMetadata(service, serviceDto)
        processServicePermissions(service, serviceDto)
        processPlans(service, serviceDto)
    }

    def deleteServiceDefinition(String id) {
        CFService service = cfServiceRepository.findByGuid(id)
        if (!service) {
            ErrorCode.SERVICE_NOT_FOUND.throwNew()
        }

        def serviceInstances = serviceInstanceRepository.findByPlanIdIn(service.plans.collect { it.id })
        if (serviceInstances && serviceInstances.size() > 0) {
            ErrorCode.SERVICE_IN_USE.throwNew()
        }

        cfServiceRepository.delete(service)
    }<commit_msg>Remove plans before deleting service<commit_after>/*
 * Copyright (c) 2018 Swisscom (Switzerland) Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file 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.
 */

package com.swisscom.cloud.sb.broker.servicedefinition

import com.google.common.base.Preconditions
import com.google.common.base.Strings
import com.swisscom.cloud.sb.broker.backup.BackupRestoreProvider
import com.swisscom.cloud.sb.broker.error.ErrorCode
import com.swisscom.cloud.sb.broker.metrics.PlanBasedMetricsService
import com.swisscom.cloud.sb.broker.model.CFService
import com.swisscom.cloud.sb.broker.model.CFServiceMetadata
import com.swisscom.cloud.sb.broker.model.CFServicePermission
import com.swisscom.cloud.sb.broker.model.Parameter
import com.swisscom.cloud.sb.broker.model.Plan
import com.swisscom.cloud.sb.broker.model.PlanMetadata
import com.swisscom.cloud.sb.broker.model.Tag
import com.swisscom.cloud.sb.broker.model.repository.CFServiceMetaDataRepository
import com.swisscom.cloud.sb.broker.model.repository.CFServicePermissionRepository
import com.swisscom.cloud.sb.broker.model.repository.CFServiceRepository
import com.swisscom.cloud.sb.broker.model.repository.ParameterRepository
import com.swisscom.cloud.sb.broker.model.repository.PlanMetadataRepository
import com.swisscom.cloud.sb.broker.model.repository.PlanRepository
import com.swisscom.cloud.sb.broker.model.repository.ServiceInstanceRepository
import com.swisscom.cloud.sb.broker.model.repository.TagRepository
import com.swisscom.cloud.sb.broker.servicedefinition.converter.ServiceDtoConverter
import com.swisscom.cloud.sb.broker.servicedefinition.dto.ServiceDto
import com.swisscom.cloud.sb.broker.services.common.ServiceProviderLookup
import com.swisscom.cloud.sb.broker.util.JsonHelper
import com.swisscom.cloud.sb.broker.util.JsonSchemaHelper
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
@Transactional
@Slf4j
class ServiceDefinitionProcessor {
    @Autowired
    ServiceProviderLookup serviceProviderLookup

    @Autowired
    ServiceDtoConverter serviceDtoConverter

    @Autowired
    private CFServiceRepository cfServiceRepository

    @Autowired
    private PlanRepository planRepository
    @Autowired
    private ServiceInstanceRepository serviceInstanceRepository
    @Autowired
    private TagRepository tagRepository
    @Autowired
    private PlanMetadataRepository planMetadataRepository
    @Autowired
    private CFServiceMetaDataRepository cfServiceMetaDataRepository
    @Autowired
    private CFServicePermissionRepository servicePermissionRepository
    @Autowired
    private ParameterRepository parameterRepository

    @Autowired
    private List<PlanBasedMetricsService> planBasedMetricServices

    def createOrUpdateServiceDefinition(String content) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(content),"Service Definition can't be empty")
        // Preconditions.checkArgument() // TODO: check content type is application/json

        def serviceJson
        try {
          serviceJson = new JsonSlurper().parseText(content)
        } catch(Exception e) {
          ErrorCode.INVALID_JSON.throwNew()
        }

        CFService service = processServiceBasicDefiniton(serviceJson)
        processServiceTags(service, serviceJson)
        processServiceMetadata(service, serviceJson)
        processServicePermissions(service, serviceJson)
        processPlans(service, serviceJson)
    }

    def createOrUpdateServiceDefinitionFromYaml(ServiceDto serviceDto) {
        CFService service = processServiceBasicDefiniton(serviceDto)
        processServiceTags(service, serviceDto)
        processServiceMetadata(service, serviceDto)
        processServicePermissions(service, serviceDto)
        processPlans(service, serviceDto)
    }

    def deleteServiceDefinition(String id) {
        CFService service = cfServiceRepository.findByGuid(id)
        if (!service) {
            ErrorCode.SERVICE_NOT_FOUND.throwNew()
        }

        def serviceInstances = serviceInstanceRepository.findByPlanIdIn(service.plans.collect { it.id })
        if (serviceInstances && serviceInstances.size() > 0) {
            ErrorCode.SERVICE_IN_USE.throwNew()
        }
        service.plans.each { plan -> planRepository.delete(plan) }
        cfServiceRepository.delete(service)
    }
{'fraction_non_alphanumeric': 0.07417554540842212, 'fraction_numerical': 0.002232369355657027, 'mean_word_length': 4.512304250559284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6966661', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2666, 'n_words': 531}