466611d9b49c1542ced1649ffa32329d70b1129f
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / servicecatalog / ServiceSpecificationDBManager.java
1 /**
2  *     Copyright (c) 2019 Amdocs
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.apis.servicecatalog;
17
18 import org.onap.nbi.apis.servicecatalog.model.ServiceSpecification;
19 import org.onap.nbi.apis.servicecatalog.model.SpecificationInputSchema;
20 import org.onap.nbi.apis.servicecatalog.repositories.ServiceSpecificationRepository;
21 import org.onap.nbi.apis.servicecatalog.repositories.SpecificationInputSchemaRepository;
22 import org.onap.nbi.exceptions.TechnicalException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Service;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Optional;
31
32 @Service
33 public class ServiceSpecificationDBManager {
34
35     @Autowired
36     ServiceSpecificationRepository serviceSpecificationRepository;
37
38     @Autowired
39     SpecificationInputSchemaRepository specificationInputSchemaRepository;
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceSpecificationService.class);
42
43     public void saveCatalogResponse(LinkedHashMap serviceCatalogResponse) {
44
45         ServiceSpecification serviceSpecification = new ServiceSpecification();
46         serviceSpecification.setId((String) serviceCatalogResponse.get("id"));
47         serviceSpecification.setCatalogResponse(serviceCatalogResponse);
48         serviceSpecificationRepository.save(serviceSpecification);
49
50    }
51
52     public boolean checkServiceSpecExistence(String serviceSpecId) {
53
54         return serviceSpecificationRepository.existsById(serviceSpecId);
55     }
56
57     public Map getServiceSpecification(String serviceSpecId) {
58
59        Optional<ServiceSpecification> optionalServiceSpecification = serviceSpecificationRepository.findById(serviceSpecId);
60        if(!optionalServiceSpecification.isPresent()) {
61            throw new TechnicalException("Unable get service specification");
62        }else {
63            return  optionalServiceSpecification.get().getCatalogResponse();
64        }
65     }
66
67     public boolean checkInputSchemaExistence(String serviceSpecId) {
68         return  specificationInputSchemaRepository.existsById(serviceSpecId);
69     }
70
71     public String getInputSchema(String serviceSpecId) {
72         Optional<SpecificationInputSchema> optionalSpecificationInputSchema = specificationInputSchemaRepository.findById(serviceSpecId);
73         if(!optionalSpecificationInputSchema.isPresent()) {
74             throw new TechnicalException("Unable get specification input schema");
75         }else {
76             return  optionalSpecificationInputSchema.get().getSpecificationSchemaJson();
77         }
78     }
79
80     public void saveSpecificationInputSchema(String svcCharacteristicsJson, Map serviceCatalogResponse) {
81             SpecificationInputSchema specificationInputSchema = new SpecificationInputSchema();
82             specificationInputSchema.setId((String) serviceCatalogResponse.get("id"));
83             specificationInputSchema.setSpecificationSchemaJson(svcCharacteristicsJson);
84             specificationInputSchemaRepository.save(specificationInputSchema);
85
86     }
87 }