Add collaboration feature
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / application-config-rest / application-config-rest-services / src / main / java / org / openecomp / sdcrests / applicationconfig / rest / services / ApplicationConfigurationImpl.java
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.sdc.logging.context.impl.MdcDataDebugMessage;
28 import org.openecomp.sdc.logging.types.LoggerConstants;
29 import org.openecomp.sdc.logging.types.LoggerServiceName;
30 import org.openecomp.sdcrests.applicationconfig.rest.ApplicationConfiguration;
31 import org.openecomp.sdcrests.applicationconfig.rest.mapping.MapApplicationConfigEntityToApplicationConfigDto;
32 import org.openecomp.sdcrests.applicationconfig.rest.mapping.MapConfigurationDataToConfigurationDataDto;
33 import org.openecomp.sdcrests.applicationconfiguration.types.ApplicationConfigDto;
34 import org.openecomp.sdcrests.applicationconfiguration.types.ConfigurationDataDto;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.slf4j.MDC;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Service;
40
41 import javax.inject.Named;
42 import javax.ws.rs.core.Response;
43 import java.io.InputStream;
44 import java.util.Collection;
45
46 /**
47  * Created by Talio on 8/8/2016.
48  */
49
50 @Named
51 @Service("applicationConfiguration")
52 @Scope(value = "prototype")
53 public class ApplicationConfigurationImpl implements ApplicationConfiguration {
54
55   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
56   @Autowired
57   private ApplicationConfigManager applicationConfigManager;
58
59   @Override
60   public Response insertToTable(String namespace, String key, InputStream fileContainingSchema) {
61
62     mdcDataDebugMessage.debugEntryMessage(null, null);
63
64     MDC.put(LoggerConstants.SERVICE_NAME,
65         LoggerServiceName.Insert_To_ApplicationConfig_Table.toString());
66     String value = new String(FileUtils.toByteArray(fileContainingSchema));
67
68     applicationConfigManager.insertIntoTable(namespace, key, value);
69
70     mdcDataDebugMessage.debugExitMessage(null, null);
71     return Response.ok().build();
72   }
73
74   @Override
75   public Response getFromTable(String namespace, String key) {
76
77     mdcDataDebugMessage.debugEntryMessage(null, null);
78
79     MDC.put(LoggerConstants.SERVICE_NAME,
80         LoggerServiceName.Get_From_ApplicationConfig_Table.toString());
81     ConfigurationData value = applicationConfigManager.getFromTable(namespace, key);
82     ConfigurationDataDto valueDto = new MapConfigurationDataToConfigurationDataDto()
83         .applyMapping(value, ConfigurationDataDto.class);
84
85     mdcDataDebugMessage.debugExitMessage(null, null);
86
87     return Response.ok(valueDto).build();
88   }
89
90   @Override
91   public Response getListOfConfigurationByNamespaceFromTable(String namespace) {
92
93     mdcDataDebugMessage.debugEntryMessage(null, null);
94
95     MDC.put(LoggerConstants.SERVICE_NAME, LoggerServiceName
96         .Get_List_From_ApplicationConfig_Table_By_Namespace.toString());
97     Collection<ApplicationConfigEntity> applicationConfigEntities =
98         applicationConfigManager.getListOfConfigurationByNamespace(namespace);
99     GenericCollectionWrapper<ApplicationConfigDto> applicationConfigWrapper =
100         new GenericCollectionWrapper<>();
101     MapApplicationConfigEntityToApplicationConfigDto mapper =
102         new MapApplicationConfigEntityToApplicationConfigDto();
103
104     for (ApplicationConfigEntity applicationConfigEntity : applicationConfigEntities) {
105       applicationConfigWrapper
106           .add(mapper.applyMapping(applicationConfigEntity, ApplicationConfigDto.class));
107     }
108
109     mdcDataDebugMessage.debugExitMessage(null, null);
110
111     return Response.ok(applicationConfigWrapper).build();
112   }
113 }