+
+ /**
+ * THis method registers a cm handle and intiates modules sync.
+ *
+ * @param dmiPluginRegistration dmi plugin registration information.
+ * @throws JsonProcessingException thrown if json is malformed or missing.
+ */
+ public void parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
+ final DmiPluginRegistration dmiPluginRegistration) throws JsonProcessingException {
+ final PersistenceCmHandlesList createdPersistenceCmHandlesList =
+ getUpdatedPersistenceCmHandlesList(dmiPluginRegistration, dmiPluginRegistration.getCreatedCmHandles());
+ registerAndSyncNewCmHandles(createdPersistenceCmHandlesList);
+ }
+
+ private static Object handleResponse(final ResponseEntity<?> responseEntity,
+ final String exceptionMessage) {
+ if (responseEntity.getStatusCode().is2xxSuccessful()) {
+ return responseEntity.getBody();
+ } else {
+ throw new NcmpException(exceptionMessage,
+ "DMI status code: " + responseEntity.getStatusCodeValue()
+ + ", DMI response body: " + responseEntity.getBody());
+ }
+ }
+
+ private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration)
+ throws JsonProcessingException {
+ final PersistenceCmHandlesList updatedPersistenceCmHandlesList =
+ getUpdatedPersistenceCmHandlesList(dmiPluginRegistration, dmiPluginRegistration.getUpdatedCmHandles());
+ final String cmHandlesAsJson = jsonObjectMapper.asJsonString(updatedPersistenceCmHandlesList);
+ cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
+ "/dmi-registry", cmHandlesAsJson, NO_TIMESTAMP);
+ }
+
+ private PersistenceCmHandlesList getUpdatedPersistenceCmHandlesList(
+ final DmiPluginRegistration dmiPluginRegistration,
+ final List<CmHandle> updatedCmHandles) {
+ return PersistenceCmHandlesList.toPersistenceCmHandlesList(
+ dmiPluginRegistration.getDmiPlugin(),
+ dmiPluginRegistration.getDmiDataPlugin(),
+ dmiPluginRegistration.getDmiModelPlugin(),
+ updatedCmHandles);
+ }
+
+ private static void handleJsonProcessingException(final DmiPluginRegistration dmiPluginRegistration,
+ final JsonProcessingException e) {
+ final String message = "Parsing error occurred while processing DMI Plugin Registration"
+ + dmiPluginRegistration;
+ log.error(message);
+ throw new DataValidationException(message, e.getMessage(), e);
+ }
+
+ private void registerAndSyncNewCmHandles(final PersistenceCmHandlesList persistenceCmHandlesList)
+ throws JsonProcessingException {
+ final String cmHandleJsonData = jsonObjectMapper.asJsonString(persistenceCmHandlesList);
+ cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry",
+ cmHandleJsonData, NO_TIMESTAMP);
+
+ for (final PersistenceCmHandle persistenceCmHandle : persistenceCmHandlesList.getPersistenceCmHandles()) {
+ syncModulesAndCreateAnchor(persistenceCmHandle);
+ }
+ }
+
+ protected void syncModulesAndCreateAnchor(final PersistenceCmHandle persistenceCmHandle) {
+ syncAndCreateSchemaSet(persistenceCmHandle);
+ createAnchor(persistenceCmHandle);
+ }
+
+ private void parseAndRemoveCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
+ for (final String cmHandle : dmiPluginRegistration.getRemovedCmHandles()) {
+ try {
+ attemptToDeleteSchemaSetWithCascade(cmHandle);
+ cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
+ "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
+ } catch (final DataNodeNotFoundException e) {
+ log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
+ }
+ }
+ }
+
+ private void attemptToDeleteSchemaSetWithCascade(final String schemaSetName) {
+ try {
+ cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
+ CASCADE_DELETE_ALLOWED);
+ } catch (final Exception e) {
+ log.warn("Schema set {} delete failed, reason {}", schemaSetName, e.getMessage());
+ }
+ }
+
+ private void syncAndCreateSchemaSet(final PersistenceCmHandle persistenceCmHandle) {
+ final List<ModuleReference> moduleReferencesFromCmHandle =
+ dmiModelOperations.getModuleReferences(persistenceCmHandle);
+ final List<ModuleReference> existingModuleReferences = new ArrayList<>();
+ final List<ModuleReference> unknownModuleReferences = new ArrayList<>();
+ prepareModuleSubsets(moduleReferencesFromCmHandle, existingModuleReferences, unknownModuleReferences);
+
+ final Map<String, String> newYangResourcesModuleNameToContentMap;
+ if (unknownModuleReferences.isEmpty()) {
+ newYangResourcesModuleNameToContentMap = new HashMap<>();
+ } else {
+ newYangResourcesModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(persistenceCmHandle,
+ unknownModuleReferences);
+ }
+ cpsModuleService
+ .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(),
+ newYangResourcesModuleNameToContentMap, existingModuleReferences);
+ }
+
+ private void prepareModuleSubsets(final List<ModuleReference> moduleReferencesFromCmHandle,
+ final List<ModuleReference> existingModuleReferences,
+ final List<ModuleReference> unknownModuleReferences) {
+
+ final Collection<ModuleReference> knownModuleReferencesInCps =
+ cpsModuleService.getYangResourceModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
+
+ for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromCmHandle) {
+ if (knownModuleReferencesInCps.contains(moduleReferenceFromDmiForCmHandle)) {
+ existingModuleReferences.add(moduleReferenceFromDmiForCmHandle);
+ } else {
+ unknownModuleReferences.add(moduleReferenceFromDmiForCmHandle);
+ }
+ }
+ }
+
+ private void createAnchor(final PersistenceCmHandle persistenceCmHandle) {
+ cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, persistenceCmHandle.getId(),
+ persistenceCmHandle.getId());
+ }
+
+
+