* ========================LICENSE_START=================================
* O-RAN-SC
* %%
- * Copyright (C) 2019 Nordix Foundation
+ * Copyright (C) 2020 Nordix Foundation
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.oransc.policyagent.clients;
import java.lang.invoke.MethodHandles;
-import java.util.ArrayList;
import java.util.List;
-import org.json.JSONArray;
-import org.json.JSONException;
import org.json.JSONObject;
import org.oransc.policyagent.configuration.RicConfig;
import org.oransc.policyagent.repository.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UriComponentsBuilder;
+
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class OscA1Client implements A1Client {
private static final String URL_PREFIX = "/a1-p";
- private static final String POLICYTYPES = "/policytypes";
+ private static final String POLICY_TYPES = "/policytypes";
private static final String CREATE_SCHEMA = "create_schema";
private static final String TITLE = "title";
@Override
public Mono<List<String>> getPolicyTypeIdentities() {
- return restClient.get(POLICYTYPES) //
- .flatMap(this::parseJsonArrayOfString);
+ return getPolicyTypeIds() //
+ .collectList();
}
@Override
public Mono<List<String>> getPolicyIdentities() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(type -> getPolicyIdentitiesById(type)) //
- .flatMap(policyIds -> Flux.fromIterable(policyIds)) //
+ return getPolicyTypeIds() //
+ .flatMap(this::getPolicyIdentitiesByType) //
.collectList();
}
@Override
public Mono<A1ProtocolType> getProtocolVersion() {
return restClient.get(HEALTHCHECK) //
- .flatMap(resp -> Mono.just(A1ProtocolType.OSC_V1));
+ .flatMap(notUsed -> Mono.just(A1ProtocolType.OSC_V1));
}
@Override
public Flux<String> deleteAllPolicies() {
- return getPolicyTypeIdentities() //
- .flatMapMany(types -> Flux.fromIterable(types)) //
- .flatMap(typeId -> deletePoliciesForType(typeId)); //
+ return getPolicyTypeIds() //
+ .flatMap(this::deletePoliciesForType);
}
@Override
}
- private Mono<List<String>> getPolicyIdentitiesById(String typeId) {
- String uri = POLICY_IDS_URI.buildAndExpand(typeId).toUriString();
- return restClient.get(uri) //
- .flatMap(this::parseJsonArrayOfString);
+ private Flux<String> getPolicyTypeIds() {
+ return restClient.get(POLICY_TYPES) //
+ .flatMapMany(JsonHelper::parseJsonArrayOfString);
+ }
+
+ private Flux<String> getPolicyIdentitiesByType(String typeId) {
+ return restClient.get(POLICY_IDS_URI.buildAndExpand(typeId).toUriString()) //
+ .flatMapMany(JsonHelper::parseJsonArrayOfString);
}
private Mono<String> getCreateSchema(String policyTypeResponse, String policyTypeId) {
schemaObj.put(TITLE, policyTypeId);
return Mono.just(schemaObj.toString());
} catch (Exception e) {
- logger.error("Unexcpected response for policy type: {}", policyTypeResponse, e);
+ logger.error("Unexpected response for policy type: {}, exception: {}", policyTypeResponse, e.toString());
return Mono.error(e);
}
}
}
private Flux<String> deletePoliciesForType(String typeId) {
- return getPolicyIdentitiesById(typeId) //
- .flatMapMany(policyIds -> Flux.fromIterable(policyIds)) //
- .flatMap(policyId -> deletePolicyById(typeId, policyId)); //
- }
-
- private Mono<List<String>> parseJsonArrayOfString(String inputString) {
- try {
- List<String> arrayList = new ArrayList<>();
- JSONArray jsonArray = new JSONArray(inputString);
- for (int i = 0; i < jsonArray.length(); i++) {
- arrayList.add(jsonArray.getString(i));
- }
- logger.debug("A1 client: received list = {}", arrayList);
- return Mono.just(arrayList);
- } catch (JSONException ex) { // invalid json
- return Mono.error(ex);
- }
+ return getPolicyIdentitiesByType(typeId) //
+ .flatMap(policyId -> deletePolicyById(typeId, policyId));
}
}