Search in sources :

Example 71 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project netconf by opendaylight.

the class ControllerContext method addKeyValue.

private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue, final DOMMountPoint mountPoint) {
    checkArgument(node instanceof LeafSchemaNode);
    final EffectiveModelContext schemaContext = mountPoint == null ? globalSchema : getModelContext(mountPoint);
    final String urlDecoded = urlPathArgDecode(requireNonNull(uriValue));
    TypeDefinition<?> typedef = ((LeafSchemaNode) node).getType();
    final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef);
    if (baseType instanceof LeafrefTypeDefinition) {
        typedef = SchemaInferenceStack.ofInstantiatedPath(schemaContext, node.getPath()).resolveLeafref((LeafrefTypeDefinition) baseType);
    }
    final IllegalArgumentCodec<Object, Object> codec = RestCodec.from(typedef, mountPoint, this);
    Object decoded = codec.deserialize(urlDecoded);
    String additionalInfo = "";
    if (decoded == null) {
        if (typedef instanceof IdentityrefTypeDefinition) {
            decoded = toQName(schemaContext, urlDecoded);
            additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name.";
        }
    }
    if (decoded == null) {
        throw new RestconfDocumentedException(uriValue + " from URI can't be resolved. " + additionalInfo, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
    }
    map.put(node.getQName(), decoded);
}
Also used : RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) IdentityrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 72 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project netconf by opendaylight.

the class BrokerFacade method patchConfigurationDataWithinTransaction.

public PatchStatusContext patchConfigurationDataWithinTransaction(final PatchContext patchContext) throws Exception {
    final DOMMountPoint mountPoint = patchContext.getInstanceIdentifierContext().getMountPoint();
    // get new transaction and schema context on server or on mounted device
    final EffectiveModelContext schemaContext;
    final DOMDataTreeReadWriteTransaction patchTransaction;
    if (mountPoint == null) {
        schemaContext = patchContext.getInstanceIdentifierContext().getSchemaContext();
        patchTransaction = this.domDataBroker.newReadWriteTransaction();
    } else {
        schemaContext = modelContext(mountPoint);
        final Optional<DOMDataBroker> optional = mountPoint.getService(DOMDataBroker.class);
        if (optional.isPresent()) {
            patchTransaction = optional.get().newReadWriteTransaction();
        } else {
            // if mount point does not have broker it is not possible to continue and global error is reported
            LOG.error("Http Patch {} has failed - device {} does not support broker service", patchContext.getPatchId(), mountPoint.getIdentifier());
            return new PatchStatusContext(patchContext.getPatchId(), null, false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier())));
        }
    }
    final List<PatchStatusEntity> editCollection = new ArrayList<>();
    List<RestconfError> editErrors;
    boolean withoutError = true;
    for (final PatchEntity patchEntity : patchContext.getData()) {
        final PatchEditOperation operation = patchEntity.getOperation();
        switch(operation) {
            case CREATE:
                if (withoutError) {
                    try {
                        postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case REPLACE:
                if (withoutError) {
                    try {
                        putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case DELETE:
            case REMOVE:
                if (withoutError) {
                    try {
                        deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            case MERGE:
                if (withoutError) {
                    try {
                        mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(), patchEntity.getNode(), schemaContext);
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), true, null));
                    } catch (final RestconfDocumentedException e) {
                        LOG.error("Error call http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                        editErrors = new ArrayList<>();
                        editErrors.addAll(e.getErrors());
                        editCollection.add(new PatchStatusEntity(patchEntity.getEditId(), false, editErrors));
                        withoutError = false;
                    }
                }
                break;
            default:
                LOG.error("Unsupported http Patch operation {} on target {}", operation, patchEntity.getTargetNode().toString());
                break;
        }
    }
    // if errors then cancel transaction and return error status
    if (!withoutError) {
        patchTransaction.cancel();
        return new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, null);
    }
    // if no errors commit transaction
    final CountDownLatch waiter = new CountDownLatch(1);
    final FluentFuture<? extends CommitInfo> future = patchTransaction.commit();
    final PatchStatusContextHelper status = new PatchStatusContextHelper();
    future.addCallback(new FutureCallback<CommitInfo>() {

        @Override
        public void onSuccess(final CommitInfo result) {
            status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), true, null));
            waiter.countDown();
        }

        @Override
        public void onFailure(final Throwable throwable) {
            // if commit failed it is global error
            LOG.error("Http Patch {} transaction commit has failed", patchContext.getPatchId());
            status.setStatus(new PatchStatusContext(patchContext.getPatchId(), ImmutableList.copyOf(editCollection), false, ImmutableList.of(new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, throwable.getMessage()))));
            waiter.countDown();
        }
    }, MoreExecutors.directExecutor());
    waiter.await();
    return status.getStatus();
}
Also used : PatchEditOperation(org.opendaylight.restconf.common.patch.PatchEditOperation) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) PatchEntity(org.opendaylight.restconf.common.patch.PatchEntity) DOMMountPoint(org.opendaylight.mdsal.dom.api.DOMMountPoint) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) DOMDataTreeReadWriteTransaction(org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction) PatchStatusContext(org.opendaylight.restconf.common.patch.PatchStatusContext) PatchStatusEntity(org.opendaylight.restconf.common.patch.PatchStatusEntity) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) CommitInfo(org.opendaylight.mdsal.common.api.CommitInfo) DOMDataBroker(org.opendaylight.mdsal.dom.api.DOMDataBroker) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 73 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project netconf by opendaylight.

the class FilterContentValidator method getKeyValues.

private Map<QName, Object> getKeyValues(final List<String> path, final XmlElement filterContent, final DataSchemaNode parentSchemaNode, final ListSchemaNode listSchemaNode) {
    XmlElement current = filterContent;
    // find list element
    for (final String pathElement : path) {
        final List<XmlElement> childElements = current.getChildElements(pathElement);
        // if there are multiple list entries present in the filter, we can't use any keys and must read whole list
        if (childElements.size() != 1) {
            return Map.of();
        }
        current = childElements.get(0);
    }
    final Map<QName, Object> keys = new HashMap<>();
    final List<QName> keyDefinition = listSchemaNode.getKeyDefinition();
    for (final QName qualifiedName : keyDefinition) {
        final Optional<XmlElement> childElements = current.getOnlyChildElementOptionally(qualifiedName.getLocalName());
        if (childElements.isEmpty()) {
            return Map.of();
        }
        final Optional<String> keyValue = childElements.get().getOnlyTextContentOptionally();
        if (keyValue.isPresent()) {
            final LeafSchemaNode listKey = (LeafSchemaNode) listSchemaNode.getDataChildByName(qualifiedName);
            if (listKey instanceof IdentityrefTypeDefinition) {
                keys.put(qualifiedName, keyValue.get());
            } else {
                final TypeDefinition<? extends TypeDefinition<?>> keyType = listKey.getType();
                if (keyType instanceof IdentityrefTypeDefinition || keyType instanceof LeafrefTypeDefinition) {
                    final Document document = filterContent.getDomElement().getOwnerDocument();
                    final NamespaceContext nsContext = new UniversalNamespaceContextImpl(document, false);
                    final EffectiveModelContext modelContext = schemaContext.getCurrentContext();
                    final XmlCodecFactory xmlCodecFactory = XmlCodecFactory.create(modelContext);
                    final SchemaInferenceStack resolver = SchemaInferenceStack.of(modelContext, Absolute.of(parentSchemaNode.getQName(), listSchemaNode.getQName(), listKey.getQName()));
                    final TypeAwareCodec<?, NamespaceContext, XMLStreamWriter> typeCodec = xmlCodecFactory.codecFor(listKey, resolver);
                    final Object deserializedKeyValue = typeCodec.parseValue(nsContext, keyValue.get());
                    keys.put(qualifiedName, deserializedKeyValue);
                } else {
                    final Object deserializedKey = TypeDefinitionAwareCodec.from(keyType).deserialize(keyValue.get());
                    keys.put(qualifiedName, deserializedKey);
                }
            }
        }
    }
    return keys;
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) IdentityrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition) HashMap(java.util.HashMap) QName(org.opendaylight.yangtools.yang.common.QName) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) Document(org.w3c.dom.Document) LeafrefTypeDefinition(org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition) NamespaceContext(javax.xml.namespace.NamespaceContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) XmlCodecFactory(org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 74 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project netconf by opendaylight.

the class NotificationListenerAdapter method onNotification.

@Override
public void onNotification(final DOMNotification notification) {
    final Instant now = Instant.now();
    if (!checkStartStop(now, this)) {
        return;
    }
    final EffectiveModelContext schemaContext = controllerContext.getGlobalSchema();
    final String xml = prepareXml(schemaContext, notification);
    if (checkFilter(xml)) {
        prepareAndPostData(outputType.equals("JSON") ? prepareJson(schemaContext, notification) : xml);
    }
}
Also used : Instant(java.time.Instant) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 75 with EffectiveModelContext

use of org.opendaylight.yangtools.yang.model.api.EffectiveModelContext in project netconf by opendaylight.

the class InstanceIdentifierTypeLeafTest method stringToInstanceIdentifierTest.

@Test
public void stringToInstanceIdentifierTest() throws Exception {
    final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/instanceidentifier"));
    ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
    final InstanceIdentifierContext<?> instanceIdentifier = controllerContext.toInstanceIdentifier("/iid-value-module:cont-iid/iid-list/%2Fiid-value-module%3Acont-iid%2Fiid-value-module%3A" + "values-iid%5Biid-value-module:value-iid='value'%5D");
    final YangInstanceIdentifier yiD = instanceIdentifier.getInstanceIdentifier();
    Assert.assertNotNull(yiD);
    final PathArgument lastPathArgument = yiD.getLastPathArgument();
    Assert.assertTrue(lastPathArgument.getNodeType().getNamespace().toString().equals("iid:value:module"));
    Assert.assertTrue(lastPathArgument.getNodeType().getLocalName().equals("iid-list"));
    final NodeIdentifierWithPredicates list = (NodeIdentifierWithPredicates) lastPathArgument;
    final YangInstanceIdentifier value = (YangInstanceIdentifier) list.getValue(QName.create(lastPathArgument.getNodeType(), "iid-leaf"));
    final PathArgument lastPathArgumentOfValue = value.getLastPathArgument();
    Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getNamespace().toString().equals("iid:value:module"));
    Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getLocalName().equals("values-iid"));
    final NodeIdentifierWithPredicates valueList = (NodeIdentifierWithPredicates) lastPathArgumentOfValue;
    final String valueIid = (String) valueList.getValue(QName.create(lastPathArgumentOfValue.getNodeType(), "value-iid"));
    Assert.assertEquals("value", valueIid);
}
Also used : ControllerContext(org.opendaylight.netconf.sal.restconf.impl.ControllerContext) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Aggregations

EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)182 Test (org.junit.Test)99 Module (org.opendaylight.yangtools.yang.model.api.Module)37 QName (org.opendaylight.yangtools.yang.common.QName)29 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)28 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)26 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)24 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)24 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)21 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)18 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)16 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)16 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)16 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)14 DOMMountPoint (org.opendaylight.mdsal.dom.api.DOMMountPoint)13 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)13 RpcDefinition (org.opendaylight.yangtools.yang.model.api.RpcDefinition)13 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)11 IOException (java.io.IOException)9 Collection (java.util.Collection)9