Search in sources :

Example 11 with EdgeConnectorForKVSException

use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class EdgeConnectorForKVSService method main.

public static void main(String[] args) throws InterruptedException {
    log.info("---------- EdgeConnectorForKVS Starting ----------");
    boolean retry = true;
    boolean isInitialSetup = true;
    List<String> restartNeededConfigurationList = new ArrayList<>();
    do {
        hubSiteWiseAssetId = args[Constants.ARG_INDEX_SITE_WISE_ASSET_ID_FOR_HUB];
        log.info("EdgeConnectorForKVS Hub Asset Id: " + hubSiteWiseAssetId);
        final EdgeConnectorForKVSService edgeConnectorForKVSService = new EdgeConnectorForKVSService();
        try {
            // set up shared configurations first
            if (isInitialSetup) {
                edgeConnectorForKVSService.setUpSharedEdgeConnectorForKVSService();
                restartNeededConfigurationList.addAll(edgeConnectorForKVSConfigurationList.stream().map(EdgeConnectorForKVSConfiguration::getKinesisVideoStreamName).collect(Collectors.toList()));
                isInitialSetup = false;
            }
            if (restartNeededConfigurationList == null || restartNeededConfigurationList.isEmpty()) {
                throw new EdgeConnectorForKVSUnrecoverableException("Unable to initialize component");
            }
            // initialize or re-init camera level configurations
            edgeConnectorForKVSConfigurationList.forEach(configuration -> {
                if (restartNeededConfigurationList.contains(configuration.kinesisVideoStreamName)) {
                    edgeConnectorForKVSService.setUpCameraLevelEdgeConnectorForKVSService(configuration);
                }
            });
            // clear this configuration list after each restart or initial setup
            restartNeededConfigurationList.clear();
            // block main thread and regularly check camera level health status
            while (true) {
                for (EdgeConnectorForKVSConfiguration configuration : edgeConnectorForKVSConfigurationList) {
                    if (configuration.getFatalStatus().get()) {
                        log.info("fatal status found for " + configuration.getKinesisVideoStreamName());
                        restartNeededConfigurationList.add(configuration.kinesisVideoStreamName);
                    }
                }
                if (!restartNeededConfigurationList.isEmpty()) {
                    // fatal status was set, throw an exception to trigger restart
                    throw new EdgeConnectorForKVSException("Fatal error reported");
                }
                Thread.sleep(WAIT_TIME_BEFORE_POLLING_IN_MILLISECS);
            }
        } catch (final EdgeConnectorForKVSException ex) {
            log.error("Start kicking off camera level restart: " + "Failed {}: {}", ex.getClass().getName(), ex.getMessage());
        } catch (final EdgeConnectorForKVSUnrecoverableException ex) {
            log.error("Unrecoverable exception caught, please re-deploy or restart the Component." + "ERROR: Failed {}: {}", ex.getClass().getName(), ex.getMessage());
            retry = false;
        } catch (final Exception ex) {
            log.error("Uncaught exception found, please re-deploy or restart the Component." + "ERROR: Failed {}: {}", ex.getClass().getName(), ex.getMessage());
            retry = false;
        }
        // clear fatalStatus and clean up failed cameras
        clearFatalStatus();
        edgeConnectorForKVSConfigurationList.forEach(configuration -> {
            if (restartNeededConfigurationList.contains(configuration.kinesisVideoStreamName)) {
                edgeConnectorForKVSService.cleanUpEdgeConnectorForKVSService(configuration);
            }
        });
        if (retry) {
            // If the fatalStatus is true after camera level restart, re-initializing the component
            boolean isFatalStatusSet = edgeConnectorForKVSConfigurationList.stream().anyMatch(configuration -> configuration.getFatalStatus().get());
            if (isFatalStatusSet) {
                log.info("---------- Component Re-initializing ----------");
                edgeConnectorForKVSService.cleanUpEdgeConnectorForKVSService(null);
                restartNeededConfigurationList.clear();
                clearFatalStatus();
                isInitialSetup = true;
            } else {
                log.info("---------- Camera Re-starting ----------");
            // wait for a bit before restarting
            }
            Thread.sleep(WAIT_TIME_BEFORE_RESTART_IN_MILLISECS);
        }
    } while (retry);
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) ArrayList(java.util.ArrayList) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) TimeoutException(java.util.concurrent.TimeoutException) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with EdgeConnectorForKVSException

use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class SiteWiseClient method isAssetInheritedFromAssetModel.

/**
 * Check whether given siteWiseAssetId belongs to given siteWiseAssetModel. Return true if asset's model contains
 * given siteWiseAssetModelPrefix, otherwise return false.
 * @param siteWiseAssetId siteWiseAssetId
 * @param siteWiseAssetModelPrefix siteWiseAssetModelPrefix
 * @return true|false
 * @throws EdgeConnectorForKVSException - EdgeConnectorForKVS generic exception
 */
public boolean isAssetInheritedFromAssetModel(@NonNull String siteWiseAssetId, @NonNull String siteWiseAssetModelPrefix) throws EdgeConnectorForKVSException {
    final DescribeAssetResponse describeAssetResponse;
    final String assetModelId;
    try {
        describeAssetResponse = describeAsset(siteWiseAssetId);
        assetModelId = describeAssetResponse.assetModelId();
        if (assetModelId != null) {
            DescribeAssetModelResponse describeAssetModelResponse = describeAssetModel(assetModelId);
            if (describeAssetModelResponse.assetModelName() != null) {
                return describeAssetModelResponse.assetModelName().startsWith(siteWiseAssetModelPrefix);
            } else {
                log.error(String.format("Could not find the model name for given siteWise assetId and siteWise " + "assetModel Id. Return false. siteWiseAssetId : %s, siteWiseAssetModelId : %s", siteWiseAssetId, assetModelId));
                return false;
            }
        } else {
            final String errorMessage = String.format("Could not find the model ID for given siteWiseAssetId. " + "Return false. siteWiseAssetId : %s", siteWiseAssetId);
            log.error(errorMessage);
            return false;
        }
    } catch (Exception e) {
        final String errorMessage = String.format("Failed to in isAssetBelongsAssetModel API call." + " siteWiseAssetId: %s, siteWiseAssetModelPrefix : %s", siteWiseAssetId, siteWiseAssetModelPrefix);
        log.error(errorMessage, e);
        throw new EdgeConnectorForKVSException(errorMessage, e);
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) DescribeAssetResponse(software.amazon.awssdk.services.iotsitewise.model.DescribeAssetResponse) DescribeAssetModelResponse(software.amazon.awssdk.services.iotsitewise.model.DescribeAssetModelResponse) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)

Example 13 with EdgeConnectorForKVSException

use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class SiteWiseClient method listAssociatedAssets.

/**
 * Return all Associated asset Id as String list
 * @param siteWiseAssetId siteWiseAssetId
 * @return List which contains associated asset id
 * @throws EdgeConnectorForKVSException - EdgeConnectorForKVS generic exception
 */
public List<String> listAssociatedAssets(@NonNull String siteWiseAssetId) throws EdgeConnectorForKVSException {
    try {
        List<String> assetHierarchiesIdList = getAssetHierarchiesIdList(siteWiseAssetId);
        List<String> result = new ArrayList<>();
        for (String hierarchiesId : assetHierarchiesIdList) {
            final ListAssociatedAssetsRequest listAssociatedAssetsRequest = ListAssociatedAssetsRequest.builder().assetId(siteWiseAssetId).hierarchyId(hierarchiesId).build();
            ListAssociatedAssetsResponse listAssociatedAssetsResponse = siteWiseClient.listAssociatedAssets(listAssociatedAssetsRequest);
            result.addAll(listAssociatedAssetsResponse.assetSummaries().stream().map(AssociatedAssetsSummary::id).collect(Collectors.toList()));
        }
        return result;
    } catch (Exception e) {
        final String errorMessage = String.format("Failed to getAssetHierarchiesIdList from given siteWiseAssetId, " + "please check EdgeConnectorForKVS configuration and ensure given provide SiteWise property " + "generated from EdgeConnectorForKVSHubDeviceModel. siteWiseAssetId : %s", siteWiseAssetId);
        log.error(errorMessage);
        throw new EdgeConnectorForKVSException(errorMessage);
    }
}
Also used : EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) ArrayList(java.util.ArrayList) ListAssociatedAssetsRequest(software.amazon.awssdk.services.iotsitewise.model.ListAssociatedAssetsRequest) AssociatedAssetsSummary(software.amazon.awssdk.services.iotsitewise.model.AssociatedAssetsSummary) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) ListAssociatedAssetsResponse(software.amazon.awssdk.services.iotsitewise.model.ListAssociatedAssetsResponse)

Example 14 with EdgeConnectorForKVSException

use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class SiteWiseManager method initEdgeConnectorForKVSServiceConfiguration.

/**
 * Init EdgeConnectorForKVS Service configuration.
 * This method will call SiteWise service, query the asset property value to init the EdgeConnectorForKVS config.
 * @param hubDeviceSiteWiseAssetId The SiteWise asset Id for EdgeConnectorForKVS hub device.
 * @throws EdgeConnectorForKVSUnrecoverableException when there are issues querying SiteWise service
 * @return List of EdgeConnectorForKVSConfiguration
 */
public List<EdgeConnectorForKVSConfiguration> initEdgeConnectorForKVSServiceConfiguration(String hubDeviceSiteWiseAssetId) {
    try {
        verifyConfiguration(hubDeviceSiteWiseAssetId);
        List<String> cameraSiteWiseAssetIdList = siteWiseClient.listAssociatedAssets(hubDeviceSiteWiseAssetId);
        if (cameraSiteWiseAssetIdList.size() > 0) {
            return cameraSiteWiseAssetIdList.stream().map(cameraSiteWiseAssetId -> {
                try {
                    return buildEdgeConnectorForKVSConfiguration(cameraSiteWiseAssetId);
                } catch (Exception e) {
                    throw new EdgeConnectorForKVSException(e);
                }
            }).collect(Collectors.toList());
        } else {
            final String warnMessage = String.format("Could not find any camera asset under the given hub assetId." + " Please check the provided hub device SiteWise assetId: %s", hubDeviceSiteWiseAssetId);
            log.warn(warnMessage);
            return new ArrayList<>();
        }
    } catch (Exception e) {
        final String errorMessage = String.format("Failed to init EdgeConnectorForKVS component. " + "Please check the provided hub device SiteWise assetId: %s", hubDeviceSiteWiseAssetId);
        log.error(errorMessage, e);
        throw new EdgeConnectorForKVSUnrecoverableException(errorMessage, e);
    }
}
Also used : EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) DescribeAssetResponse(software.amazon.awssdk.services.iotsitewise.model.DescribeAssetResponse) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GetAssetPropertyValueResponse(software.amazon.awssdk.services.iotsitewise.model.GetAssetPropertyValueResponse) ArrayList(java.util.ArrayList) AssetProperty(software.amazon.awssdk.services.iotsitewise.model.AssetProperty) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Builder(lombok.Builder) Constants(com.aws.iot.edgeconnectorforkvs.util.Constants) EdgeConnectorForKVSConfiguration(com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration) AllArgsConstructor(lombok.AllArgsConstructor) PropertyNotificationState(software.amazon.awssdk.services.iotsitewise.model.PropertyNotificationState) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) ArrayList(java.util.ArrayList) EdgeConnectorForKVSUnrecoverableException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)

Example 15 with EdgeConnectorForKVSException

use of com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class DiskManager method setupWatchService.

private void setupWatchService() throws IOException {
    try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
        try {
            edgeConnectorForKVSConfigurationList.forEach(configuration -> {
                System.out.println("Start watching!");
                Path path = configuration.getVideoRecordFolderPath();
                try {
                    path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
                    log.info(String.format("Start watch service for video record path: %s", path.getFileName()));
                } catch (IOException e) {
                    log.error(String.format("Start watch service for video record path: %s failed!", path.toString()));
                    throw new EdgeConnectorForKVSException(e);
                }
            });
            while (true) {
                final WatchKey watchKey = watchService.take();
                final Watchable watchable = watchKey.watchable();
                if (watchable instanceof Path) {
                    final Path directoryPath = (Path) watchable;
                    for (WatchEvent<?> event : watchKey.pollEvents()) {
                        this.callback.handleWatchEvent(directoryPath, event, diskManagerUtil);
                    }
                }
                if (!watchKey.reset()) {
                    log.warn("Disk Management run into issue. Restarting the process...");
                    setupDiskManagerThread();
                    break;
                }
            }
        } catch (InterruptedException e) {
            log.error("Disk Management process interrupted! ");
            Thread.currentThread().interrupt();
        }
    }
}
Also used : Path(java.nio.file.Path) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) Watchable(java.nio.file.Watchable) WatchKey(java.nio.file.WatchKey) IOException(java.io.IOException) WatchService(java.nio.file.WatchService)

Aggregations

EdgeConnectorForKVSException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException)17 ArrayList (java.util.ArrayList)7 EdgeConnectorForKVSConfiguration (com.aws.iot.edgeconnectorforkvs.model.EdgeConnectorForKVSConfiguration)4 Scheduler (org.quartz.Scheduler)4 SchedulerException (org.quartz.SchedulerException)4 IOException (java.io.IOException)3 DescribeAssetResponse (software.amazon.awssdk.services.iotsitewise.model.DescribeAssetResponse)3 StreamManagerException (com.amazonaws.greengrass.streammanager.client.exception.StreamManagerException)2 TestWatchEventCallBack (com.aws.iot.edgeconnectorforkvs.diskmanager.callback.TestWatchEventCallBack)2 EdgeConnectorForKVSUnrecoverableException (com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSUnrecoverableException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 WatchEvent (java.nio.file.WatchEvent)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 Test (org.junit.jupiter.api.Test)2 JobDetail (org.quartz.JobDetail)2 MessageStreamDefinition (com.amazonaws.greengrass.streammanager.model.MessageStreamDefinition)1 ExportDefinition (com.amazonaws.greengrass.streammanager.model.export.ExportDefinition)1 IoTSiteWiseConfig (com.amazonaws.greengrass.streammanager.model.export.IoTSiteWiseConfig)1 AssetPropertyValue (com.amazonaws.greengrass.streammanager.model.sitewise.AssetPropertyValue)1