aboutsummaryrefslogtreecommitdiffstats
path: root/a1-policy-management/src/test/java/org/onap/ccsdk/oran/a1policymanagementservice/ConcurrencyTestRunnable.java
blob: 0e8803a51992d1e1d36374a455371cfdd3ac4f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*-
 * ========================LICENSE_START=================================
 * ONAP : ccsdk oran
 * ======================================================================
 * Copyright (C) 2020 Nordix Foundation. 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;

import java.time.Instant;
import java.util.concurrent.atomic.AtomicInteger;

import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;

/**
 * Invoke operations over the NBI and start synchronizations in a separate
 * thread. For test of robustness using concurrent clients.
 */
class ConcurrencyTestRunnable implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(ConcurrencyTestRunnable.class);
    private final AsyncRestClient webClient;
    static AtomicInteger nextCount = new AtomicInteger(0);
    private final int count;
    private final RicSupervision supervision;
    private final MockA1ClientFactory a1ClientFactory;
    private final Rics rics;
    private final PolicyTypes types;
    private boolean failed = false;

    ConcurrencyTestRunnable(AsyncRestClient client, RicSupervision supervision, MockA1ClientFactory a1ClientFactory,
        Rics rics, PolicyTypes types) {
        this.count = nextCount.incrementAndGet();
        this.supervision = supervision;
        this.a1ClientFactory = a1ClientFactory;
        this.rics = rics;
        this.types = types;
        this.webClient = client;
    }

    private void printStatusInfo() {
        try {
            String url = "/actuator/metrics/jvm.threads.live";
            ResponseEntity<String> result = webClient.getForEntity(url).block();
            System.out.println(Thread.currentThread() + result.getBody());

            url = "/rics";
            result = webClient.getForEntity(url).block();
            System.out.println(Thread.currentThread() + result.getBody());

        } catch (Exception e) {
            logger.error(Thread.currentThread() + "Concurrency test printStatusInfo exception " + e.toString());
        }
    }

    @Override
    public void run() {
        try {
            for (int i = 0; i < 500; ++i) {
                if (i % 100 == 0) {
                    createInconsistency();
                    this.supervision.checkAllRics();
                }
                String name = "policy:" + count + ":" + i;
                putPolicy(name);
                putPolicy(name + "-");
                listPolicies();
                listTypes();
                deletePolicy(name);
                deletePolicy(name + "-");
            }
        } catch (Exception e) {
            logger.error("Concurrency test exception " + e.toString());
            printStatusInfo();
            failed = true;
        }
    }

    public boolean isFailed() {
        return this.failed;
    }

    private Policy createPolicyObject(String id) {
        Ric ric = this.rics.get("ric");
        PolicyType type = this.types.get("type1");
        return ImmutablePolicy.builder() //
            .id(id) //
            .json("{}") //
            .type(type) //
            .ric(ric) //
            .ownerServiceId("") //
            .lastModified(Instant.now()) //
            .isTransient(false) //
            .build();
    }

    private void createInconsistency() {
        MockA1Client client = a1ClientFactory.getOrCreateA1Client("ric");
        Policy policy = createPolicyObject("junk");
        client.putPolicy(policy).block();

    }

    private void listPolicies() {
        String uri = "/policies";
        webClient.getForEntity(uri).block();
    }

    private void listTypes() {
        String uri = "/policy-types";
        webClient.getForEntity(uri).block();
    }

    private void putPolicy(String name) {
        String putUrl = "/policy?policytype_id=type1&policy_id=" + name + "&ric_id=ric&service_id=service1";
        webClient.putForEntity(putUrl, "{}").block();
    }

    private void deletePolicy(String name) {
        String deleteUrl = "/policy?policy_id=" + name;
        webClient.delete(deleteUrl).block();
    }
}