bc346a8a6341e08e509fc7bd537ffad2174f9c1e
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / unique-type-rest / unique-type-rest-services / src / main / java / org / openecomp / sdcrests / uniquevalue / rest / services / UniqueTypesImpl.java
1 /*
2  * Copyright © 2018 European Support Limited
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.openecomp.sdcrests.uniquevalue.rest.services;
17
18 import org.openecomp.core.dao.UniqueValueDaoFactory;
19 import org.openecomp.core.util.UniqueValueUtil;
20 import org.openecomp.sdc.common.errors.ErrorCategory;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
23 import org.openecomp.sdcrests.uniquevalue.rest.UniqueTypes;
24 import org.openecomp.sdcrests.uniquevalue.types.UniqueTypesProvider;
25 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
26 import org.springframework.context.annotation.Scope;
27 import org.springframework.stereotype.Service;
28
29 import javax.inject.Named;
30 import javax.ws.rs.core.Response;
31
32 import java.util.*;
33
34 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
35
36 @Named
37 @Service("uniqueTypes")
38 @Scope(value = "prototype")
39 public class UniqueTypesImpl implements UniqueTypes {
40
41   private static final String UNIQUE_TYPE_NOT_FOUND_ERR_ID = "UNIQUE_TYPE_NOT_FOUND";
42   private static final String UNIQUE_TYPE_NOT_FOUND_MSG = "%s is not a supported unique type.";
43
44   private static final Map<String, String> UNIQUE_TYPE_TO_INTERNAL;
45
46   static {
47     Map<String, String> uniqueTypes = new HashMap<>();
48     ServiceLoader.load(UniqueTypesProvider.class)
49         .forEach(typesProvider -> uniqueTypes.putAll(typesProvider.listUniqueTypes()));
50     UNIQUE_TYPE_TO_INTERNAL = Collections.unmodifiableMap(uniqueTypes);
51   }
52
53   private final UniqueValueUtil uniqueValueUtil =
54       new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface());
55
56   @Override
57   public Response listUniqueTypes(String user) {
58     return Response.ok(
59         new GenericCollectionWrapper<>(new ArrayList<>(UNIQUE_TYPE_TO_INTERNAL.keySet())))
60         .build();
61   }
62
63   @Override
64   public Response getUniqueValue(String type, String value, String user) {
65     String internalType = UNIQUE_TYPE_TO_INTERNAL.get(type);
66
67     if (internalType == null) {
68       ErrorCode error = new ErrorCode.ErrorCodeBuilder()
69           .withCategory(ErrorCategory.APPLICATION)
70           .withId(UNIQUE_TYPE_NOT_FOUND_ERR_ID)
71           .withMessage(String.format(UNIQUE_TYPE_NOT_FOUND_MSG, type)).build();
72       return Response.status(NOT_FOUND).entity(new ErrorCodeAndMessage(NOT_FOUND, error)).build();
73     }
74
75     return Response.ok(Collections
76         .singletonMap("occupied", uniqueValueUtil.isUniqueValueOccupied(internalType, value)))
77         .build();
78   }
79 }