aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/a1pesimulator/service/a1/RanA1ServiceLocalStoreImpl.java
blob: de451cf0befa348b34c466a6d7e8da4cd318c478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2021 Samsung Electronics
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.onap.a1pesimulator.service.a1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * A1 Service implementation which uses in-memory policy data store.
 */
@Service
public class RanA1ServiceLocalStoreImpl implements A1Service {

    private static final Logger log = LoggerFactory.getLogger(RanA1ServiceLocalStoreImpl.class);

    private Map<Integer, Map<String, String>> policyTypesMap = new HashMap<>();
    private Map<Integer, String> policySchemaMap = new HashMap<>();
    private ObjectMapper mapper;

    public RanA1ServiceLocalStoreImpl(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public ResponseEntity<String> healthCheck() throws RestClientException {
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<String> putPolicySchema(Integer policyTypeId, String body) {
        policySchemaMap.put(policyTypeId, body);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Override
    public ResponseEntity<String> putPolicy(final Integer policyTypeId, final String policyId, final String body) {
        log.debug("Create or update policy id {} of policy type id {} with following content {} ", policyId,
                policyTypeId, body);
        if (policyTypesMap.containsKey(policyTypeId)) {
            policyTypesMap.get(policyTypeId).put(policyId, body);
        } else {
            Map<String, String> policies = new HashMap<>();
            policies.put(policyId, body);
            policyTypesMap.put(policyTypeId, policies);
        }
        return ResponseEntity.accepted().build();
    }

    @Override
    public ResponseEntity<String> deletePolicy(final Integer policyTypeId, final String policyId) {
        log.debug("Delete policy id {} of policy type id {}", policyId, policyTypeId);
        if (policyTypesMap.containsKey(policyTypeId)) {
            policyTypesMap.get(policyTypeId).remove(policyId);
            return ResponseEntity.accepted().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Override
    public ResponseEntity<String> getPolicyTypeIds() throws RestClientException {
        return getRestAsString(policySchemaMap.keySet());
    }

    @Override
    public ResponseEntity<String> getPolicyType(final Integer policyTypeId) throws RestClientException {
        if (policySchemaMap.isEmpty() || !policySchemaMap.containsKey(policyTypeId)) {
            return ResponseEntity.notFound().build();
        } else {
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(policySchemaMap.get(policyTypeId));
        }
    }

    @Override
    public ResponseEntity<String> getPolicyIdsOfType(final Integer policyTypeId) throws RestClientException {
        Set<String> result = new HashSet<>();
        if (policyTypesMap.containsKey(policyTypeId)) {
            result = policyTypesMap.get(policyTypeId).keySet();
        }
        return getRestAsString(result);
    }

    @Override
    public ResponseEntity<String> getPolicy(final Integer policyTypeId, final String policyId)
            throws RestClientException {
        if (policyTypesMap.containsKey(policyTypeId) && policyTypesMap.get(policyTypeId).containsKey(policyId)) {
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
                    .body(policyTypesMap.get(policyTypeId).get(policyId));
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @Override
    public ResponseEntity<String> getAllPoliciesForType(final Integer policyTypeId) throws RestClientException {
        return getRestAsString(policyTypesMap.get(policyTypeId));
    }

    private ResponseEntity<String> getRestAsString(Object obj) throws RestClientException {
        try {
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(mapper.writeValueAsString(obj));
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Cannot serialize object", e);
        }
    }

}