push addional code
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-config-lib / src / main / java / org / openecomp / core / utilities / applicationconfig / impl / ApplicationConfigImpl.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.core.utilities.applicationconfig.impl;
22
23 import org.openecomp.core.utilities.applicationconfig.ApplicationConfig;
24 import org.openecomp.core.utilities.applicationconfig.dao.ApplicationConfigDao;
25 import org.openecomp.core.utilities.applicationconfig.dao.ApplicationConfigDaoFactory;
26 import org.openecomp.core.utilities.applicationconfig.dao.type.ApplicationConfigEntity;
27 import org.openecomp.core.utilities.applicationconfig.type.ConfigurationData;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.errors.ErrorCategory;
30 import org.openecomp.sdc.common.errors.ErrorCode;
31
32 import java.util.Collection;
33 import java.util.Objects;
34
35 /**
36  * The type Application config.
37  */
38 public class ApplicationConfigImpl implements ApplicationConfig {
39   private static final ApplicationConfigDao applicationConfigDao =
40       ApplicationConfigDaoFactory.getInstance().createInterface();
41
42   private static final String CONFIGURATION_SEARCH_ERROR = "CONFIGURATION_NOT_FOUND";
43   private static final String CONFIGURATION_SEARCH_ERROR_MSG =
44       "Configuration for namespace %s and key %s was not found";
45
46   @Override
47   public ConfigurationData getConfigurationData(String namespace, String key) {
48     ConfigurationData configurationData = applicationConfigDao.getConfigurationData(namespace, key);
49
50     if (Objects.isNull(configurationData)) {
51       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
52           .withCategory(ErrorCategory.APPLICATION)
53           .withId(CONFIGURATION_SEARCH_ERROR)
54           .withMessage(String.format(CONFIGURATION_SEARCH_ERROR_MSG, namespace, key))
55           .build());
56     }
57
58     return configurationData;
59   }
60
61   @Override
62   public void insertValue(String namespace, String key, String value) {
63     ApplicationConfigEntity applicationConfigEntity =
64         new ApplicationConfigEntity(namespace, key, value);
65     applicationConfigDao.create(applicationConfigEntity);
66   }
67
68   @Override
69   public Collection<ApplicationConfigEntity> getListOfConfigurationByNamespace(String namespace) {
70     ApplicationConfigEntity applicationConfigEntity =
71         new ApplicationConfigEntity(namespace, null, null);
72     return applicationConfigDao.list(applicationConfigEntity);
73   }
74 }