9a722a237b7aea77a192bfc15c78193a247b68dc
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdcrests.applicationconfig.rest.services;
22
23 import org.openecomp.core.utilities.applicationconfig.dao.type.ApplicationConfigEntity;
24 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.applicationconfig.ApplicationConfigManager;
27 import org.openecomp.sdcrests.applicationconfig.rest.ApplicationConfiguration;
28 import org.openecomp.sdcrests.applicationconfig.rest.mapping.MapApplicationConfigEntityToApplicationConfigDto;
29 import org.openecomp.sdcrests.applicationconfig.rest.mapping.MapConfigurationDataToConfigurationDataDto;
30 import org.openecomp.sdcrests.applicationconfiguration.types.ApplicationConfigDto;
31 import org.openecomp.sdcrests.applicationconfiguration.types.ConfigurationDataDto;
32 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
36
37 import javax.inject.Named;
38 import javax.ws.rs.core.Response;
39 import java.io.InputStream;
40 import java.util.Collection;
41
42 /**
43  * Created by Talio on 8/8/2016.
44  */
45
46 @Named
47 @Service("applicationConfiguration")
48 @Scope(value = "prototype")
49 public class ApplicationConfigurationImpl implements ApplicationConfiguration {
50   @Autowired
51   private ApplicationConfigManager applicationConfigManager;
52
53   @Override
54   public Response insertToTable(String namespace, String key, InputStream fileContainingSchema) {
55     String value = new String(FileUtils.toByteArray(fileContainingSchema));
56     applicationConfigManager.insertIntoTable(namespace, key, value);
57     return Response.ok().build();
58   }
59
60   @Override
61   public Response getFromTable(String namespace, String key) {
62     ConfigurationData value = applicationConfigManager.getFromTable(namespace, key);
63     ConfigurationDataDto valueDto = new MapConfigurationDataToConfigurationDataDto()
64         .applyMapping(value, ConfigurationDataDto.class);
65     return Response.ok(valueDto).build();
66   }
67
68   @Override
69   public Response getListOfConfigurationByNamespaceFromTable(String namespace) {
70     Collection<ApplicationConfigEntity> applicationConfigEntities =
71         applicationConfigManager.getListOfConfigurationByNamespace(namespace);
72     GenericCollectionWrapper<ApplicationConfigDto> applicationConfigWrapper =
73         new GenericCollectionWrapper<>();
74     MapApplicationConfigEntityToApplicationConfigDto mapper =
75         new MapApplicationConfigEntityToApplicationConfigDto();
76
77     for (ApplicationConfigEntity applicationConfigEntity : applicationConfigEntities) {
78       applicationConfigWrapper
79           .add(mapper.applyMapping(applicationConfigEntity, ApplicationConfigDto.class));
80     }
81     return Response.ok(applicationConfigWrapper).build();
82   }
83 }