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
|
package org.onap.sdnc.apps.ms.sanitytest.controllers;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.ccsdk.apps.services.RestException;
import org.onap.ccsdk.apps.services.SvcLogicFactory;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;
import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;
import org.onap.sdnc.apps.ms.sanitytest.swagger.OperationsApi;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiInputBodyparam;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiRequestInformation;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiResponseField;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiResponseFields;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiResultEnumeration;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiServiceConfigurationOperation;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiTestListItem;
import org.onap.sdnc.apps.ms.sanitytest.swagger.model.SanitytestApiTestNameEnumeration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@Controller
@ComponentScan(basePackages = { "org.onap.sdnc.apps.ms.sanitytest.*", "org.onap.ccsdk.apps.services" })
@Import(value = SvcLogicFactory.class)
public class SanityTestOperationsApi implements OperationsApi
{
private static final String MODULE_NAME = "SANITYTEST-API";
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
@Autowired
protected SvcLogicServiceBase svc;
@Autowired
protected SvcLogicLoader svcLogicLoader;
private static class Iso8601Util {
private static TimeZone timeZone = TimeZone.getTimeZone("UTC");
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
private Iso8601Util() {
}
static {
dateFormat.setTimeZone(timeZone);
}
private static String now() {
return dateFormat.format(new Date());
}
}
@org.springframework.beans.factory.annotation.Autowired
public SanityTestOperationsApi(ObjectMapper objectMapper, HttpServletRequest request) {
this.objectMapper = objectMapper;
this.request = request;
}
@Override
public Optional<ObjectMapper> getObjectMapper() {
return Optional.ofNullable(objectMapper);
}
@Override
public Optional<HttpServletRequest> getRequest() {
return Optional.ofNullable(request);
}
@Override
public ResponseEntity<SanitytestApiServiceConfigurationOperation> operationsSANITYTESTAPIserviceConfigurationOperationPost(
@Valid SanitytestApiInputBodyparam input) throws RestException {
final String svcOperation = "service-configuration-operation";
SanitytestApiRequestInformation reqInfo = input.getInput().getRequestInformation();
List<SanitytestApiTestListItem> testList = reqInfo.getTestList();
SanitytestApiServiceConfigurationOperation retval = new SanitytestApiServiceConfigurationOperation();
SanitytestApiResponseFields resp = new SanitytestApiResponseFields();
SvcLogicContext ctxIn = new SvcLogicContext();
// Add input to SvcLogicContext
try {
ctxIn.mergeJson(svcOperation + "-input", objectMapper.writeValueAsString(input.getInput()));
} catch (JsonProcessingException e) {
log.error("Caught exception trying to save input to SvcLogicContext", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
// Call DG
try {
// Any of these can throw a nullpointer exception
// execute should only throw a SvcLogicException
SvcLogicContext ctxOut = svc.execute(MODULE_NAME, svcOperation, null, "sync", ctxIn);
Properties respProps = ctxOut.toProperties();
String errCode = respProps.getProperty("error-code", "200");
if ("200".equals(errCode)) {
// DG returns success - return test results.
int testcount = Integer.parseInt(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list_length"));
for (int i = 0; i < testcount; i++) {
SanitytestApiResponseField respItem = new SanitytestApiResponseField();
SanitytestApiTestListItem testItem = testList.get(i);
respItem.setResponseTestNumber(testItem.getTestNumber());
String testName = respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].response-test-name");
respItem.setResponseTestName(SanitytestApiTestNameEnumeration.fromValue(testName));
respItem.setStartTime(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].start-time"));
respItem.setEndTime(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].end-time"));
respItem.setElapsedTime(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].elapsed-time"));
respItem.setErrorMessage(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].error-message"));
respItem.setResult(SanitytestApiResultEnumeration.fromValue(respProps.getProperty("service-configuration-operation-output.response-information.response-test-list["
+ i + "].result")));
resp.addResponseInformationItem(respItem);
}
log.info("Returned SUCCESS for " + svcOperation);
retval.setOutput(resp);
return new ResponseEntity<> (retval, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
} catch (NullPointerException npe) {
log.error("Caught NPE", npe);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} catch (SvcLogicException e) {
log.error("Caught SvcLogicException", e);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
|