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