aboutsummaryrefslogtreecommitdiffstats
path: root/examples/examples-onap-vcpe/src/test/java/org/onap/policy/apex/domains/onap/vcpe/OnapVCpeSimEndpoint.java
blob: 3e7962823f53445d795856370ac025199079c629 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2020,2022-2023 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.domains.onap.vcpe;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.time.Instant;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.common.gson.InstantAsMillisTypeAdapter;
import org.onap.policy.common.utils.resources.TextFileUtils;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class AaiAndGuardSimEndpoint.
 */
@Path("/sim")
public class OnapVCpeSimEndpoint {
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(OnapVCpeSimEndpoint.class);

    private static BlockingQueue<String> appcResponseQueue = new LinkedBlockingQueue<>();

    private static AtomicInteger guardMessagesReceived = new AtomicInteger();
    private static AtomicInteger postMessagesReceived = new AtomicInteger();
    private static AtomicInteger putMessagesReceived = new AtomicInteger();
    private static AtomicInteger statMessagesReceived = new AtomicInteger();
    private static AtomicInteger getMessagesReceived = new AtomicInteger();

    private static final Random randomDelayInc = new Random();

    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(Instant.class, new InstantAsMillisTypeAdapter()).setPrettyPrinting().create();

    private static final AtomicInteger nextVnfId = new AtomicInteger(0);
    private static Boolean nextControlLoopMessageIsOnset = true;

    /**
     * Service get stats.
     *
     * @return the response
     */
    @Path("/pdp/api/Stats")
    @GET
    public Response serviceGetStats() {
        statMessagesReceived.incrementAndGet();
        String returnString = "{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": "
                + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}";

        return Response.status(200).entity(prettifyJsonString(returnString)).build();
    }

    /**
     * Service guard post request.
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/pdp/api/getDecision")
    @POST
    public Response serviceGuardPostRequest(final String jsonString) {
        LOGGER.info("\n*** GUARD REQUEST START ***\n" + jsonString + "\n *** GUARD REQUEST END ***");

        String target = jsonString.substring(jsonString.indexOf("00000000"));
        target = target.substring(0, target.indexOf('"'));

        int thisGuardMessageNumber = guardMessagesReceived.incrementAndGet();
        postMessagesReceived.incrementAndGet();

        String responseJsonString = null;
        if (thisGuardMessageNumber % 2 == 0) {
            responseJsonString = "{\"decision\": \"PERMIT\", \"details\": \"Decision Permit. OK!\"}";
        } else {
            responseJsonString = "{\"decision\": \"DENY\", \"details\": \"Decision Denied. NOK :-(\"}";
        }

        responseJsonString = prettifyJsonString(responseJsonString);

        LOGGER.info("\n*** GUARD RESPONSE START ***\n" + target + "\n" + responseJsonString
                + "\n*** GUARD RESPONSE END ***");

        return Response.status(200).entity(responseJsonString).build();
    }

    /**
     * AAI named query search request.
     * http://localhost:54321/aai/v16/search/nodes-query?search-node-type=vserver&filter=vserver-name:EQUALS:
     *
     * @param searchNodeType the node type to search for
     * @param filter the filter to apply in the search
     * @return the response
     * @throws IOException on I/O errors
     */
    @Path("aai/v16/search/nodes-query")
    @GET
    public Response aaiNamedQuerySearchRequest(@QueryParam("search-node-type") final String searchNodeType,
            @QueryParam("filter") final String filter) throws IOException {
        getMessagesReceived.incrementAndGet();

        LOGGER.info("\n*** AAI NODE QUERY GET START ***\nsearchNodeType=" + searchNodeType + "\nfilter=" + filter
                + "\n *** AAI REQUEST END ***");

        String adjustedVserverUuid =
                "b4fe00ac-1da6-4b00-ac0d-8e8300db" + String.format("%04d", nextVnfId.getAndIncrement());

        String responseJsonString =
                TextFileUtils.getTextFileAsString("src/test/resources/aai/SearchNodeTypeResponse.json")
                        .replaceAll("b4fe00ac-1da6-4b00-ac0d-8e8300db0007", adjustedVserverUuid);

        responseJsonString = prettifyJsonString(responseJsonString);

        LOGGER.info("\n*** AAI RESPONSE START ***\n" + responseJsonString + "\n *** AAI RESPONSE END ***");

        return Response.status(200).entity(responseJsonString).build();
    }

    /**
     * AAI named query request on a particular resource.
     * http://localhost:54321/OnapVCpeSim/sim/aai/v16/query?format=resource
     *
     * @param format the format of the request
     * @param jsonString the body of the request
     * @return the response
     * @throws IOException on I/O errors
     */
    @Path("aai/v16/query")
    @PUT
    public Response aaiNamedQueryResourceRequest(@QueryParam("format") final String format, final String jsonString)
            throws IOException {
        putMessagesReceived.incrementAndGet();

        LOGGER.info("\n*** AAI NODE RESOURE POST QUERY START ***\\nformat=" + format + "\njson=" + jsonString
                + "\n *** AAI REQUEST END ***");

        int beginIndex =
                jsonString.indexOf("b4fe00ac-1da6-4b00-ac0d-8e8300db") + "b4fe00ac-1da6-4b00-ac0d-8e8300db".length();
        String nextVnfIdUrlEnding = jsonString.substring(beginIndex, beginIndex + 4);
        String responseJsonString = TextFileUtils.getTextFileAsString("src/test/resources/aai/NodeQueryResponse.json")
                .replaceAll("bbb3cefd-01c8-413c-9bdd-2b92f9ca3d38",
                        "00000000-0000-0000-0000-00000000" + nextVnfIdUrlEnding);

        responseJsonString = prettifyJsonString(responseJsonString);

        LOGGER.info("\n*** AAI RESPONSE START ***\n" + responseJsonString + "\n *** AAI RESPONSE END ***");

        return Response.status(200).entity(responseJsonString).build();
    }

    /**
     * DCAE input of events (simulation of DMaaP).
     *
     * @param timeout the timeout to wait for
     * @return the response
     * @throws IOException on I/O errors
     */
    @Path("events/unauthenticated.DCAE_CL_OUTPUT/APEX/1")
    @GET
    public Response dcaeClOutput(@QueryParam("timeout") final int timeout) throws IOException {
        getMessagesReceived.incrementAndGet();

        ThreadUtilities.sleep(timeout - 500);

        if (nextControlLoopMessageIsOnset) {
            nextControlLoopMessageIsOnset = false;

            String clOnsetEvent = TextFileUtils
                    .getTextFileAsString("src/main/resources/examples/events/ONAPvCPEStandalone/CLOnsetEvent.json");
            LOGGER.info("\n*** CONTROL LOOP ONSET START ***\n" + clOnsetEvent + "\n *** CONTROL LOOP ONSET END ***");

            return Response.status(200).entity(clOnsetEvent).build();
        } else {
            nextControlLoopMessageIsOnset = true;

            String clAbatedEvent = TextFileUtils
                    .getTextFileAsString("src/main/resources/examples/events/ONAPvCPEStandalone/CLAbatedEvent.json");
            LOGGER.info("\n*** CONTROL LOOP ABATED START ***\n" + clAbatedEvent + "\n *** CONTROL LOOP ABATED END ***");

            return Response.status(200).entity(clAbatedEvent).build();
        }
    }

    /**
     * APPC response events (simulation of DMaaP).
     *
     * @param timeout the timeout to wait for
     * @return the response
     * @throws InterruptedException on queue interrupts
     */
    @Path("events/APPC_LCM_WRITE/APEX/1")
    @GET
    public Response appcResponseOutput(@QueryParam("timeout") final int timeout) throws InterruptedException {
        getMessagesReceived.incrementAndGet();

        int timeLeft = timeout - 500;

        do {
            String appcResponse = appcResponseQueue.poll(100, TimeUnit.MILLISECONDS);

            if (appcResponse != null) {
                LOGGER.info("\n*** CONTROLLER RESPONSE START ***");
                LOGGER.info("\n*** CONTROLLER RESPONSE END ***");

                return Response.status(200).entity(appcResponse).build();
            }
            timeLeft -= 100;
        } while (timeLeft > 0);

        return Response.status(200).build();
    }

    /**
     * Post to Policy management log (Simulation of DMaaP).
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/events/POLICY_CL_MGT")
    @POST
    public Response policyLogRequest(final String jsonString) {
        postMessagesReceived.incrementAndGet();

        String logJsonString = prettifyJsonString(jsonString);

        LOGGER.info("\n*** POLICY LOG ENTRY START ***\n" + logJsonString + "\n *** POLICY LOG ENTRY END ***");

        return Response.status(200).build();
    }

    /**
     * Post to APPC LCM (Simulation of DMaaP).
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/events/APPC-LCM-READ")
    @POST
    public Response appcRequest(final String jsonString) {
        postMessagesReceived.incrementAndGet();

        String appcJsonString = prettifyJsonString(jsonString);

        LOGGER.info("\n*** CONTROLLER REQUEST START ***\n" + appcJsonString + "\n *** CONTROLLER REQUEST END ***");

        new AppcResponseCreator(appcResponseQueue, appcJsonString, 10000 + randomDelayInc.nextInt(10000));

        return Response.status(200).build();
    }

    /**
     * Post to BLACK WHITE LIST READ (Simulation of DMaaP).
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/events/BLACK-WHITE-LIST-READ")
    @POST
    public Response blackWhiteListRead(final String jsonString) {
        postMessagesReceived.incrementAndGet();

        String bwJsonString = prettifyJsonString(jsonString);

        LOGGER.info("\n*** BLACK WHITE LIST START ***\n" + bwJsonString + "\n *** BLACK WHITE LIST END ***");

        return Response.status(200).build();
    }

    /**
     * Service get event.
     *
     * @return the response
     */
    @Path("/event/GetEvent")
    @GET
    public Response serviceGetEvent() {
        final Random rand = new Random();
        final int nextMatchCase = rand.nextInt(4);
        final String nextEventName = "Event0" + rand.nextInt(2) + "00";

        final String eventString = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.sample.events\",\n" + "\"name\": \""
                + nextEventName + "\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + getMessagesReceived
                + "\",\n" + "\"target\": \"apex\",\n" + "\"TestSlogan\": \"Test slogan for External Event0\",\n"
                + "\"TestMatchCase\": " + nextMatchCase + ",\n" + "\"TestTimestamp\": " + System.currentTimeMillis()
                + ",\n" + "\"TestTemperature\": 9080.866\n" + "}";

        getMessagesReceived.incrementAndGet();

        return Response.status(200).entity(eventString).build();
    }

    /**
     * Service get empty event.
     *
     * @return the response
     */
    @Path("/event/GetEmptyEvent")
    @GET
    public Response serviceGetEmptyEvent() {
        return Response.status(200).build();
    }

    /**
     * Service get event bad response.
     *
     * @return the response
     */
    @Path("/event/GetEventBadResponse")
    @GET
    public Response serviceGetEventBadResponse() {
        return Response.status(400).build();
    }

    /**
     * Service post request.
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/event/PostEvent")
    @POST
    public Response servicePostRequest(final String jsonString) {
        postMessagesReceived.incrementAndGet();

        @SuppressWarnings("unchecked")
        final Map<String, Object> jsonMap = gson.fromJson(jsonString, Map.class);
        assertTrue(jsonMap.containsKey("name"));
        assertEquals("0.0.1", jsonMap.get("version"));
        assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
        assertEquals("Act", jsonMap.get("source"));
        assertEquals("Outside", jsonMap.get("target"));

        return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
                + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
    }

    /**
     * Service post request bad response.
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/event/PostEventBadResponse")
    @POST
    public Response servicePostRequestBadResponse(final String jsonString) {
        return Response.status(400).build();
    }

    /**
     * Service put request.
     *
     * @param jsonString the json string
     * @return the response
     */
    @Path("/event/PutEvent")
    @PUT
    public Response servicePutRequest(final String jsonString) {
        putMessagesReceived.incrementAndGet();

        @SuppressWarnings("unchecked")
        final Map<String, Object> jsonMap = gson.fromJson(jsonString, Map.class);
        assertTrue(jsonMap.containsKey("name"));
        assertEquals("0.0.1", jsonMap.get("version"));
        assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace"));
        assertEquals("Act", jsonMap.get("source"));
        assertEquals("Outside", jsonMap.get("target"));

        return Response.status(200).entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
                + ",\"POST\": , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + "}").build();
    }

    private static final String prettifyJsonString(final String uglyJsonString) {
        JsonElement je = JsonParser.parseString(uglyJsonString);
        return gson.toJson(je);
    }
}