/*
* Copyright © 2020 FRINX s.r.o. and others. All rights reserved.
* Copyright © 2021 PANTHEON.tech, s.r.o.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.restconf.nb.rfc8040.utils.parser;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.nb.rfc8040.FieldsParam;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
/**
* A translator between {@link FieldsParam} and {@link YangInstanceIdentifier}s suitable for use as field identifiers
* in {@code netconf-dom-api}.
*
*
* Fields parser that stores set of {@link LinkedPathElement}s in each level. Using {@link LinkedPathElement} it is
* possible to create a chain of path arguments and build complete paths since this element contains identifiers of
* intermediary mixin nodes and also linked previous element.
*
*
* Example: field 'a(/b/c);d/e' ('e' is place under choice node 'x') is parsed into following levels:
*
* level 0: ['./a', './d']
* level 1: ['a/b', '/d/x/e']
* level 2: ['b/c']
*
*/
public final class NetconfFieldsTranslator extends AbstractFieldsTranslator {
private static final NetconfFieldsTranslator INSTANCE = new NetconfFieldsTranslator();
private NetconfFieldsTranslator() {
// Hidden on purpose
}
/**
* Translate a {@link FieldsParam} to a list of child node paths saved in lists, suitable for use with
* {@link NetconfDataTreeService}.
*
* @param identifier identifier context created from request URI
* @param input input value of fields parameter
* @return {@link List} of {@link YangInstanceIdentifier} that are relative to the last {@link PathArgument}
* of provided {@code identifier}
*/
public static @NonNull List translate(
final @NonNull InstanceIdentifierContext> identifier, final @NonNull FieldsParam input) {
final List> levels = INSTANCE.parseFields(identifier, input);
final List