aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/SoSimulatorJaxRs.java
blob: 9575419933d12ccf221e07ad61af80d4d86d7455 (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
/*-
 * ============LICENSE_START=======================================================
 * simulators
 * ================================================================================
 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * Modifications Copyright (C) 2020 Wipro Limited.
 * Modifications Copyright (C) 2022 CTC, Inc. and others.
 * ================================================================================
 * 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.policy.simulators;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import lombok.Setter;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.so.SoRequest;
import org.onap.policy.so.SoRequest3gpp;

@Path("/")
public class SoSimulatorJaxRs {

    private static final String REPLACE_ME = "${replaceMe}";
    /**
     * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
     * ID is added to the set. When the request is polled, the ID is removed and a "still
     * running" response is returned. When the request is polled again, it sees that there
     * is no entry and returns a completion indication.
     *
     * <p/>
     * This is static so request IDs are retained across servlets.
     */
    private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();

    /**
     * {@code True} if requests should require polling, {@code false}
     * otherwise.  This is used when junit testing the SO actor.
     */
    @Setter
    private static boolean requirePolling = false;

    /**
     * SO post query.
     *
     * @param serviceInstanceId the service instance Id
     * @param vnfInstanceId the VNF Id
     * @return the response
     */
    @POST
    @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
                    @PathParam("vnfInstanceId") final String vnfInstanceId,
                    SoRequest request) {

        List<Map<String, String>> userParam = null;
        userParam = request.getRequestDetails().getRequestParameters().getUserParams();
        if (!userParam.isEmpty() && userParam.toString().contains("FAIL")) {
            // this will be treated as a failure by the SO actor as it's missing the request ID
            return "{}";
        }
        return (requirePolling ? makeStarted() : makeImmediateComplete());
    }

    /**
     * SO Delete.
     *
     * @param serviceInstanceId the service instance Id
     * @param vnfInstanceId the VNF Id
     * @return the response
     */
    @DELETE
    @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
                    @PathParam("vnfInstanceId") final String vnfInstanceId,
                    @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {

        return (requirePolling ? makeStarted() : makeImmediateComplete());
    }

    /**
     * Poll SO result.
     *
     * @param requestId the ID of the request whose status is to be queried
     * @return the response
     */
    @GET
    @Path("/orchestrationRequests/v5/{requestId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String soGetQuery(@PathParam("requestId") final String requestId) {
        if (incomplete.remove(requestId)) {
            // first poll - return "still running"
            return makeStillRunning(requestId);

        } else {
            return makeComplete(requestId);
        }
    }

    @PUT
    @Path("/3gppservices/v7/modify")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String soPost3gpp(SoRequest3gpp request) {
        return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
    }

    @PUT
    @Path("/infra/serviceIntent/v1/modify")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String soPostModifyCll(SoRequest3gpp request) {
        return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.cll.success.json");
    }

    private String makeStarted() {
        var requestId = UUID.randomUUID().toString();

        var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");

        incomplete.add(requestId);

        return response.replace(REPLACE_ME, requestId);
    }

    private String makeImmediateComplete() {
        var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
        return response.replace(REPLACE_ME, UUID.randomUUID().toString());
    }

    private String makeComplete(String requestId) {
        var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
        return response.replace(REPLACE_ME, requestId);
    }

    private String makeStillRunning(String requestId) {
        var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
        return response.replace(REPLACE_ME, requestId);
    }
}