Skip to content

Commit

Permalink
display error message if a mapping could not be enabled for a connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Oct 2, 2024
1 parent 8b99a76 commit f37be37
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ public void updateActiveSubscriptionsOutbound(List<Mapping> updatedMappings) {
* the same subscriptionTopic the subscriptionTopic is unsubscribed.
* Only inactive mappings can be updated except activation/deactivation.
**/
public void updateActiveSubscriptionInbound(Mapping mapping, Boolean create, Boolean activationChanged) {
public boolean updateActiveSubscriptionInbound(Mapping mapping, Boolean create, Boolean activationChanged) {
if (isConnected()) {
Boolean containsWildcards = mapping.subscriptionTopic.matches(".*[#\\+].*");
boolean validDeployment = (supportsWildcardsInTopic() || !containsWildcards);
Expand Down Expand Up @@ -504,10 +504,12 @@ public void updateActiveSubscriptionInbound(Mapping mapping, Boolean create, Boo
}
}
} else {
log.warn("Tenant {} - Mapping {} contains wildcards like #,+ which are not support by connector {}",tenant, mapping.getId(), connectorName);
log.warn("Tenant {} - Mapping {} contains wildcards like #,+ which are not support by connector {}",
tenant, mapping.getId(), connectorName);
return false;
}

}
return true;
}

/**
Expand All @@ -528,8 +530,9 @@ public void updateActiveSubscriptionsInbound(List<Mapping> updatedMappings, bool
updatedMappings.forEach(mapping -> {
Boolean containsWildcards = mapping.subscriptionTopic.matches(".*[#\\+].*");
boolean validDeployment = (supportsWildcardsInTopic() || !containsWildcards);
if(!validDeployment)
log.warn("Tenant {} - Mapping {} contains wildcards like #,+ which are not support by connector {}",tenant, mapping.getId(), connectorName);
if (!validDeployment)
log.warn("Tenant {} - Mapping {} contains wildcards like #,+ which are not support by connector {}",
tenant, mapping.getId(), connectorName);
List<String> deploymentMapEntry = mappingComponent.getDeploymentMapEntry(tenant, mapping.ident);
boolean isDeployed = false;
if (deploymentMapEntry != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public void setConfigurationRegistry(@Lazy ConfigurationRegistry configurationRe

public static final String STATUS_SUBSCRIPTION_EVENT_TYPE = "d11r_subscriptionEvent";
public static final String STATUS_CONNECTOR_EVENT_TYPE = "d11r_connectorStatusEvent";
public static final String STATUS_MAPPING_ACTIVATION_ERROR_EVENT_TYPE = "d11r_mappingActivationErrorEvent";
public static final String STATUS_MAPPING_CHANGED_EVENT_TYPE = "d11r_mappingChangedEvent";
public static final String STATUS_NOTIFICATION_EVENT_TYPE = "d11r_notificationStatusEvent";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import dynamic.mapping.connector.core.registry.ConnectorRegistryException;
import dynamic.mapping.processor.model.ProcessingContext;
import org.apache.commons.lang3.mutable.MutableInt;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
Expand All @@ -65,6 +66,7 @@

import lombok.extern.slf4j.Slf4j;
import dynamic.mapping.core.BootstrapService;
import dynamic.mapping.core.C8YAgent;
import dynamic.mapping.core.ConfigurationRegistry;
import dynamic.mapping.core.ConnectorStatusEvent;
import dynamic.mapping.core.MappingComponent;
Expand Down Expand Up @@ -403,7 +405,7 @@ public ResponseEntity<Map<String, List<String>>> getDeploymentMap() {
}

@RequestMapping(value = "/operation", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<HttpStatus> runOperation(@Valid @RequestBody ServiceOperation operation) {
public ResponseEntity<?> runOperation(@Valid @RequestBody ServiceOperation operation) {
String tenant = contextService.getContext().getTenant();
log.info("Tenant {} - Post operation: {}", tenant, operation.toString());
try {
Expand Down Expand Up @@ -471,13 +473,30 @@ public ResponseEntity<HttpStatus> runOperation(@Valid @RequestBody ServiceOperat
.getClientsForTenant(tenant);
// subscribe/unsubscribe respective subscriptionTopic of mapping only for
// outbound mapping
Map<String, String> failed = new HashMap<>();
for (AConnectorClient client : connectorMap.values()) {
if (updatedMapping.direction == Direction.INBOUND) {
client.updateActiveSubscriptionInbound(updatedMapping, false, true);
if (!client.updateActiveSubscriptionInbound(updatedMapping, false, true)) {
ConnectorConfiguration conf = client.getConnectorConfiguration();
failed.put(conf.getIdent(), conf.getName());
}
;
} else {
client.updateActiveSubscriptionOutbound(updatedMapping);
}
}

if (failed.size() > 0) {
// configurationRegistry.getC8yAgent().createEvent("Activation of mapping: " +
// updatedMapping.name,
// C8YAgent.STATUS_MAPPING_ACTIVATION_ERROR_EVENT_TYPE,
// DateTime.now(),
// configurationRegistry.getMappingServiceRepresentations().get(tenant),
// tenant,
// failed);
return new ResponseEntity<Map<String, String>>(failed, HttpStatus.BAD_REQUEST);
}

} else if (operation.getOperation().equals(Operation.DEBUG_MAPPING)) {
String id = operation.getParameter().get("id");
Boolean debugBoolean = Boolean.parseBoolean(operation.getParameter().get("debug"));
Expand Down
12 changes: 2 additions & 10 deletions dynamic-mapping-ui/src/mapping/core/mapping.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ import {
map,
shareReplay,
switchMap,
take,
tap
take
} from 'rxjs';
import {
BASE_URL,
Expand Down Expand Up @@ -69,7 +68,6 @@ import { C8YNotificationSubscription } from '../shared/mapping.model';
import {
AlertService,
EventRealtimeService,
RealtimeMessage,
RealtimeSubjectService
} from '@c8y/ngx-components';
import { ConnectorConfigurationService } from '../../connector';
Expand Down Expand Up @@ -111,13 +109,7 @@ export class MappingService {
reloadOutbound$: Subject<void>; // = new Subject<void>();

async changeActivationMapping(parameter: any) {
const conf = await this.brokerConnectorService.getConnectorConfigurations();
if (parameter.active && conf.length == 0) {
this.alertService.warning(
'Mapping was activated, but no connector is configured. Please add connector on the Connector tab!'
);
}
await this.sharedService.runOperation(
return await this.sharedService.runOperation(
Operation.ACTIVATE_MAPPING,
parameter
);
Expand Down
13 changes: 11 additions & 2 deletions dynamic-mapping-ui/src/mapping/grid/mapping.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,18 @@ export class MappingComponent implements OnInit, OnDestroy {
const { mapping } = m;
const newActive = !mapping.active;
const action = newActive ? 'Activated' : 'Deactivated';
this.alertService.success(`${action} mapping: ${mapping.id}`);
const parameter = { id: mapping.id, active: newActive };
await this.mappingService.changeActivationMapping(parameter);
const response =
await this.mappingService.changeActivationMapping(parameter);
if (response.status != 200) {
const failedMap = await response.json();
const failedList = Object.values(failedMap).join(',');
this.alertService.warning(
`Mapping could only activate partially. It failed for the following connectors: ${failedList}`
);
} else {
this.alertService.success(`${action} mapping: ${mapping.id}`);
}
this.mappingService.refreshMappings(this.stepperConfiguration.direction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ export class StatusActivationRendererComponent {
const { mapping } = this.context.item;
const newActive = !mapping.active;
const action = newActive ? 'Activated' : 'Deactivated';
this.alertService.success(`${action} mapping: ${mapping.id}`);
const parameter = { id: mapping.id, active: newActive };
await this.mappingService.changeActivationMapping(parameter);
const response =
await this.mappingService.changeActivationMapping(parameter);
if (response.status != 200) {
const failedMap = await response.json();
const failedList = Object.values(failedMap).join(',');
this.alertService.warning(
`Mapping could only activate partially. It failed for the following connectors: ${failedList}`
);
} else {
this.alertService.success(`${action} mapping: ${mapping.id}`);
}
this.mappingService.refreshMappings(Direction.INBOUND);
this.mappingService.refreshMappings(Direction.OUTBOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ export enum StatusEventTypes {
STATUS_MAPPING_CHANGED_EVENT_TYPE = 'd11r_mappingChangedEvent',
STATUS_SUBSCRIPTION_EVENT_TYPE = 'd11r_subscriptionEvent',
STATUS_NOTIFICATION_EVENT_TYPE = 'd11r_notificationStatusEvent',
STATUS_MAPPING_ACTIVATION_ERROR_EVENT_TYPE = 'STATUS_MAPPING_ACTIVATION_ERROR_EVENT_TYPE',
ALL = 'ALL'
}

0 comments on commit f37be37

Please sign in to comment.