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