aboutsummaryrefslogtreecommitdiffstats
path: root/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vtp/profile/VTPProfileResource.java
blob: ca046b5acdfb0c70f8a00fded265b66ac58d4750 (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
/**
 * Copyright 2018 Huawei Technologies Co., Ltd.
 *
 * 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.
 */

package org.onap.vtp.profile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpStatus;
import org.onap.vtp.VTPResource;
import org.onap.vtp.error.VTPError;
import org.onap.vtp.error.VTPError.VTPException;
import org.onap.vtp.profile.model.VTPTestProfile;
import org.onap.vtp.profile.model.VTPTestProfile.VTPTestProfileList;
import org.onap.vtp.profile.model.VTPTestProfile.VTPTestProfileProperty;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Path("/vtp")
@Api(tags = {"VTP Envrionment Profile"})
public class VTPProfileResource extends VTPResource {

    public VTPTestProfileList listTestProfilesHandler()  throws VTPException, IOException{
        List<String> args = new ArrayList<>();
        args.addAll(Arrays.asList(new String[] {
                "--product", "open-cli", "profile-list", "--format", "json"
                }));

        JsonArray results = this.makeRpcAndGetJson(args).getAsJsonArray();

        VTPTestProfileList list = new VTPTestProfileList();

       if (results != null && results.isJsonArray() && results.getAsJsonArray().size() > 0) {
       	 JsonArray resultsArray = results.getAsJsonArray();
            for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
                JsonElement jsonElement = it.next();
                JsonObject n = jsonElement.getAsJsonObject();
                if (n.has("profile")){
                        list.getTestProfiles().add(new VTPTestProfile().setName(n.get("profile").getAsString()));
                }
            }
        }

        return list;
    }

    @Path("/profiles")
    @GET
    @ApiOperation(tags = "VTP Envrionment Profile", value = "List available profiles", response = VTPTestProfile.class, responseContainer = "List")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class) })
    public Response listTestProfiles() throws VTPException, IOException {
        return Response.ok(this.listTestProfilesHandler().getTestProfiles().toString(), MediaType.APPLICATION_JSON).build();
    }

    public VTPTestProfile getTestProfileHandler(String profileName) throws VTPException, IOException {
        List<String> args = new ArrayList<>();
        args.addAll(Arrays.asList(new String[] {
                 "--product", "open-cli", "profile-show", "--profile", profileName, "--format", "json"
                }));
        JsonArray results = this.makeRpcAndGetJson(args).getAsJsonArray();

        VTPTestProfile profile = new VTPTestProfile();
        profile.setName(profileName);

        if (results != null && results.isJsonArray() && results.getAsJsonArray().size() > 0) {
          	 JsonArray resultsArray = results.getAsJsonArray();
               for (Iterator<JsonElement> it = resultsArray.iterator(); it.hasNext();) {
                   JsonElement jsonElement = it.next();
                   JsonObject n = jsonElement.getAsJsonObject();
                    VTPTestProfileProperty prp = new VTPTestProfileProperty();

                    prp.setInputParameterName(n.get("parameter").getAsString());
                    prp.setValue(n.get("value").getAsString());

                    if (n.get("service") != null && !n.get("service").getAsString().equals("*"))
                        prp.setTestSuiteName(n.get("service").getAsString());

                    if (n.get("command") != null && !n.get("command").getAsString().equals("*"))
                        prp.setTestCaseName(n.get("command").getAsString());

                    if (n.get("product") != null && !n.get("product").getAsString().equals("*"))
                        prp.setScenario(n.get("product").getAsString());

                    profile.getProperties().add(prp);
            }
        }

        return profile;
    }

    @Path("/profiles/{profileName}")
    @GET
    @ApiOperation(tags = "VTP Envrionment Profile", value = " Retrieve profile details", response = VTPTestProfile.class)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation", response = VTPError.class),
            @ApiResponse(code = HttpStatus.NOT_FOUND_404,
                    message = "Test profile does not exist", response = VTPError.class)})
    public Response getTestProfile(
            @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName)
                    throws IOException, VTPException {
        return Response.ok(this.getTestProfileHandler(profileName).toString(), MediaType.APPLICATION_JSON).build();
    }

    public void setPofileHanlder(VTPTestProfile profile) throws VTPException, IOException {
        List<String> args = new ArrayList<>();


        args.addAll(Arrays.asList(new String[] {
                "--product", "open-cli", "profile-set", "--format", "json", "--profile", profile.getName()
                }));

        Map<String, List<String>> productVsProfile = new HashMap<>();

        for (VTPTestProfileProperty prp: profile.getProperties()) {
            String scenario = prp.getScenario();

            if (prp.getScenario() == null ) {
                scenario = "__global__";
            }

            if (productVsProfile.get(scenario) == null) {
                productVsProfile.put(scenario, new ArrayList<String>());
            }

            if ((prp.getInputParameterName() != null) && !prp.getInputParameterName().isEmpty() &&
                    (prp.getValue() != null) && !prp.getValue().isEmpty()) {

                String paramName = "";
                if (prp.getTestSuiteName() != null && !prp.getTestSuiteName().isEmpty()) {
                    paramName += prp.getTestSuiteName() + ":";

                    if (prp.getTestCaseName() != null && !prp.getTestCaseName().isEmpty()) {
                        paramName += prp.getTestCaseName() + ":";
                    }
                }

                paramName += prp.getInputParameterName();

                productVsProfile.get(scenario).add("--parameter");
                productVsProfile.get(scenario).add(paramName + "=" + prp.getValue());
            }
        }

        for (String scenario: productVsProfile.keySet()) {
            List<String> arguments = new ArrayList<>();
            arguments.addAll(args);
            arguments.addAll(productVsProfile.get(scenario));

            if (!scenario.equals("__global__")) { //profile is set across.
                arguments.add("--product");
                arguments.add(scenario);
            }

            this.makeRpcAndGetJson(arguments);
        }
    }

    public void unsetPofileHanlder(VTPTestProfile profile) throws VTPException, IOException {
        List<String> args = new ArrayList<>();


        args.addAll(Arrays.asList(new String[] {
                "--product", "open-cli", "profile-unset", "--format", "json", "--profile", profile.getName()
                }));

        Map<String, List<String>> productVsProfile = new HashMap<>();

        for (VTPTestProfileProperty prp: profile.getProperties()) {
            String scenario = prp.getScenario();

            if (prp.getScenario() == null ) {
                scenario = "__global__";
            }

            if (productVsProfile.get(scenario) == null) {
                productVsProfile.put(scenario, new ArrayList<String>());
            }

            if ((prp.getInputParameterName() != null) && !prp.getInputParameterName().isEmpty() &&
                    ((prp.getValue() == null) || prp.getValue().isEmpty())) {

                String paramName = "";
                if (prp.getTestSuiteName() != null && !prp.getTestSuiteName().isEmpty()) {
                    paramName += prp.getTestSuiteName() + ":";

                    if (prp.getTestCaseName() != null && !prp.getTestCaseName().isEmpty()) {
                        paramName += prp.getTestCaseName() + ":";
                    }
                }

                paramName += prp.getInputParameterName();

                productVsProfile.get(scenario).add("--parameter");
                productVsProfile.get(scenario).add(paramName);
            }
        }

        for (String scenario: productVsProfile.keySet()) {

            if (productVsProfile.get(scenario).size() == 0) continue;

            List<String> arguments = new ArrayList<>();
            arguments.addAll(args);
            arguments.addAll(productVsProfile.get(scenario));

            if (!scenario.equals("__global__")) { //profile is set across.
                arguments.add("--product");
                arguments.add(scenario);
            }

            this.makeRpcAndGetJson(arguments);
        }
    }

    @Path("/profiles")
    @POST
    @ApiOperation(tags = "VTP Envrionment Profile", value = "Create profile.")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class),
            @ApiResponse(code = HttpStatus.CONFLICT_409,
            message = "Test profile does already exist with given name", response = VTPError.class) })
    public Response createProfile(@Context HttpServletRequest request) throws VTPException, IOException {

        VTPTestProfile profile = new Gson().fromJson(IOUtils.toString(request.getInputStream()), VTPTestProfile.class);

        for (VTPTestProfile p: this.listTestProfilesHandler().getTestProfiles()) {
            if (p.getName().equalsIgnoreCase(profile.getName())) {
                throw new VTPException(
                        new VTPError().setMessage("Test profile does already exist with given name").setHttpStatus(HttpStatus.CONFLICT_409));
            }
        }

        this.setPofileHanlder(profile);

        return Response.ok().build();
    }

    @Path("/profiles/{profileName}")
    @PUT
    @ApiOperation(tags = "VTP Envrionment Profile", value = "Update profile. To remove a profile parameter, set its value to null or empty.")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation",
                    response = VTPError.class) })
    public Response updateProfile(
            @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName,
            @Context HttpServletRequest request) throws VTPException, IOException {

        VTPTestProfile profile = new Gson().fromJson(IOUtils.toString(request.getInputStream()), VTPTestProfile.class);
        if (profile.getName() != null && !profile.getName().equalsIgnoreCase(profileName)) {
            //TODO: rename profile
        }

        profile.setName(profileName);

        //Unset those params which value is null or empty.
        this.unsetPofileHanlder(profile);

        //Set profile
        this.setPofileHanlder(profile);

        return Response.ok().build();
    }

    public void deleteProfileHandler(String profileName) throws VTPException, IOException {
        List<String> args = new ArrayList<>();
        args.addAll(Arrays.asList(new String[] {
                 "--product", "open-cli", "profile-delete", "--profile", profileName, "--format", "json"
                }));
        this.makeRpcAndGetJson(args);
    }

    @Path("/profiles/{profileName}")
    @DELETE
    @ApiOperation(tags = "VTP Envrionment Profile", value = "Delete profile")
    @ApiResponses(value = {
            @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500,
                    message = "Failed to perform the operation", response = VTPError.class),
            @ApiResponse(code = HttpStatus.NOT_FOUND_404,
                    message = "Test profile does not exist", response = VTPError.class)})
    public Response deleteProfile(
            @ApiParam(value = "Test profile name") @PathParam("profileName") String profileName)
                    throws IOException, VTPException {

        this.deleteProfileHandler(profileName);

        return Response.status(HttpStatus.NO_CONTENT_204).build();
    }
}