3f301b77948f5a44edbe91a7ab15e25f41316081
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.util.v3;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import lombok.RequiredArgsConstructor;
26 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
27 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation;
28 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
29 import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.*;
31 import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.TokenService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.stereotype.Component;
38 import reactor.core.publisher.Flux;
39 import org.springframework.web.server.ServerWebExchange;
40 import org.springframework.web.util.UriComponentsBuilder;
41 import reactor.core.publisher.Mono;
42
43 import java.lang.invoke.MethodHandles;
44 import java.time.Instant;
45 import java.util.Collection;
46 import java.util.UUID;
47 import java.util.stream.Collectors;
48
49 @Component
50 @RequiredArgsConstructor
51 public class Helper {
52
53     @Autowired
54     private TokenService tokenService;
55
56     private final Services services;
57
58     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     private static final Gson gson = new GsonBuilder().create();
61
62     public void keepServiceAlive(String name) {
63         Service s = this.services.get(name);
64         if (s != null) {
65             s.keepAlive();
66         }
67     }
68
69     public Mono<Ric> checkRicStateIdle(Ric ric) {
70         if (ric.getState() == Ric.RicState.AVAILABLE) {
71             return Mono.just(ric);
72         } else {
73             logger.debug("Request rejected Near-RT RIC not IDLE, ric: {}", ric);
74             ServiceException e = new ServiceException(
75                     "Near-RT RIC: is not operational, id: " + ric.id() + ", state: " + ric.getState(),
76                     HttpStatus.LOCKED);
77             return Mono.error(e);
78         }
79     }
80
81     public Mono<Ric> checkSupportedType(Ric ric, PolicyType type) {
82         if (!ric.isSupportingType(type.getId())) {
83             logger.debug("Request rejected, type not supported, RIC: {}", ric);
84             ServiceException e = new ServiceException(
85                     "Type: " + type.getId() + " not supported by RIC: " + ric.id(), HttpStatus.BAD_REQUEST);
86             return Mono.error(e);
87         }
88         return Mono.just(ric);
89     }
90
91     public Policy buildPolicy(PolicyObjectInformation policyObjectInformation, PolicyType policyType, Ric ric, String policyId, ServerWebExchange exchange) {
92         return Policy.builder()
93                 .id(policyId)
94                 .json(toJson(policyObjectInformation.getPolicyObject()))
95                 .type(policyType)
96                 .ric(ric)
97                 .ownerServiceId(tokenService.getServiceId(policyObjectInformation, exchange))
98                 .lastModified(Instant.now())
99                 .isTransient(policyObjectInformation.getTransient())
100                 .build();
101     }
102
103     public Boolean jsonSchemaValidation(Object jsonObject) {
104         toJson(jsonObject);
105         return true;
106     }
107
108     public String policyIdGeneration(PolicyObjectInformation policyObjectInfo) {
109         if (policyObjectInfo.getPolicyId() == null || policyObjectInfo.getPolicyId().isEmpty() ||
110                 policyObjectInfo.getPolicyId().isBlank())
111             return UUID.randomUUID().toString();
112         else
113             return policyObjectInfo.getPolicyId().trim();
114     }
115
116     public String toJson(Object jsonObject) {
117         return gson.toJson(jsonObject);
118     }
119
120     public HttpHeaders createHttpHeaders(String headerName, String headerValue) {
121         HttpHeaders httpHeaders = new HttpHeaders();
122         httpHeaders.add(headerName, headerValue);
123         return httpHeaders;
124     }
125
126     public String buildURI(String policyId, ServerWebExchange serverWebExchange) {
127         return UriComponentsBuilder.fromUri(serverWebExchange.getRequest().getURI())
128                 .path("/{id}")
129                 .buildAndExpand(policyId)
130                 .toString();
131     }
132
133     public Mono<Policy> isPolicyAlreadyCreated(Policy policy, Policies policies) {
134         if (policies.get(policy.getId()) != null) {
135             return Mono.error(new ServiceException
136                     ("Policy already created with ID: " + policy.getId(), HttpStatus.CONFLICT));
137         }
138             return Mono.just(policy);
139     }
140
141     public Flux<PolicyInformation> toFluxPolicyInformation(Collection<Policy> policyCollection) {
142         return Flux.fromIterable(policyCollection.stream()
143                 .map(singlePolicy -> new PolicyInformation(singlePolicy.getId(), singlePolicy.getRic().getConfig().getRicId()))
144                 .collect(Collectors.toList()));
145     }
146
147     public Collection<PolicyTypeInformation> toPolicyTypeInfoCollection(Collection<PolicyType> policyTypeCollection, Ric ric) {
148         return policyTypeCollection.stream()
149                 .map(type -> new PolicyTypeInformation(type.getId(), ric.getConfig().getRicId()))
150                 .collect(Collectors.toList());
151     }
152 }