2  * ========================LICENSE_START=================================
 
   4  * ======================================================================
 
   5  * Copyright (C) 2020-2023 Nordix Foundation. 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.controllers.v2;
 
  23 import com.fasterxml.jackson.core.JsonProcessingException;
 
  24 import com.fasterxml.jackson.databind.ObjectMapper;
 
  25 import com.google.gson.Gson;
 
  26 import com.google.gson.GsonBuilder;
 
  28 import java.lang.invoke.MethodHandles;
 
  29 import java.time.Instant;
 
  30 import java.util.HashMap;
 
  32 import java.util.concurrent.atomic.AtomicInteger;
 
  34 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
 
  35 import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfo;
 
  36 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
 
  37 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
 
  38 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
 
  39 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
 
  40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
 
  41 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
 
  42 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
 
  43 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
 
  44 import org.slf4j.Logger;
 
  45 import org.slf4j.LoggerFactory;
 
  46 import org.springframework.beans.factory.annotation.Autowired;
 
  47 import org.springframework.http.ResponseEntity;
 
  50  * Invoke operations over the NBI and start synchronizations in a separate
 
  51  * thread. For test of robustness using concurrent clients.
 
  53 class ConcurrencyTestRunnable implements Runnable {
 
  54     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
  55     private final AsyncRestClient webClient;
 
  56     static AtomicInteger nextCount = new AtomicInteger(0);
 
  57     private final int count;
 
  58     private final RicSupervision supervision;
 
  59     private final MockA1ClientFactory a1ClientFactory;
 
  60     private final Rics rics;
 
  61     private final PolicyTypes types;
 
  62     private boolean failed = false;
 
  64     private ObjectMapper objectMapper = new ObjectMapper();
 
  66     private static Gson gson = new GsonBuilder().create();
 
  68     ConcurrencyTestRunnable(AsyncRestClient client, RicSupervision supervision, MockA1ClientFactory a1ClientFactory,
 
  69             Rics rics, PolicyTypes types) {
 
  70         this.count = nextCount.incrementAndGet();
 
  71         this.supervision = supervision;
 
  72         this.a1ClientFactory = a1ClientFactory;
 
  75         this.webClient = client;
 
  78     private void printStatusInfo() {
 
  80             String url = "/actuator/metrics/jvm.threads.live";
 
  81             ResponseEntity<String> result = webClient.getForEntity(url).block();
 
  82             System.out.println(Thread.currentThread() + result.getBody());
 
  85             result = webClient.getForEntity(url).block();
 
  86             System.out.println(Thread.currentThread() + result.getBody());
 
  88         } catch (Exception e) {
 
  89             logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
 
  96             for (int i = 0; i < 500; ++i) {
 
  98                     createInconsistency();
 
  99                     this.supervision.checkAllRics();
 
 101                 String name = "policy_" + count + "_" + i;
 
 103                 putPolicy(name + "-");
 
 107                 deletePolicy(name + "-");
 
 109         } catch (Exception e) {
 
 110             logger.error("Concurrency test exception " + e.toString());
 
 116     public boolean isFailed() {
 
 120     private Policy createPolicyObject(String id) {
 
 121         Ric ric = this.rics.get("ric");
 
 122         PolicyType type = this.types.get("type1");
 
 123         return Policy.builder() //
 
 128                 .ownerServiceId("") //
 
 129                 .lastModified(Instant.now()) //
 
 130                 .isTransient(false) //
 
 131                 .statusNotificationUri("/policy_status?id=XXX") //
 
 135     private void createInconsistency() {
 
 136         MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
 
 137         Policy policy = createPolicyObject("junk");
 
 138         client.putPolicy(policy).block();
 
 141     private void listPolicies() {
 
 142         String uri = "/policy-instances";
 
 143         webClient.getForEntity(uri).block();
 
 146     private void listTypes() {
 
 147         String uri = "/policy-types";
 
 148         webClient.getForEntity(uri).block();
 
 151     private void putPolicy(String name) throws JsonProcessingException {
 
 152         String putUrl = "/policies";
 
 153         String body = putPolicyBody("service1", "ric", "type1", name, false);
 
 154         webClient.putForEntity(putUrl, body).block();
 
 157     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
 
 158             boolean isTransient) throws JsonProcessingException {
 
 160         PolicyInfo policyInfo = new PolicyInfo();
 
 161         policyInfo.setPolicyId(policyInstanceId);
 
 162         policyInfo.setPolicytypeId(policyTypeName);
 
 163         policyInfo.setRicId(ricId);
 
 164         policyInfo.setServiceId(serviceName);
 
 165         policyInfo.setPolicyData(policyData());
 
 166         policyInfo.setStatusNotificationUri("/status");
 
 167         policyInfo.setTransient(isTransient);
 
 168         return objectMapper.writeValueAsString(policyInfo);
 
 171     private Map<String,String> policyData() {
 
 172         Map<String,String> policyDataInMap = new HashMap<>();
 
 173         policyDataInMap.put("servingCellNrcgi","1");
 
 174         return policyDataInMap;
 
 177     private void deletePolicy(String name) {
 
 178         String deleteUrl = "/policies/" + name;
 
 179         webClient.delete(deleteUrl).block();