11edbfd93cd95eb4dd53670528bcacd515009a6c
[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.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;
37
38 import java.lang.invoke.MethodHandles;
39 import java.time.Instant;
40 import java.util.UUID;
41
42 @Component
43 public class Helper {
44
45     @Autowired
46     private Services services;
47
48     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49
50     private static Gson gson = new GsonBuilder().create();
51     public void keepServiceAlive(String name) {
52         Service s = this.services.get(name);
53         if (s != null) {
54             s.keepAlive();
55         }
56     }
57
58     public Mono<Ric> checkRicStateIdle(Ric ric) {
59         if (ric.getState() == Ric.RicState.AVAILABLE) {
60             return Mono.just(ric);
61         } else {
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(),
65                     HttpStatus.LOCKED);
66             return Mono.error(e);
67         }
68     }
69
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);
75             return Mono.error(e);
76         }
77         return Mono.just(ric);
78     }
79
80     public Policy buildPolicy(PolicyObjectInformation policyObjectInformation, PolicyType policyType, Ric ric, String policyId) {
81         return Policy.builder()
82                 .id(policyId)
83                 .json(toJson(policyObjectInformation.getPolicyObject()))
84                 .type(policyType)
85                 .ric(ric)
86                 .ownerServiceId(policyObjectInformation.getServiceId() == null ? ""
87                         : policyObjectInformation.getServiceId())
88                 .lastModified(Instant.now())
89                 .isTransient(policyObjectInformation.getTransient())
90                 .statusNotificationUri(policyObjectInformation.getStatusNotificationUri() == null ? ""
91                         : policyObjectInformation.getStatusNotificationUri())
92                 .build();
93     }
94
95     public Boolean jsonSchemaValidation(Object jsonObject) {
96         String jsonString = toJson(jsonObject);
97         return true;
98     }
99
100     public String policyIdGeneration() {
101         return UUID.randomUUID().toString();
102     }
103
104     public String toJson(Object jsonObject) {
105         return gson.toJson(jsonObject);
106     }
107
108     public HttpHeaders createHttpHeaders(String headerName, String headerValue) {
109         HttpHeaders httpHeaders = new HttpHeaders();
110         httpHeaders.add(headerName, headerValue);
111         return httpHeaders;
112     }
113
114     public String buildURI(String policyId, ServerWebExchange serverWebExchange) {
115         return UriComponentsBuilder.fromHttpRequest(serverWebExchange.getRequest())
116                 .path("/{id}")
117                 .buildAndExpand(policyId)
118                 .toString();
119     }
120
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));
125         }
126             return Mono.just(policy);
127     }
128 }