aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/main/java
diff options
context:
space:
mode:
authorraviteja.karumuri <raviteja.karumuri@est.tech>2024-03-28 18:22:42 +0000
committerraviteja.karumuri <raviteja.karumuri@est.tech>2024-05-29 17:09:25 +0100
commit87ac816229c2bf3049402d727a2c3edc0592965a (patch)
tree6eac8d105afb929ed3711a83e8c23567ad50781a /a1-policy-management/src/main/java
parent6de5c037f336ac5e7bee62e1dd2062a797a83e51 (diff)
Implementing Create Policy for V3
Issue-ID: CCSDK-4003 Change-Id: I0da2525c8ef273f23519e9bb4332f64e92e23cb8 Signed-off-by: Raviteja Karumuri <raviteja.karumuri@est.tech>
Diffstat (limited to 'a1-policy-management/src/main/java')
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java2
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java95
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/AuthorizationService.java42
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/ErrorHandlingService.java65
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java96
-rw-r--r--a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java128
6 files changed, 428 insertions, 0 deletions
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java
index c554dbd5..21da2f77 100644
--- a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v2/Consts.java
@@ -32,6 +32,8 @@ public class Consts {
public static final String V2_API_ROOT = "/a1-policy/v2";
+ public static final String V3_API_ROOT = "/a1policymanagement/v1";
+
public static final String V2_API_SERVICE_CALLBACKS_NAME = "Service callbacks";
public static final String V2_API_SERVICE_CALLBACKS_DESCRIPTION = "";
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java
new file mode 100644
index 00000000..36da71da
--- /dev/null
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/controllers/v3/PolicyControllerV3.java
@@ -0,0 +1,95 @@
+/*-
+ * ========================LICENSE_START=================================
+ * ONAP : ccsdk oran
+ * ======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ======================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.api.v3.A1PolicyManagementApi;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.Consts;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyController;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyInformation;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyTypeInformation;
+import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
+import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
+import org.onap.ccsdk.oran.a1policymanagementservice.service.v3.PolicyService;
+import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+@RestController("PolicyControllerV3")
+@Tag(//
+ name = PolicyController.API_NAME, //
+ description = PolicyController.API_DESCRIPTION //
+)
+@RequestMapping(Consts.V3_API_ROOT)
+public class PolicyControllerV3 implements A1PolicyManagementApi {
+ public static final String API_NAME = "A1 Policy Management";
+ public static final String API_DESCRIPTION = "API to create,update and get policies or policy definitions";
+ @Autowired
+ private PolicyService policyService;
+
+ @Autowired
+ private Helper helper;
+
+ private PolicyType policyType;
+
+ private Ric ric;
+ @Override
+ public Mono<ResponseEntity<PolicyObjectInformation>> createPolicy(Mono<PolicyObjectInformation> policyObjectInformation, ServerWebExchange exchange) {
+ return policyObjectInformation.flatMap(policyObjectInfo -> {
+ return policyService.createPolicyService(policyObjectInfo, exchange);
+ });
+ }
+
+ @Override
+ public Mono<ResponseEntity<Void>> deletePolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Mono<ResponseEntity<Object>> getPolicy(String policyId, String accept, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Mono<ResponseEntity<Flux<PolicyInformation>>> getPolicyIds(String policyTypeId, String nearRtRicId, String serviceId, String typeName, String accept, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Mono<ResponseEntity<Object>> getPolicyTypeDefinition(String policyTypeId, String accept, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Mono<ResponseEntity<Flux<PolicyTypeInformation>>> getPolicyTypes(String nearRtRicId, String typeName, String compatibleWithVersion, String accept, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+
+ @Override
+ public Mono<ResponseEntity<Object>> putPolicy(String policyId, Mono<Object> body, ServerWebExchange exchange) throws Exception {
+ return null;
+ }
+}
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/AuthorizationService.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/AuthorizationService.java
new file mode 100644
index 00000000..ac4320a1
--- /dev/null
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/AuthorizationService.java
@@ -0,0 +1,42 @@
+/*-
+ * ========================LICENSE_START=================================
+ * ONAP : ccsdk oran
+ * ======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ======================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
+
+
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.AuthorizationCheck;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
+import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+@Service
+public class AuthorizationService {
+
+ @Autowired
+ private AuthorizationCheck authorization;
+
+ public Mono<Policy> authCheck (ServerWebExchange serverWebExchange, Policy policy, AccessType accessType){
+ return authorization.doAccessControl(serverWebExchange.getRequest().getHeaders().toSingleValueMap(),
+ policy, AccessType.WRITE);
+ }
+}
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/ErrorHandlingService.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/ErrorHandlingService.java
new file mode 100644
index 00000000..693ae513
--- /dev/null
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/ErrorHandlingService.java
@@ -0,0 +1,65 @@
+/*-
+ * ========================LICENSE_START=================================
+ * ONAP : ccsdk oran
+ * ======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ======================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
+
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyController;
+import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.ProblemDetails;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.*;
+import org.springframework.stereotype.Service;
+import org.springframework.web.reactive.function.client.WebClientException;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
+import reactor.core.publisher.Mono;
+
+import java.lang.invoke.MethodHandles;
+import java.math.BigDecimal;
+
+@Service
+public class ErrorHandlingService {
+
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+ public Mono<ResponseEntity<ProblemDetails>> handleError(Throwable throwable) {
+ if (throwable instanceof WebClientResponseException) {
+ WebClientResponseException e = (WebClientResponseException) throwable;
+ return createErrorResponse(e.getResponseBodyAsString(), e.getStatusCode());
+ } else if (throwable instanceof WebClientException) {
+ WebClientException e = (WebClientException) throwable;
+ return createErrorResponse(e.getMessage(), HttpStatus.BAD_GATEWAY);
+ } else if (throwable instanceof ServiceException) {
+ ServiceException e = (ServiceException) throwable;
+ return createErrorResponse(e.getMessage(), e.getHttpStatus());
+ } else {
+ return createErrorResponse(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ public Mono<ResponseEntity<ProblemDetails>> createErrorResponse(String errorBody, HttpStatusCode statusCode) {
+ logger.debug("Error content: {}, with status code {}", errorBody, statusCode);
+ ProblemDetails problemDetail = new ProblemDetails().type("about:blank");
+ problemDetail.setDetail(errorBody);
+ problemDetail.setStatus(new BigDecimal(statusCode.value()));
+ HttpHeaders headers = new HttpHeaders();
+ headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON);
+ return Mono.just(new ResponseEntity<>(problemDetail, headers, statusCode));
+ }
+}
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java
new file mode 100644
index 00000000..6c994b42
--- /dev/null
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/service/v3/PolicyService.java
@@ -0,0 +1,96 @@
+/*-
+ * ========================LICENSE_START=================================
+ * ONAP : ccsdk oran
+ * ======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ======================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
+
+import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
+import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
+import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
+import org.onap.ccsdk.oran.a1policymanagementservice.repository.*;
+import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+@Service
+public class PolicyService {
+
+ @Autowired
+ private Helper helper;
+
+ @Autowired
+ private Rics rics;
+
+ @Autowired
+ private PolicyTypes policyTypes;
+
+ @Autowired
+ private Policies policies;
+
+ @Autowired
+ private AuthorizationService authorizationService;
+
+ @Autowired
+ private A1ClientFactory a1ClientFactory;
+
+ @Autowired
+ private ErrorHandlingService errorHandlingService;
+
+ public Mono<ResponseEntity<PolicyObjectInformation>> createPolicyService
+ (PolicyObjectInformation policyObjectInfo, ServerWebExchange serverWebExchange) {
+ try {
+ if (!helper.jsonSchemaValidation(policyObjectInfo.getPolicyObject()))
+ return Mono.error(new ServiceException("Schema validation failed", HttpStatus.BAD_REQUEST));
+ Ric ric = rics.getRic(policyObjectInfo.getNearRtRicId());
+ PolicyType policyType = policyTypes.getType(policyObjectInfo.getPolicyTypeId());
+ Policy policy = helper.buildPolicy(policyObjectInfo, policyType, ric, helper.policyIdGeneration());
+ return helper.isPolicyAlreadyCreated(policy,policies)
+ .doOnError(error -> errorHandlingService.handleError(error))
+ .flatMap(policyBuilt -> authorizationService.authCheck(serverWebExchange, policy, AccessType.WRITE))
+ .doOnError(error -> errorHandlingService.handleError(error))
+ .flatMap(policyNotUsed -> ric.getLock().lock(Lock.LockType.SHARED, "createPolicy"))
+ .flatMap(grant -> postPolicy(policy, grant))
+ .map(locationHeaderValue ->
+ new ResponseEntity<PolicyObjectInformation>(policyObjectInfo,helper.createHttpHeaders(
+ "location",helper.buildURI(policy.getId(), serverWebExchange)), HttpStatus.CREATED))
+ .doOnError(error -> errorHandlingService.handleError(error));
+ } catch (Exception ex) {
+ return Mono.error(ex);
+ }
+
+ }
+
+ private Mono<String> postPolicy(Policy policy, Lock.Grant grant) {
+ return helper.checkRicStateIdle(policy.getRic())
+ .doOnError(error -> errorHandlingService.handleError(error))
+ .flatMap(ric -> helper.checkSupportedType(ric, policy.getType()))
+ .doOnError(error -> errorHandlingService.handleError(error))
+ .flatMap(ric -> a1ClientFactory.createA1Client(ric))
+ .flatMap(a1Client -> a1Client.putPolicy(policy))
+ .doOnError(error -> errorHandlingService.handleError(error))
+ .doOnNext(policyString -> policies.put(policy))
+ .doFinally(releaseLock -> grant.unlockBlocking())
+ .doOnError(error -> errorHandlingService.handleError(error));
+ }
+}
diff --git a/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java
new file mode 100644
index 00000000..11edbfd9
--- /dev/null
+++ b/a1-policy-management/src/main/java/org/onap/ccsdk/oran/a1policymanagementservice/util/v3/Helper.java
@@ -0,0 +1,128 @@
+/*-
+ * ========================LICENSE_START=================================
+ * ONAP : ccsdk oran
+ * ======================================================================
+ * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
+ * ======================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================LICENSE_END===================================
+ */
+
+package org.onap.ccsdk.oran.a1policymanagementservice.util.v3;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
+import org.onap.ccsdk.oran.a1policymanagementservice.repository.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Component;
+import org.springframework.web.server.ServerWebExchange;
+import org.springframework.web.util.UriComponentsBuilder;
+import reactor.core.publisher.Mono;
+
+import java.lang.invoke.MethodHandles;
+import java.time.Instant;
+import java.util.UUID;
+
+@Component
+public class Helper {
+
+ @Autowired
+ private Services services;
+
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+ private static Gson gson = new GsonBuilder().create();
+ public void keepServiceAlive(String name) {
+ Service s = this.services.get(name);
+ if (s != null) {
+ s.keepAlive();
+ }
+ }
+
+ public Mono<Ric> checkRicStateIdle(Ric ric) {
+ if (ric.getState() == Ric.RicState.AVAILABLE) {
+ return Mono.just(ric);
+ } else {
+ logger.debug("Request rejected Near-RT RIC not IDLE, ric: {}", ric);
+ ServiceException e = new ServiceException(
+ "Near-RT RIC: is not operational, id: " + ric.id() + ", state: " + ric.getState(),
+ HttpStatus.LOCKED);
+ return Mono.error(e);
+ }
+ }
+
+ public Mono<Ric> checkSupportedType(Ric ric, PolicyType type) {
+ if (!ric.isSupportingType(type.getId())) {
+ logger.debug("Request rejected, type not supported, RIC: {}", ric);
+ ServiceException e = new ServiceException(
+ "Type: " + type.getId() + " not supported by RIC: " + ric.id(), HttpStatus.BAD_REQUEST);
+ return Mono.error(e);
+ }
+ return Mono.just(ric);
+ }
+
+ public Policy buildPolicy(PolicyObjectInformation policyObjectInformation, PolicyType policyType, Ric ric, String policyId) {
+ return Policy.builder()
+ .id(policyId)
+ .json(toJson(policyObjectInformation.getPolicyObject()))
+ .type(policyType)
+ .ric(ric)
+ .ownerServiceId(policyObjectInformation.getServiceId() == null ? ""
+ : policyObjectInformation.getServiceId())
+ .lastModified(Instant.now())
+ .isTransient(policyObjectInformation.getTransient())
+ .statusNotificationUri(policyObjectInformation.getStatusNotificationUri() == null ? ""
+ : policyObjectInformation.getStatusNotificationUri())
+ .build();
+ }
+
+ public Boolean jsonSchemaValidation(Object jsonObject) {
+ String jsonString = toJson(jsonObject);
+ return true;
+ }
+
+ public String policyIdGeneration() {
+ return UUID.randomUUID().toString();
+ }
+
+ public String toJson(Object jsonObject) {
+ return gson.toJson(jsonObject);
+ }
+
+ public HttpHeaders createHttpHeaders(String headerName, String headerValue) {
+ HttpHeaders httpHeaders = new HttpHeaders();
+ httpHeaders.add(headerName, headerValue);
+ return httpHeaders;
+ }
+
+ public String buildURI(String policyId, ServerWebExchange serverWebExchange) {
+ return UriComponentsBuilder.fromHttpRequest(serverWebExchange.getRequest())
+ .path("/{id}")
+ .buildAndExpand(policyId)
+ .toString();
+ }
+
+ public Mono<Policy> isPolicyAlreadyCreated(Policy policy, Policies policies) {
+ if (policies.get(policy.getId()) != null) {
+ return Mono.error(new ServiceException
+ ("Policy already created with ID: " + policy.getId(), HttpStatus.CONFLICT));
+ }
+ return Mono.just(policy);
+ }
+}