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