diff options
Diffstat (limited to 'runtime-controlloop/src/main')
6 files changed, 271 insertions, 18 deletions
diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java index e676cbe0e..74b5394f4 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/commissioning/CommissioningProvider.java @@ -33,8 +33,13 @@ import java.util.stream.Collectors; import javax.ws.rs.core.Response.Status; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; +import org.onap.policy.clamp.controlloop.runtime.supervision.SupervisionHandler; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityType; @@ -62,6 +67,8 @@ public class CommissioningProvider { private final PolicyModelsProvider modelsProvider; private final ControlLoopProvider clProvider; private final ObjectMapper mapper = new ObjectMapper(); + private final ParticipantProvider participantProvider; + private final SupervisionHandler supervisionHandler; private static final Object lockit = new Object(); @@ -71,9 +78,14 @@ public class CommissioningProvider { * @param modelsProvider the PolicyModelsProvider * @param clProvider the ControlLoopProvider */ - public CommissioningProvider(PolicyModelsProvider modelsProvider, ControlLoopProvider clProvider) { + public CommissioningProvider(PolicyModelsProvider modelsProvider, + ControlLoopProvider clProvider, + SupervisionHandler supervisionHandler, + ParticipantProvider participantProvider) { this.modelsProvider = modelsProvider; this.clProvider = clProvider; + this.supervisionHandler = supervisionHandler; + this.participantProvider = participantProvider; mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); } @@ -85,9 +97,33 @@ public class CommissioningProvider { * @throws PfModelException on creation errors */ public CommissioningResponse createControlLoopDefinitions(ToscaServiceTemplate serviceTemplate) - throws PfModelException { + throws PfModelException, ControlLoopException { + + if (verifyIfInstancePropertiesExists()) { + throw new ControlLoopException(Status.BAD_REQUEST, + "Delete instances, to commission control loop definitions"); + } + synchronized (lockit) { modelsProvider.createServiceTemplate(serviceTemplate); + List<Participant> participantList = + participantProvider.getParticipants(null, + null); + + if (participantList != null) { + for (Participant participant: participantList) { + var participantType = new ToscaConceptIdentifier(); + participantType.setName(participant.getType()); + participantType.setVersion(participant.getTypeVersion()); + + var participantUpdate = new ParticipantUpdate(); + participantUpdate.setParticipantId(participant.getDefinition()); + participantUpdate.setParticipantType(participantType); + + this.supervisionHandler.handleSendCommissionMessage(participantUpdate); + } + } + } var response = new CommissioningResponse(); @@ -110,8 +146,33 @@ public class CommissioningProvider { * @return the result of the deletion * @throws PfModelException on deletion errors */ - public CommissioningResponse deleteControlLoopDefinition(String name, String version) throws PfModelException { + public CommissioningResponse deleteControlLoopDefinition(String name, String version) + throws PfModelException, ControlLoopException { + + if (verifyIfInstancePropertiesExists()) { + throw new ControlLoopException(Status.BAD_REQUEST, + "Delete instances, to commission control loop definitions"); + } + synchronized (lockit) { + List<Participant> participantList = + participantProvider.getParticipants(null, + null); + + if (participantList != null) { + for (Participant participant : participantList) { + var participantType = new ToscaConceptIdentifier(); + participantType.setName(participant.getType()); + participantType.setVersion(participant.getTypeVersion()); + + var participantUpdate = new ParticipantUpdate(); + participantUpdate.setParticipantId(participant.getDefinition()); + participantUpdate.setParticipantType(participantType); + + this.supervisionHandler.handleSendDeCommissionMessage(participantUpdate); + } + } + modelsProvider.deleteServiceTemplate(name, version); } @@ -365,7 +426,9 @@ public class CommissioningProvider { * @return the tosca service template * @throws PfModelException on errors getting tosca service template */ - public String getToscaServiceTemplateReduced(String name, String version) throws PfModelException { + public String getToscaServiceTemplateReduced(String name, String version) + throws PfModelException { + var serviceTemplates = new ToscaServiceTemplates(); serviceTemplates.setServiceTemplates(modelsProvider.getServiceTemplateList(name, version)); @@ -431,4 +494,15 @@ public class CommissioningProvider { throw new PfModelException(Status.BAD_REQUEST, "Converion to Json Schema failed", e); } } + + /** + * Validates to see if there is any instance properties saved. + * + * @return true if exists instance properties + */ + private Boolean verifyIfInstancePropertiesExists() { + return clProvider.getNodeTemplates(null, null).stream() + .anyMatch(nodeTemplate -> nodeTemplate.getKey().getName().contains("_Instance")); + + } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java index cb22132b4..da85b0a83 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProvider.java @@ -37,6 +37,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.GenericNameVersion; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; @@ -48,6 +49,7 @@ import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.stereotype.Component; /** @@ -62,6 +64,44 @@ public class ControlLoopInstantiationProvider { private static final Object lockit = new Object(); + private static final String CL_ELEMENT_NAME = "name"; + + /** + * Create Instance Properties. + * + * @param serviceTemplate the service template + * @return the result of the instantiation operation + * @throws PfModelException on creation errors + */ + public InstancePropertiesResponse saveInstanceProperties(ToscaServiceTemplate serviceTemplate) { + + String instanceName = generateSequentialInstanceName(); + + Map<String, ToscaNodeTemplate> nodeTemplates = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + + nodeTemplates.forEach((key, template) -> { + String name = key + instanceName; + String description = template.getDescription() + instanceName; + template.setName(name); + template.setDescription(description); + + changeInstanceElementsName(template, instanceName); + + }); + + Map<String, ToscaNodeTemplate> toscaSavedNodeTemplate = controlLoopProvider + .saveInstanceProperties(serviceTemplate); + + var response = new InstancePropertiesResponse(); + + // @formatter:off + response.setAffectedInstanceProperties(toscaSavedNodeTemplate.values().stream().map(template -> + template.getKey().asIdentifier()).collect(Collectors.toList())); + // @formatter:on + + return response; + } + /** * Create control loops. * @@ -267,10 +307,10 @@ public class ControlLoopInstantiationProvider { List<ControlLoop> controlLoops = controlLoopProvider.getControlLoops(name, version); - ControlLoopOrderStateResponse response = new ControlLoopOrderStateResponse(); + var response = new ControlLoopOrderStateResponse(); controlLoops.forEach(controlLoop -> { - GenericNameVersion genericNameVersion = new GenericNameVersion(); + var genericNameVersion = new GenericNameVersion(); genericNameVersion.setName(controlLoop.getName()); genericNameVersion.setVersion(controlLoop.getVersion()); response.getControlLoopIdentifierList().add(genericNameVersion); @@ -278,4 +318,44 @@ public class ControlLoopInstantiationProvider { return response; } + + /** + * Creates instance element name. + * + * @param serviceTemplate the service serviceTemplate + * @param instanceName to amend to the element name + */ + private void changeInstanceElementsName(ToscaNodeTemplate serviceTemplate, String instanceName) { + + @SuppressWarnings("unchecked") + List<Map<String, String>> controlLoopElements = (List<Map<String, String>>) serviceTemplate.getProperties() + .get("elements"); + + if (controlLoopElements != null) { + controlLoopElements.forEach(clElement -> { + String name = clElement.get(CL_ELEMENT_NAME) + instanceName; + clElement.replace(CL_ELEMENT_NAME, name); + }); + } + } + + + /** + * Generates Instance Name in sequential order and return it to append to the Node Template Name. + * + * @return instanceName + */ + private String generateSequentialInstanceName() { + List<ToscaNodeTemplate> nodeTemplates = controlLoopProvider.getNodeTemplates(null, null); + + int instanceNumber = + nodeTemplates.stream().map(ToscaNodeTemplate::getName) + .filter(name -> name.contains("_Instance")).map(n -> { + String[] defNameArr = n.split("_Instance"); + + return Integer.parseInt(defNameArr[1]); + }).reduce(0, Math::max); + + return "_Instance" + (instanceNumber + 1); + } } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/CommissioningController.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/CommissioningController.java index ec7f14d8b..2c3a41e26 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/CommissioningController.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/CommissioningController.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.UUID; import javax.ws.rs.core.Response.Status; import lombok.RequiredArgsConstructor; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController; @@ -120,7 +121,7 @@ public class CommissioningController extends AbstractRestController { name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId, @ApiParam(value = "Entity Body of Control Loop", required = true) @RequestBody ToscaServiceTemplate body) - throws PfModelException { + throws PfModelException, ControlLoopException { return ResponseEntity.ok().body(provider.createControlLoopDefinitions(body)); } @@ -187,7 +188,7 @@ public class CommissioningController extends AbstractRestController { @ApiParam( value = "Control Loop definition version", required = true) @RequestParam("version") String version) - throws PfModelException { + throws PfModelException, ControlLoopException { return ResponseEntity.ok().body(provider.deleteControlLoopDefinition(name, version)); } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/InstantiationController.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/InstantiationController.java index 6f0c859da..d2a85c46d 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/InstantiationController.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/rest/InstantiationController.java @@ -33,11 +33,13 @@ import lombok.RequiredArgsConstructor; import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopOrderStateResponse; +import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstancePropertiesResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider; import org.onap.policy.clamp.controlloop.runtime.main.web.AbstractRestController; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; @@ -127,6 +129,70 @@ public class InstantiationController extends AbstractRestController { } /** + * Saves instance properties. + * + * @param requestId request ID used in ONAP logging + * @param body the body of control loop following TOSCA definition + * @return a response + */ + // @formatter:off + @PostMapping(value = "/instanceProperties", + consumes = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML}, + produces = {MediaType.APPLICATION_JSON_VALUE, APPLICATION_YAML}) + @ApiOperation( + value = "Saves instance properties", + notes = "Saves instance properties, returning the saved instances properties and it's version", + response = InstancePropertiesResponse.class, + tags = {TAGS}, + authorizations = @Authorization(value = AUTHORIZATION_TYPE), + responseHeaders = { + @ResponseHeader( + name = VERSION_MINOR_NAME, + description = VERSION_MINOR_DESCRIPTION, + response = String.class), + @ResponseHeader( + name = VERSION_PATCH_NAME, + description = VERSION_PATCH_DESCRIPTION, + response = String.class), + @ResponseHeader( + name = VERSION_LATEST_NAME, + description = VERSION_LATEST_DESCRIPTION, + response = String.class), + @ResponseHeader( + name = REQUEST_ID_NAME, + description = REQUEST_ID_HDR_DESCRIPTION, + response = UUID.class) + }, + extensions = { + @Extension + ( + name = EXTENSION_NAME, + properties = { + @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION), + @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE) + } + ) + } + ) + @ApiResponses( + value = { + @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE), + @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE), + @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE) + } + ) + // @formatter:on + public ResponseEntity<InstancePropertiesResponse> createInstanceProperties( + @RequestHeader( + name = REQUEST_ID_NAME, + required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId, + @ApiParam(value = "Body of instance properties", required = true) @RequestBody ToscaServiceTemplate body) + throws PfModelException { + + return ResponseEntity.ok().body(provider.saveInstanceProperties(body)); + } + + /** * Queries details of all control loops. * * @param requestId request ID used in ONAP logging diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java index db7d34895..d06698ec4 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java @@ -34,6 +34,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus; +import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck; import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher; @@ -140,7 +141,7 @@ public class SupervisionHandler { participantRegisterAckPublisher.send(participantRegisterMessage.getMessageId()); participantUpdatePublisher.send(participantRegisterMessage.getParticipantId(), - participantRegisterMessage.getParticipantType()); + participantRegisterMessage.getParticipantType(), true); } /** @@ -165,6 +166,30 @@ public class SupervisionHandler { } /** + * Send commissioning update message to dmaap. + * + * @param participantUpdateMessage the ParticipantUpdate message to send + */ + public void handleSendCommissionMessage(ParticipantUpdate participantUpdateMessage) { + LOGGER.debug("Participant update message being sent {}", participantUpdateMessage); + + participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(), + participantUpdateMessage.getParticipantType(), true); + } + + /** + * Send decommissioning update message to dmaap. + * + * @param participantUpdateMessage the ParticipantUpdate message to send + */ + public void handleSendDeCommissionMessage(ParticipantUpdate participantUpdateMessage) { + LOGGER.debug("Participant update message being sent {}", participantUpdateMessage); + + participantUpdatePublisher.send(participantUpdateMessage.getParticipantId(), + participantUpdateMessage.getParticipantType(), false); + } + + /** * Supervise a control loop, performing whatever actions need to be performed on the control loop. * * @param controlLoop the control loop to supervises diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java index 5edf528b8..d5dc4a6d0 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ParticipantUpdatePublisher.java @@ -22,22 +22,21 @@ package org.onap.policy.clamp.controlloop.runtime.supervision.comm; import java.time.Instant; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import lombok.AllArgsConstructor; -import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate; -import org.onap.policy.clamp.controlloop.runtime.commissioning.CommissioningProvider; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -51,7 +50,7 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class); private static final String CONTROL_LOOP_ELEMENT = "ControlLoopElement"; - private final CommissioningProvider commissioningProvider; + private final PolicyModelsProvider modelsProvider; private static final Coder CODER = new StandardCoder(); /** @@ -60,7 +59,8 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par * @param participantId the participant Id * @param participantType the participant Type */ - public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType) { + public void send(ToscaConceptIdentifier participantId, ToscaConceptIdentifier participantType, + boolean commissionFlag) { var message = new ParticipantUpdate(); message.setParticipantId(participantId); message.setParticipantType(participantType); @@ -68,7 +68,7 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par ToscaServiceTemplate toscaServiceTemplate; try { - toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null); + toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0); } catch (PfModelException pfme) { LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme); return; @@ -91,8 +91,15 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par } } - message.setParticipantDefinitionUpdates(participantDefinitionUpdates); - message.setToscaServiceTemplate(toscaServiceTemplate); + if (commissionFlag) { + // Commission the controlloop but sending participantdefinitions to participants + message.setParticipantDefinitionUpdates(participantDefinitionUpdates); + message.setToscaServiceTemplate(toscaServiceTemplate); + } else { + // DeCommission the controlloop but deleting participantdefinitions on participants + message.setParticipantDefinitionUpdates(null); + message.setToscaServiceTemplate(null); + } LOGGER.debug("Participant Update sent {}", message); super.send(message); } @@ -127,7 +134,7 @@ public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<Par private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition, ToscaConceptIdentifier clParticipantId, List<ControlLoopElementDefinition> controlLoopElementDefinitionList) { - ParticipantDefinition participantDefinition = new ParticipantDefinition(); + var participantDefinition = new ParticipantDefinition(); participantDefinition.setParticipantId(clParticipantId); controlLoopElementDefinitionList.add(clDefinition); participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList); |