baa998b541643773a900c48b93c1097e476d3045
[nonrtric.git] / dmaap-adaptor-java / src / main / java / org / oran / dmaapadapter / repository / InfoTypes.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2021 Nordix Foundation
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.oran.dmaapadapter.repository;
22
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Vector;
27
28 import org.oran.dmaapadapter.exceptions.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.http.HttpStatus;
32
33 public class InfoTypes {
34     private static final Logger logger = LoggerFactory.getLogger(InfoTypes.class);
35
36     private Map<String, InfoType> allTypes = new HashMap<>();
37
38     public InfoTypes(Collection<InfoType> types) {
39         for (InfoType type : types) {
40             put(type);
41         }
42     }
43
44     public synchronized InfoType get(String id) {
45         return allTypes.get(id);
46     }
47
48     public synchronized InfoType getType(String id) throws ServiceException {
49         InfoType type = allTypes.get(id);
50         if (type == null) {
51             throw new ServiceException("Could not find type: " + id, HttpStatus.NOT_FOUND);
52         }
53         return type;
54     }
55
56     public static class ConfigFile {
57         Collection<InfoType> types;
58     }
59
60     private synchronized void put(InfoType type) {
61         logger.debug("Put type: {}", type.getId());
62         allTypes.put(type.getId(), type);
63     }
64
65     public synchronized Iterable<InfoType> getAll() {
66         return new Vector<>(allTypes.values());
67     }
68
69     public synchronized Collection<String> typeIds() {
70         return allTypes.keySet();
71     }
72
73     public synchronized int size() {
74         return allTypes.size();
75     }
76
77     public synchronized void clear() {
78         allTypes.clear();
79     }
80 }