2 * ========================LICENSE_START=================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.util.v3;
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.PolicyObjectInformation;
27 import org.onap.ccsdk.oran.a1policymanagementservice.repository.*;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.HttpStatus;
33 import org.springframework.stereotype.Component;
34 import org.springframework.web.server.ServerWebExchange;
35 import org.springframework.web.util.UriComponentsBuilder;
36 import reactor.core.publisher.Mono;
38 import java.lang.invoke.MethodHandles;
39 import java.time.Instant;
40 import java.util.UUID;
46 private Services services;
48 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50 private static Gson gson = new GsonBuilder().create();
51 public void keepServiceAlive(String name) {
52 Service s = this.services.get(name);
58 public Mono<Ric> checkRicStateIdle(Ric ric) {
59 if (ric.getState() == Ric.RicState.AVAILABLE) {
60 return Mono.just(ric);
62 logger.debug("Request rejected Near-RT RIC not IDLE, ric: {}", ric);
63 ServiceException e = new ServiceException(
64 "Near-RT RIC: is not operational, id: " + ric.id() + ", state: " + ric.getState(),
70 public Mono<Ric> checkSupportedType(Ric ric, PolicyType type) {
71 if (!ric.isSupportingType(type.getId())) {
72 logger.debug("Request rejected, type not supported, RIC: {}", ric);
73 ServiceException e = new ServiceException(
74 "Type: " + type.getId() + " not supported by RIC: " + ric.id(), HttpStatus.BAD_REQUEST);
77 return Mono.just(ric);
80 public Policy buildPolicy(PolicyObjectInformation policyObjectInformation, PolicyType policyType, Ric ric, String policyId) {
81 return Policy.builder()
83 .json(toJson(policyObjectInformation.getPolicyObject()))
86 .ownerServiceId(policyObjectInformation.getServiceId() == null ? ""
87 : policyObjectInformation.getServiceId())
88 .lastModified(Instant.now())
89 .isTransient(policyObjectInformation.getTransient())
90 .statusNotificationUri(policyObjectInformation.getStatusNotificationUri() == null ? ""
91 : policyObjectInformation.getStatusNotificationUri())
95 public Boolean jsonSchemaValidation(Object jsonObject) {
96 String jsonString = toJson(jsonObject);
100 public String policyIdGeneration() {
101 return UUID.randomUUID().toString();
104 public String toJson(Object jsonObject) {
105 return gson.toJson(jsonObject);
108 public HttpHeaders createHttpHeaders(String headerName, String headerValue) {
109 HttpHeaders httpHeaders = new HttpHeaders();
110 httpHeaders.add(headerName, headerValue);
114 public String buildURI(String policyId, ServerWebExchange serverWebExchange) {
115 return UriComponentsBuilder.fromHttpRequest(serverWebExchange.getRequest())
117 .buildAndExpand(policyId)
121 public Mono<Policy> isPolicyAlreadyCreated(Policy policy, Policies policies) {
122 if (policies.get(policy.getId()) != null) {
123 return Mono.error(new ServiceException
124 ("Policy already created with ID: " + policy.getId(), HttpStatus.CONFLICT));
126 return Mono.just(policy);