package org.onap.vid.services;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.hibernate.NonUniqueObjectException;
import org.json.JSONObject;
-import org.json.simple.JSONArray;
-import org.json.simple.parser.JSONParser;
-import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.openecomp.portalsdk.core.service.DataAccessService;
-import org.openecomp.portalsdk.core.util.SystemProperties;
+import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
+import org.onap.portalsdk.core.service.DataAccessService;
+import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.vid.changeManagement.*;
+import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.exceptions.NotFoundException;
import org.onap.vid.model.VNFDao;
import org.onap.vid.model.VidWorkflow;
import org.onap.vid.mso.MsoBusinessLogic;
import org.onap.vid.mso.MsoResponseWrapperInterface;
+import org.onap.vid.mso.RestObject;
+import org.onap.vid.mso.RestObjectWithRequestInfo;
import org.onap.vid.mso.rest.Request;
import org.onap.vid.scheduler.SchedulerProperties;
-import org.onap.vid.scheduler.SchedulerRestInterfaceFactory;
import org.onap.vid.scheduler.SchedulerRestInterfaceIfc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.multipart.MultipartFile;
import javax.ws.rs.BadRequestException;
+import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ChangeManagementServiceImpl implements ChangeManagementService {
- private final static String primaryKey = "payload";
- private final static Set<String> requiredKeys = new HashSet<>(Arrays.asList("request-parameters", "configuration-parameters"));
+ private static final String PRIMARY_KEY = "payload";
+ private static final Set<String> REQUIRED_KEYS = new HashSet<>(Arrays.asList("request-parameters", "configuration-parameters"));
private final DataAccessService dataAccessService;
private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ChangeManagementServiceImpl.class);
private MsoBusinessLogic msoBusinessLogic;
+ private final SchedulerRestInterfaceIfc restClient;
+ private final CloudOwnerService cloudOwnerService;
+
@Autowired
private CsvService csvService;
@Autowired
- public ChangeManagementServiceImpl(DataAccessService dataAccessService, MsoBusinessLogic msoBusinessLogic) {
+ public ChangeManagementServiceImpl(DataAccessService dataAccessService, MsoBusinessLogic msoBusinessLogic, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService) {
this.dataAccessService = dataAccessService;
this.msoBusinessLogic = msoBusinessLogic;
+ this.restClient = schedulerRestInterface;
+ this.cloudOwnerService = cloudOwnerService;
}
@Override
- public Collection<Request> getMSOChangeManagements() throws Exception {
- Collection<Request> result = null;
+ public Collection<Request> getMSOChangeManagements() {
return msoBusinessLogic.getOrchestrationRequestsForDashboard();
}
- private RequestDetails findRequestByVnfName(List<RequestDetails> requests, String vnfName) {
+ protected RequestDetails findRequestByVnfName(List<RequestDetails> requests, String vnfName) {
if (requests == null)
return null;
}
@Override
- public ResponseEntity<String> doChangeManagement(ChangeManagementRequest request, String vnfName) throws Exception {
+ public ResponseEntity<String> doChangeManagement(ChangeManagementRequest request, String vnfName) {
if (request == null)
return null;
ResponseEntity<String> response;
try {
switch (requestType.toLowerCase()) {
case ChangeManagementRequest.UPDATE: {
+ cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
msoResponseWrapperObject = msoBusinessLogic.updateVnf(currentRequestDetails, serviceInstanceId, vnfInstanceId);
break;
}
case ChangeManagementRequest.REPLACE: {
+ cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
msoResponseWrapperObject = msoBusinessLogic.replaceVnf(currentRequestDetails, serviceInstanceId, vnfInstanceId);
break;
}
case ChangeManagementRequest.VNF_IN_PLACE_SOFTWARE_UPDATE: {
+ cloudOwnerService.enrichRequestWithCloudOwner(currentRequestDetails);
msoResponseWrapperObject = msoBusinessLogic.updateVnfSoftware(currentRequestDetails, serviceInstanceId, vnfInstanceId);
break;
}
msoResponseWrapperObject = msoBusinessLogic.updateVnfConfig(currentRequestDetails, serviceInstanceId, vnfInstanceId);
break;
}
+ case ChangeManagementRequest.SCALE_OUT:{
+ msoResponseWrapperObject = msoBusinessLogic.scaleOutVfModuleInstance(currentRequestDetails, serviceInstanceId, vnfInstanceId);
+ break;
+ }
+ default:
+ throw new GenericUncheckedException("Failure during doChangeManagement with request " + request.toString());
}
- response = new ResponseEntity<String>(msoResponseWrapperObject.getResponse(), HttpStatus.OK);
+ response = new ResponseEntity<>(msoResponseWrapperObject.getResponse(), HttpStatus.OK);
return response;
} catch (Exception e) {
logger.error("Failure during doChangeManagement with request " + request.toString(), e);
return currentRequestDetails.getVnfInstanceId();
}
- private String extractServiceInstanceId(RequestDetails currentRequestDetails, String requestType) {
+ protected String extractServiceInstanceId(RequestDetails currentRequestDetails, String requestType) {
try {
String serviceInstanceId = currentRequestDetails.getRelatedInstList().get(0).getRelatedInstance().getInstanceId();
serviceInstanceId.toString(); //throw exception in case that serviceInstanceId is null...
}
@Override
- public JSONArray getSchedulerChangeManagements() {
- JSONArray result = null;
- try {
- String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_GET_SCHEDULES);
- org.onap.vid.scheduler.RestObject<String> restObject = new org.onap.vid.scheduler.RestObject<>();
- SchedulerRestInterfaceIfc restClient = SchedulerRestInterfaceFactory.getInstance();
-
- String str = new String();
- restObject.set(str);
- restClient.Get(str, "", path, restObject);
- String restCallResult = restObject.get();
- JSONParser parser = new JSONParser();
- Object parserResult = parser.parse(restCallResult);
- result = (JSONArray) parserResult;
- } catch (Exception e) {
- e.printStackTrace();
- }
+ public RestObjectWithRequestInfo<ArrayNode> getSchedulerChangeManagementsWithRequestInfo() {
+ String path = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_GET_SCHEDULES);
+ RestObject<ArrayNode> restObject = new RestObject<>();
+ ArrayNode jsonArray = new ArrayNode(new JsonNodeFactory(true));
+ restObject.set(jsonArray);
+ return restClient.Get(jsonArray, path, restObject);
+ }
- return result;
+ @Override
+ public ArrayNode getSchedulerChangeManagements() {
+ RestObjectWithRequestInfo<ArrayNode> responseWithRequestInfo = getSchedulerChangeManagementsWithRequestInfo();
+ return responseWithRequestInfo.getRestObject().get();
}
@Override
public Pair<String, Integer> deleteSchedule(String scheduleId) {
try {
String path = String.format(SystemProperties.getProperty(SchedulerProperties.SCHEDULER_DELETE_SCHEDULE), scheduleId);
- org.onap.vid.scheduler.RestObject<String> restObject = new org.onap.vid.scheduler.RestObject<>();
- SchedulerRestInterfaceIfc restClient = SchedulerRestInterfaceFactory.getInstance();
- String str = new String();
+ RestObject<String> restObject = new RestObject<>();
+ String str = "";
restObject.set(str);
restClient.Delete(str, "", path, restObject);
String restCallResult = restObject.get();
continue;
}
@SuppressWarnings("unchecked") List<VNFDao> vnfList = dataAccessService.getList(VNFDao.class, getVnfQueryString(workflowsDetail.getVnfDetails().getUUID(), workflowsDetail.getVnfDetails().getInvariantUUID()), null, null);
- if (vnfList.size() == 0) {
+ if (vnfList.isEmpty()) {
vnfList.add(saveNewVnf(workflowsDetail));
}
@SuppressWarnings("unchecked") List<VidWorkflow> workflowList = dataAccessService.getList(VidWorkflow.class, String.format(" where wokflowName = '%s'", workflowsDetail.getWorkflowName()), null, null);
- if (workflowList.size() == 0) {
+ if (workflowList.isEmpty()) {
vnfWorkflowRelationResponse.getErrors().add("Not Found instance of workflow " + workflowsDetail.getWorkflowName() + " for vnf with UUID " + workflowsDetail.getVnfDetails().getUUID() + " and with invariantUUID " + workflowsDetail.getVnfDetails().getInvariantUUID());
continue;
}
}
@Override
- public String uploadConfigUpdateFile(MultipartFile file)
- throws Exception {
- JSONObject json = csvService.convertCsvToJson(csvService.readCsv(file));
+ public String uploadConfigUpdateFile(MultipartFile file) {
+ JSONObject json = null;
+ try {
+ json = csvService.convertCsvToJson(csvService.readCsv(file));
+ } catch (InstantiationException | IllegalAccessException | IOException e) {
+ throw new BadRequestException("Invalid csv file", e);
+ }
if (!validateJsonOutput(json))
throw new BadRequestException("Invalid csv file");
- json = json.getJSONObject(primaryKey);
- json = new JSONObject().put(primaryKey, json.toString());
+ json = json.getJSONObject(PRIMARY_KEY);
+ json = new JSONObject().put(PRIMARY_KEY, json.toString());
return json.toString();
}
private boolean validateJsonOutput(org.json.JSONObject json) {
- if (!json.has(primaryKey) || !json.getJSONObject(primaryKey).keySet().containsAll(requiredKeys))
+ if (!json.has(PRIMARY_KEY) || !json.getJSONObject(PRIMARY_KEY).keySet().containsAll(REQUIRED_KEYS))
return false;
return true;
}