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.service.v3;
23 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
24 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
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.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.stereotype.Service;
33 import org.springframework.web.server.ServerWebExchange;
34 import reactor.core.publisher.Mono;
37 public class PolicyService {
40 private Helper helper;
46 private PolicyTypes policyTypes;
49 private Policies policies;
52 private AuthorizationService authorizationService;
55 private A1ClientFactory a1ClientFactory;
58 private ErrorHandlingService errorHandlingService;
60 public Mono<ResponseEntity<PolicyObjectInformation>> createPolicyService
61 (PolicyObjectInformation policyObjectInfo, ServerWebExchange serverWebExchange) {
63 if (!helper.jsonSchemaValidation(policyObjectInfo.getPolicyObject()))
64 return Mono.error(new ServiceException("Schema validation failed", HttpStatus.BAD_REQUEST));
65 Ric ric = rics.getRic(policyObjectInfo.getNearRtRicId());
66 PolicyType policyType = policyTypes.getType(policyObjectInfo.getPolicyTypeId());
67 Policy policy = helper.buildPolicy(policyObjectInfo, policyType, ric, helper.policyIdGeneration());
68 return helper.isPolicyAlreadyCreated(policy,policies)
69 .doOnError(error -> errorHandlingService.handleError(error))
70 .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE))
71 .doOnError(error -> errorHandlingService.handleError(error))
72 .flatMap(policyNotUsed -> ric.getLock().lock(Lock.LockType.SHARED, "createPolicy"))
73 .flatMap(grant -> postPolicy(policy, grant))
74 .map(locationHeaderValue ->
75 new ResponseEntity<PolicyObjectInformation>(policyObjectInfo,helper.createHttpHeaders(
76 "location",helper.buildURI(policy.getId(), serverWebExchange)), HttpStatus.CREATED))
77 .doOnError(error -> errorHandlingService.handleError(error));
78 } catch (Exception ex) {
79 return Mono.error(ex);
84 private Mono<String> postPolicy(Policy policy, Lock.Grant grant) {
85 return helper.checkRicStateIdle(policy.getRic())
86 .doOnError(error -> errorHandlingService.handleError(error))
87 .flatMap(ric -> helper.checkSupportedType(ric, policy.getType()))
88 .doOnError(error -> errorHandlingService.handleError(error))
89 .flatMap(ric -> a1ClientFactory.createA1Client(ric))
90 .flatMap(a1Client -> a1Client.putPolicy(policy))
91 .doOnError(error -> errorHandlingService.handleError(error))
92 .doOnNext(policyString -> policies.put(policy))
93 .doFinally(releaseLock -> grant.unlockBlocking())
94 .doOnError(error -> errorHandlingService.handleError(error));