aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/repository/Policies.java
blob: 45aa57e471a81550b103fc507177f59d0d9d2097 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
 * ======================================================================
 * 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.
 * ========================LICENSE_END===================================
 */

package org.onap.ccsdk.oran.a1policymanagementservice.repository;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import lombok.Builder;
import lombok.Getter;

import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.EntityNotFoundException;
import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.FileSystemUtils;

@SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
@Configuration
public class Policies {

    @Getter
    @Builder
    private static class PersistentPolicyInfo {
        private String id;
        private String json;
        private String ownerServiceId;
        private String ricId;
        private String typeId;
        private String statusNotificationUri;
        private boolean isTransient;
        private String lastModified;
    }

    private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private Map<String, Policy> policiesId = new HashMap<>();
    private MultiMap<Policy> policiesRic = new MultiMap<>();
    private MultiMap<Policy> policiesService = new MultiMap<>();
    private MultiMap<Policy> policiesType = new MultiMap<>();

    private final ApplicationConfig appConfig;
    private static Gson gson = new GsonBuilder().create();

    public Policies(@Autowired ApplicationConfig appConfig) {
        this.appConfig = appConfig;
    }

    public synchronized void put(Policy policy) {
        policiesId.put(policy.getId(), policy);
        policiesRic.put(policy.getRic().id(), policy.getId(), policy);
        policiesService.put(policy.getOwnerServiceId(), policy.getId(), policy);
        policiesType.put(policy.getType().getId(), policy.getId(), policy);
        if (this.appConfig.getVardataDirectory() != null && !policy.isTransient()) {
            store(policy);
        }
    }

    public synchronized boolean containsPolicy(String id) {
        return policiesId.containsKey(id);
    }

    public synchronized Policy get(String id) {
        return policiesId.get(id);
    }

    public synchronized Policy getPolicy(String id) throws EntityNotFoundException {
        Policy p = policiesId.get(id);
        if (p == null) {
            throw new EntityNotFoundException("Could not find policy: " + id);
        }
        return p;
    }

    public synchronized Collection<Policy> getAll() {
        return new Vector<>(policiesId.values());
    }

    public synchronized Collection<Policy> getForService(String service) {
        return policiesService.get(service);
    }

    public synchronized Collection<Policy> getForRic(String ric) {
        return policiesRic.get(ric);
    }

    public synchronized Collection<Policy> getForType(String type) {
        return policiesType.get(type);
    }

    public synchronized Policy removeId(String id) {
        Policy p = policiesId.get(id);
        if (p != null) {
            remove(p);
        }
        return p;
    }

    public synchronized void remove(Policy policy) {
        if (!policy.isTransient()) {
            try {
                Files.delete(getPath(policy));
            } catch (Exception e) {
                logger.debug("Could not delete policy from database: {}", e.getMessage());
            }
        }
        policiesId.remove(policy.getId());
        policiesRic.remove(policy.getRic().id(), policy.getId());
        policiesService.remove(policy.getOwnerServiceId(), policy.getId());
        policiesType.remove(policy.getType().getId(), policy.getId());
    }

    public synchronized void removePoliciesForRic(String ricId) {
        Collection<Policy> policiesForRic = getForRic(ricId);
        for (Policy policy : policiesForRic) {
            remove(policy);
        }
    }

    public synchronized int size() {
        return policiesId.size();
    }

    public synchronized void clear() {
        while (policiesId.size() > 0) {
            Set<String> keys = policiesId.keySet();
            removeId(keys.iterator().next());
        }
        try {
            if (this.appConfig.getVardataDirectory() != null) {
                FileSystemUtils.deleteRecursively(getDatabasePath());
            }
        } catch (Exception e) {
            logger.warn("Could not delete policy database : {}", e.getMessage());
        }
    }

    public void store(Policy policy) {
        try {
            Files.createDirectories(getDatabasePath(policy.getRic()));
            try (PrintStream out = new PrintStream(new FileOutputStream(getFile(policy)))) {
                out.print(gson.toJson(toStorageObject(policy)));
            }
        } catch (Exception e) {
            logger.warn("Could not store policy: {} {}", policy.getId(), e.getMessage());
        }
    }

    private File getFile(Policy policy) throws ServiceException {
        return getPath(policy).toFile();
    }

    private Path getPath(Policy policy) throws ServiceException {
        return Path.of(getDatabaseDirectory(policy.getRic()), policy.getId() + ".json");
    }

    public synchronized void restoreFromDatabase(Ric ric, PolicyTypes types) {
        try {
            Files.createDirectories(getDatabasePath(ric));
            for (File file : getDatabasePath(ric).toFile().listFiles()) {
                String json = Files.readString(file.toPath());
                PersistentPolicyInfo policyStorage = gson.fromJson(json, PersistentPolicyInfo.class);
                this.put(toPolicy(policyStorage, ric, types));
            }
            logger.debug("Restored policy database for RIC: {}, number of policies: {}", ric.id(),
                    this.policiesRic.get(ric.id()).size());
        } catch (Exception e) {
            logger.warn("Could not restore policy database for RIC: {}, reason : {}", ric.id(), e.getMessage());
        }
    }

    private PersistentPolicyInfo toStorageObject(Policy p) {
        return PersistentPolicyInfo.builder() //
                .id(p.getId()) //
                .json(p.getJson()) //
                .ownerServiceId(p.getOwnerServiceId()) //
                .ricId(p.getRic().id()) //
                .statusNotificationUri(p.getStatusNotificationUri()) //
                .typeId(p.getType().getId()) //
                .isTransient(p.isTransient()) //
                .lastModified(p.getLastModified().toString()) //
                .build();
    }

    Policy toPolicy(PersistentPolicyInfo p, Ric ric, PolicyTypes types) throws EntityNotFoundException {
        return Policy.builder() //
                .id(p.getId()) //
                .isTransient(p.isTransient()) //
                .json(p.getJson()) //
                .lastModified(Instant.parse(p.lastModified)) //
                .ownerServiceId(p.getOwnerServiceId()) //
                .ric(ric) //
                .statusNotificationUri(p.getStatusNotificationUri()) //
                .type(types.getType(p.getTypeId())) //
                .build();
    }

    private Path getDatabasePath(Ric ric) throws ServiceException {
        return Path.of(getDatabaseDirectory(ric));
    }

    private String getDatabaseDirectory(Ric ric) throws ServiceException {
        return getDatabaseDirectory() + "/" + ric.id();
    }

    private String getDatabaseDirectory() throws ServiceException {
        if (appConfig.getVardataDirectory() == null) {
            throw new ServiceException("No database storage provided");
        }
        return appConfig.getVardataDirectory() + "/database/policyInstances";
    }

    private Path getDatabasePath() throws ServiceException {
        return Path.of(getDatabaseDirectory());
    }
}