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);
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations