Search in sources :

Example 1 with ModifyAction

use of org.opendaylight.netconf.api.ModifyAction in project netconf by opendaylight.

the class NetconfMessageTransformUtil method createEditConfigAnyxml.

/**
 * Create edit-config structure to invoke {@code operation} with {@code lastChildOverride} data on {@code dataPath}.
 *
 * @param ctx {@link EffectiveModelContext} device's model context
 * @param dataPath {@link YangInstanceIdentifier} path to data in device's data-store
 * @param operation Optional of {@link ModifyAction} action to be invoked
 * @param lastChildOverride Optional of {@code NormalizedNode} data on which action will be invoked
 * @return {@link DOMSourceAnyxmlNode} containing edit-config structure
 */
public static DOMSourceAnyxmlNode createEditConfigAnyxml(final EffectiveModelContext ctx, final YangInstanceIdentifier dataPath, final Optional<ModifyAction> operation, final Optional<NormalizedNode> lastChildOverride) {
    if (dataPath.isEmpty()) {
        Preconditions.checkArgument(lastChildOverride.isPresent(), "Data has to be present when creating structure for top level element");
        Preconditions.checkArgument(lastChildOverride.get() instanceof DataContainerChild, "Data has to be either container or a list node when creating structure for top level element, " + "but was: %s", lastChildOverride.get());
    }
    final var element = XmlUtil.createElement(BLANK_DOCUMENT, NETCONF_CONFIG_QNAME.getLocalName(), Optional.of(NETCONF_CONFIG_QNAME.getNamespace().toString()));
    final var metadata = operation.map(o -> leafMetadata(dataPath, o)).orElse(null);
    try {
        if (lastChildOverride.isPresent()) {
            // FIXME remove ImmutableNodes.fromInstanceId usage
            final var configContent = ImmutableNodes.fromInstanceId(ctx, dataPath, lastChildOverride.get());
            NetconfUtil.writeNormalizedNode(configContent, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
        } else {
            NetconfUtil.writeNormalizedNode(dataPath, metadata, new DOMResult(element), SchemaPath.ROOT, ctx);
        }
    } catch (final IOException | XMLStreamException e) {
        throw new IllegalStateException("Unable to serialize edit config content element for path " + dataPath, e);
    }
    return Builders.anyXmlBuilder().withNodeIdentifier(NETCONF_CONFIG_NODEID).withValue(new DOMSource(element)).build();
}
Also used : NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) LoggerFactory(org.slf4j.LoggerFactory) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ErrorSeverity(org.opendaylight.yangtools.yang.common.ErrorSeverity) XmlElement(org.opendaylight.netconf.api.xml.XmlElement) MessageCounter(org.opendaylight.netconf.sal.connect.util.MessageCounter) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) NETCONF_QNAME(org.opendaylight.netconf.util.NetconfUtil.NETCONF_QNAME) NetconfState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) FailedNetconfMessage(org.opendaylight.netconf.api.FailedNetconfMessage) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) URI(java.net.URI) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) DOMDataTreeIdentifier(org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier) XmlUtil(org.opendaylight.netconf.api.xml.XmlUtil) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) NodeWithValue(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Instant(java.time.Instant) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Builder(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata.Builder) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) CreateSubscriptionInput(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) SchemaOrderedNormalizedNodeWriter(org.opendaylight.yangtools.yang.data.impl.schema.SchemaOrderedNormalizedNodeWriter) YangConstants(org.opendaylight.yangtools.yang.common.YangConstants) Entry(java.util.Map.Entry) Optional(java.util.Optional) NormalizedMetadata(org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata) NonNull(org.eclipse.jdt.annotation.NonNull) DOMSource(javax.xml.transform.dom.DOMSource) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) NETCONF_DATA_QNAME(org.opendaylight.netconf.util.NetconfUtil.NETCONF_DATA_QNAME) NetconfDocumentedException(org.opendaylight.netconf.api.NetconfDocumentedException) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) ImmutableNormalizedMetadata(org.opendaylight.yangtools.rfc7952.data.util.ImmutableNormalizedMetadata) Deque(java.util.Deque) DataSchemaContextNode(org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode) ModifyAction(org.opendaylight.netconf.api.ModifyAction) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) ImmutableList(com.google.common.collect.ImmutableList) DocumentedException(org.opendaylight.netconf.api.DocumentedException) SimpleEntry(java.util.AbstractMap.SimpleEntry) Builders(org.opendaylight.yangtools.yang.data.impl.schema.Builders) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) AnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.AnyxmlNode) IOException(java.io.IOException) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) DataSchemaContextTree(org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) Element(org.w3c.dom.Element) EditContent(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.edit.config.input.EditContent) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) NetconfCapabilityChange(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) NetconfNotification(org.opendaylight.netconf.notifications.NetconfNotification) Preconditions(com.google.common.base.Preconditions) ArrayDeque(java.util.ArrayDeque) DOMResult(javax.xml.transform.dom.DOMResult) NetconfMessageUtil(org.opendaylight.netconf.util.messages.NetconfMessageUtil) RpcError(org.opendaylight.yangtools.yang.common.RpcError) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) IOException(java.io.IOException)

Example 2 with ModifyAction

use of org.opendaylight.netconf.api.ModifyAction in project netconf by opendaylight.

the class SplittingNormalizedNodeMetadataStreamWriter method metadata.

@Override
public void metadata(final ImmutableMap<QName, Object> metadata) throws IOException {
    final Object operation = metadata.get(OPERATION_ATTRIBUTE);
    if (operation != null) {
        checkState(operation instanceof String, "Unexpected operation attribute value %s", operation);
        final ModifyAction newAction = ModifyAction.fromXmlValue((String) operation);
        currentAction = newAction;
    }
    writer.metadata(filterMeta(metadata));
}
Also used : ModifyAction(org.opendaylight.netconf.api.ModifyAction)

Example 3 with ModifyAction

use of org.opendaylight.netconf.api.ModifyAction in project netconf by opendaylight.

the class SplittingNormalizedNodeMetadataStreamWriter method endNode.

@Override
public void endNode() throws IOException {
    final ModifyAction prevAction = actions.peek();
    if (prevAction != null) {
        // a remove/delete operation
        if (prevAction != currentAction && deleteDepth == 0) {
            dataTreeChanges.add(new DataTreeChange(writer.build(), currentAction, currentPath));
        } else {
            writer.endNode();
        }
        popPath();
    } else {
        // All done, special-cased
        LOG.debug("All done ... writer {}", writer);
        writer.endNode();
        dataTreeChanges.add(new DataTreeChange(result.getResult(), currentAction, currentPath));
    }
}
Also used : ModifyAction(org.opendaylight.netconf.api.ModifyAction)

Example 4 with ModifyAction

use of org.opendaylight.netconf.api.ModifyAction in project netconf by opendaylight.

the class EditConfig method handleWithNoSubsequentOperations.

@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
    final XmlElement targetElement = extractTargetElement(operationElement, OPERATION_NAME);
    final Datastore targetDatastore = Datastore.valueOf(targetElement.getName());
    if (targetDatastore == Datastore.running) {
        throw new DocumentedException("edit-config on running datastore is not supported", ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
    }
    final ModifyAction defaultAction = getDefaultOperation(operationElement);
    final XmlElement configElement = getConfigElement(operationElement);
    for (final XmlElement element : configElement.getChildElements()) {
        final SplittingNormalizedNodeMetadataStreamWriter writer = new SplittingNormalizedNodeMetadataStreamWriter(defaultAction);
        parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element, writer);
        executeOperations(writer.getDataTreeChanges());
    }
    return document.createElement(XmlNetconfConstants.OK);
}
Also used : DocumentedException(org.opendaylight.netconf.api.DocumentedException) ModifyAction(org.opendaylight.netconf.api.ModifyAction) XmlElement(org.opendaylight.netconf.api.xml.XmlElement)

Aggregations

ModifyAction (org.opendaylight.netconf.api.ModifyAction)4 DocumentedException (org.opendaylight.netconf.api.DocumentedException)2 XmlElement (org.opendaylight.netconf.api.xml.XmlElement)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Instant (java.time.Instant)1 DateTimeParseException (java.time.format.DateTimeParseException)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayDeque (java.util.ArrayDeque)1 Collection (java.util.Collection)1 Deque (java.util.Deque)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1