import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
+import javax.annotation.PostConstruct;
import org.apache.commons.io.IOUtils;
import org.onap.nbi.OnapComponentsUrlPaths;
import org.onap.nbi.exceptions.BackendFunctionalException;
@Service
public class SdcClient {
- public static final String HTTP_CALL_SDC_ON = "HTTP call SDC on ";
@Autowired
private RestTemplate restTemplate;
private static final Logger LOGGER = LoggerFactory.getLogger(SdcClient.class);
- public LinkedHashMap callGet(String id) {
- StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
- .append("/").append(id).append(OnapComponentsUrlPaths.SDC_GET_PATH);
- UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
+ private String sdcGetUrl;
+ private String sdcFindUrl;
+ private String sdcHealthCheck;
- ResponseEntity<Object> response = callSdc(callURI.build().encode().toUri());
+ @PostConstruct
+ private void setUpAndLogSDCUrl() {
+ sdcGetUrl= new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL+"/{id}"+OnapComponentsUrlPaths.SDC_GET_PATH).toString();
+ sdcFindUrl = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL).toString();
+ sdcHealthCheck = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_HEALTH_CHECK).toString();
+
+
+
+ LOGGER.info("SDC GET url : "+sdcGetUrl);
+ LOGGER.info("SDC FIND url : "+ sdcFindUrl);
+ LOGGER.info("SDC HealthCheck : "+ sdcHealthCheck);
+
+ }
+
+
+ public Map callGet(String id) {
+
+ String callUrl = sdcGetUrl.replace("{id}", id);
+ UriComponentsBuilder callURLFormated = UriComponentsBuilder.fromHttpUrl(callUrl);
+ ResponseEntity<Object> response = callSdc(callURLFormated.build().encode().toUri());
return (LinkedHashMap) response.getBody();
}
public List<LinkedHashMap> callFind(MultiValueMap<String, String> parametersMap) {
- UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(sdcHost + OnapComponentsUrlPaths.SDC_ROOT_URL);
+ UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(sdcFindUrl);
if (parametersMap != null) {
Map<String, String> stringStringMap = parametersMap.toSingleValueMap();
- for (String key : stringStringMap.keySet()) {
- if (!key.equals("fields")) {
- callURI.queryParam(key, stringStringMap.get(key));
+ for (Entry<String, String> entry : stringStringMap.entrySet()) {
+ if (!entry.getKey().equals("fields")) {
+ callURI.queryParam(entry.getKey(), entry.getValue());
}
}
}
}
+ public LinkedHashMap callCheckConnectivity() {
+
+ UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(sdcHealthCheck);
+ ResponseEntity<Object> response = callSdc(callURI.build().encode().toUri());
+ return (LinkedHashMap) response.getBody();
+
+ }
+
+
public File callGetWithAttachment(String toscaModelUrl) {
StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(toscaModelUrl);
UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
+ File directory = new File("temptoscafile");
+ if (! directory.exists()){
+ directory.mkdir();
+ }
- String fileName = System.currentTimeMillis() + "tosca.csar";
+ String fileName = "temptoscafile/"+System.currentTimeMillis() + "tosca.csar";
ResponseEntity<byte[]> response = callSdcWithAttachment(callURI.build().encode().toUri());
File toscaFile = new File(fileName);
try {
return toscaFile;
}
+
+ public Path getServiceToscaModel(String uuid) throws IOException {
+ StringBuilder urlBuilder = new StringBuilder().append(sdcHost).append(OnapComponentsUrlPaths.SDC_ROOT_URL)
+ .append("/").append(uuid).append(OnapComponentsUrlPaths.SDC_TOSCA_PATH);
+
+ UriComponentsBuilder callURI = UriComponentsBuilder.fromHttpUrl(urlBuilder.toString());
+
+ InputStream inputStream = (InputStream) callSdc(callURI.build().encode().toUri()).getBody();
+
+ return createTmpFile(inputStream);
+ }
+
+ private Path createTmpFile(InputStream csarInputStream) throws IOException {
+ Path csarFile = Files.createTempFile("csar", ".zip");
+ Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
+
+ LOGGER.debug("Tosca file was saved at: {} ", csarFile.toAbsolutePath());
+
+ return csarFile;
+ }
private HttpEntity<String> buildRequestHeader() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HEADER_ECOMP_INSTANCE_ID, ecompInstanceId);
httpHeaders.add(HEADER_AUTHORIZATION, sdcHeaderAuthorization);
- HttpEntity<String> entity = new HttpEntity<>("parameters", httpHeaders);
-
- return entity;
+ return new HttpEntity<>("parameters", httpHeaders);
}
private ResponseEntity<Object> callSdc(URI callURI) {
ResponseEntity<Object> response =
restTemplate.exchange(callURI, HttpMethod.GET, buildRequestHeader(), Object.class);
- LOGGER.debug("response body : " + response.getBody().toString());
- LOGGER.info("response status : " + response.getStatusCodeValue());
+
+ if(LOGGER.isDebugEnabled()) {
+ LOGGER.debug("response body : {} ",response.getBody().toString());
+ }
+ LOGGER.info("response status : {}", response.getStatusCodeValue());
loggDebugIfResponseKo(callURI.toString(), response);
return response;
}
ResponseEntity<byte[]> response =
restTemplate.exchange(callURI, HttpMethod.GET, buildRequestHeader(), byte[].class);
LOGGER.info("response status : " + response.getStatusCodeValue());
- if (!response.getStatusCode().equals(HttpStatus.OK)) {
- LOGGER.error(HTTP_CALL_SDC_ON + callURI.toString() + " returns " + response.getStatusCodeValue() + ", "
- + response.getBody().toString());
+ if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
+ LOGGER.warn("HTTP call SDC on {} returns {} ", callURI.toString() , response.getStatusCodeValue());
}
return response;
} catch (BackendFunctionalException e) {
- LOGGER.error(HTTP_CALL_SDC_ON + callURI.toString() + " error " + e);
+ LOGGER.error("HTTP call SDC on {} error : {}", callURI.toString() , e);
return null;
}
}
private void loggDebugIfResponseKo(String callURI, ResponseEntity<Object> response) {
- if (!response.getStatusCode().equals(HttpStatus.OK)) {
- LOGGER.warn(HTTP_CALL_SDC_ON + callURI + " returns " + response.getStatusCodeValue() + ", "
- + response.getBody().toString());
+ if (LOGGER.isWarnEnabled() && !response.getStatusCode().equals(HttpStatus.OK)) {
+ LOGGER.warn("HTTP call SDC on {} returns {} , {}", callURI , response.getStatusCodeValue() , response.getBody().toString());
}
}
}