+ public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec<IdentityValuesDTO> {
+ private final Logger logger = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class);
+ private final MountInstance mountPoint;
+
+ public InstanceIdentifierCodecImpl(MountInstance mountPoint) {
+ this.mountPoint = mountPoint;
+ }
+
+ @Override
+ public IdentityValuesDTO serialize(InstanceIdentifier data) {
+ List<PathArgument> pathArguments = data.getPath();
+ IdentityValuesDTO identityValuesDTO = new IdentityValuesDTO();
+ for (PathArgument pathArgument : pathArguments) {
+ IdentityValue identityValue = qNameToIdentityValue(pathArgument.getNodeType());
+ if (pathArgument instanceof NodeIdentifierWithPredicates && identityValue != null) {
+ List<Predicate> predicates = keyValuesToPredicateList(((NodeIdentifierWithPredicates) pathArgument)
+ .getKeyValues());
+ identityValue.setPredicates(predicates);
+ } else if (pathArgument instanceof NodeWithValue && identityValue != null) {
+ List<Predicate> predicates = new ArrayList<>();
+ String value = String.valueOf(((NodeWithValue) pathArgument).getValue());
+ predicates.add(new Predicate(null, value));
+ identityValue.setPredicates(predicates);
+ }
+ identityValuesDTO.add(identityValue);
+ }
+ return identityValuesDTO;
+ }
+
+ @Override
+ public InstanceIdentifier deserialize(IdentityValuesDTO data) {
+ List<PathArgument> result = new ArrayList<PathArgument>();
+ IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
+ Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), mountPoint);
+ if (module == null) {
+ logger.info("Module by namespace '{}' of first node in instance-identiefier was not found.", valueWithNamespace.getNamespace());
+ logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(valueWithNamespace.getValue()));
+ return null;
+ }
+
+ DataNodeContainer parentContainer = module;
+ List<IdentityValue> identities = data.getValuesWithNamespaces();
+ for (int i = 0; i < identities.size(); i++) {
+ IdentityValue identityValue = identities.get(i);
+ URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), mountPoint);
+ DataSchemaNode node = ControllerContext.getInstance().findInstanceDataChildByNameAndNamespace(
+ parentContainer, identityValue.getValue(), validNamespace);
+ if (node == null) {
+ logger.info("'{}' node was not found in {}", identityValue, parentContainer.getChildNodes());
+ logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue()));
+ return null;
+ }
+ QName qName = node.getQName();
+ PathArgument pathArgument = null;
+ if (identityValue.getPredicates().isEmpty()) {
+ pathArgument = new NodeIdentifier(qName);
+ } else {
+ if (node instanceof LeafListSchemaNode) { // predicate is value of leaf-list entry
+ Predicate leafListPredicate = identityValue.getPredicates().get(0);
+ if (!leafListPredicate.isLeafList()) {
+ logger.info("Predicate's data is not type of leaf-list. It should be in format \".='value'\"");
+ logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue()));
+ return null;
+ }
+ pathArgument = new NodeWithValue(qName, leafListPredicate.getValue());
+ } else if (node instanceof ListSchemaNode) { // predicates are keys of list
+ DataNodeContainer listNode = (DataNodeContainer) node;
+ Map<QName, Object> predicatesMap = new HashMap<>();
+ for (Predicate predicate : identityValue.getPredicates()) {
+ validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), mountPoint);
+ DataSchemaNode listKey = ControllerContext.getInstance().findInstanceDataChildByNameAndNamespace(
+ listNode, predicate.getName().getValue(), validNamespace);
+ predicatesMap.put(listKey.getQName(), predicate.getValue());
+ }
+ pathArgument = new NodeIdentifierWithPredicates(qName, predicatesMap);
+ } else {
+ logger.info("Node {} is not List or Leaf-list.", node);
+ logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue()));
+ return null;
+ }
+ }
+ result.add(pathArgument);
+ if (i < identities.size() - 1) { // last element in instance-identifier can be other than DataNodeContainer
+ if (node instanceof DataNodeContainer) {
+ parentContainer = (DataNodeContainer) node;
+ } else {
+ logger.info("Node {} isn't instance of DataNodeContainer", node);
+ logger.info("Instance-identifier will be translated as NULL for data - {}", String.valueOf(identityValue.getValue()));
+ return null;
+ }
+ }
+ }
+
+ return result.isEmpty() ? null : new InstanceIdentifier(result);
+ }
+
+ private List<Predicate> keyValuesToPredicateList(Map<QName, Object> keyValues) {
+ List<Predicate> result = new ArrayList<>();
+ for (QName qName : keyValues.keySet()) {
+ Object value = keyValues.get(qName);
+ result.add(new Predicate(qNameToIdentityValue(qName), String.valueOf(value)));
+ }
+ return result;
+ }
+
+ private IdentityValue qNameToIdentityValue(QName qName) {
+ if (qName != null) {
+ return new IdentityValue(qName.getNamespace().toString(), qName.getLocalName(), qName.getPrefix());
+ }
+ return null;
+ }
+ }
+
+ private static Module getModuleByNamespace(String namespace, MountInstance mountPoint) {
+ URI validNamespace = resolveValidNamespace(namespace, mountPoint);
+
+ Module module = null;
+ if (mountPoint != null) {
+ module = ControllerContext.getInstance().findModuleByNamespace(mountPoint, validNamespace);
+ } else {
+ module = ControllerContext.getInstance().findModuleByNamespace(validNamespace);
+ }
+ if (module == null) {
+ logger.info("Module for namespace " + validNamespace + " wasn't found.");
+ return null;
+ }
+ return module;
+ }
+
+ private static URI resolveValidNamespace(String namespace, MountInstance mountPoint) {
+ URI validNamespace;
+ if (mountPoint != null) {
+ validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace);
+ } else {
+ validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace);
+ }
+ if (validNamespace == null) {
+ validNamespace = URI.create(namespace);
+ }
+
+ return validNamespace;
+ }
+