917e4eb638cebe091f2678a2c68cb5fcf816a8ce
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================ Copyright (C)
5  * 2018 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================ Licensed under
7  * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License. ============LICENSE_END=========================================================
16  */
17
18 package org.onap.pnfsimulator.simulator;
19
20 import static java.lang.Integer.parseInt;
21 import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL;
22 import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION;
23 import static org.onap.pnfsimulator.message.MessageConstants.VES_SERVER_URL;
24 import com.github.fge.jsonschema.core.exceptions.ProcessingException;
25 import java.io.IOException;
26 import java.time.Duration;
27 import java.util.ArrayList;
28 import java.util.Optional;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.pnfsimulator.ConfigurationProvider;
32 import org.onap.pnfsimulator.FileProvider;
33 import org.onap.pnfsimulator.PnfSimConfig;
34 import org.onap.pnfsimulator.message.MessageProvider;
35 import org.onap.pnfsimulator.simulator.validation.JSONValidator;
36 import org.onap.pnfsimulator.simulator.validation.ValidationException;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 @Service
41 public class SimulatorFactory {
42
43     private static final String DEFAULT_OUTPUT_SCHEMA_PATH = "json_schema/output_validator_ves_schema_30.0.1.json";
44
45     private MessageProvider messageProvider;
46     private JSONValidator validator;
47
48     @Autowired
49     public SimulatorFactory(MessageProvider messageProvider, JSONValidator validator) {
50         this.messageProvider = messageProvider;
51         this.validator = validator;
52     }
53
54     public Simulator create(JSONObject simulatorParams, JSONObject commonEventHeaderParams,
55             Optional<JSONObject> pnfRegistrationParams, Optional<JSONObject> notificationParams)
56             throws ProcessingException, IOException, ValidationException {
57         Duration duration = Duration.ofSeconds(parseInt(simulatorParams.getString(TEST_DURATION)));
58         Duration interval = Duration.ofSeconds(parseInt(simulatorParams.getString(MESSAGE_INTERVAL)));
59         String vesUrl = simulatorParams.getString(VES_SERVER_URL);
60
61         JSONObject messageBody =
62                 messageProvider.createMessage(commonEventHeaderParams, pnfRegistrationParams, notificationParams);
63         validator.validate(messageBody.toString(), DEFAULT_OUTPUT_SCHEMA_PATH);
64
65         JSONArray messageBodyArray = new JSONArray();
66
67         PnfSimConfig configuration = ConfigurationProvider.getConfigInstance();
68         String xnfUrl = configuration.getIpsftp() + "/";
69
70         ArrayList<String> fileList = FileProvider.getFiles();
71
72         // for (String f : fileList) {
73         // System.out.println("f processed from fileList: " + f.toString());
74         // JSONObject vesEvent = messageProvider.createOneVes(commonEventHeaderParams,
75         // pnfRegistrationParams,
76         // notificationParams, url, f);
77         // messageBodyArray.put(vesEvent);
78         // }
79
80         String fileName = fileList.get(1);
81         System.out.println("f processed from fileList: " + fileName.toString());
82         JSONObject vesEvent = messageProvider.createOneVesEvent(xnfUrl, fileName);
83
84         return Simulator.builder().withVesUrl(configuration.getVesip()).withDuration(duration).withInterval(interval)
85                 .withMessageBody(vesEvent).build();
86
87     }
88 }