.flatMap(module -> getRpcDefinitionFromModule(module, xmlNamespace, netconfOperationName));
if (rpcDef.isEmpty()) {
LOG.debug("Cannot handle rpc: {}, {}", netconfOperationName, namespace);
- return HandlingPriority.CANNOT_HANDLE;
+ return null;
}
return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
}
package org.opendaylight.netconf.server.mdsal.operations;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
-import com.google.common.base.Preconditions;
import com.google.common.io.CharSource;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-output.xml");
final HandlingPriority priority = rpc.canHandle(rpcDocument);
- Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+ assertNotNull(priority);
final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
final HandlingPriority priority = rpc.canHandle(rpcDocument);
- Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+ assertNotNull(priority);
final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid-control.xml"));
final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container.xml");
final HandlingPriority priority = rpc.canHandle(rpcDocument);
- Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+ assertNotNull(priority);
final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
verifyResponse(response, XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-container-control.xml"));
final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-nonvoid.xml");
final HandlingPriority priority = rpc.canHandle(rpcDocument);
- Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+ assertNotNull(priority);
final DocumentedException e = assertThrows(DocumentedException.class,
() -> rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT));
final Document rpcDocument = XmlFileLoader.xmlFileToDocument("messages/mapping/rpcs/rpc-void-input-output.xml");
final HandlingPriority priority = rpc.canHandle(rpcDocument);
- Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
+ assertNotNull(priority);
final Document response = rpc.handle(rpcDocument, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
- ? getHandlingPriority()
- : HandlingPriority.CANNOT_HANDLE;
+ ? getHandlingPriority() : null;
}
public static final class OperationNameAndNamespace {
NamespaceURN.BASE);
}
- protected HandlingPriority getHandlingPriority() {
+ protected @NonNull HandlingPriority getHandlingPriority() {
return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
}
package org.opendaylight.netconf.server.api.operations;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
-import com.google.common.base.MoreObjects;
-import java.util.Objects;
-import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
-public final class HandlingPriority implements Comparable<HandlingPriority> {
- // FIXME: remote this constant
- public static final HandlingPriority CANNOT_HANDLE = new HandlingPriority(null);
- public static final HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY = new HandlingPriority(Integer.MIN_VALUE);
- public static final HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
-
- private final Integer priority;
-
- private HandlingPriority(final Integer priority) {
- this.priority = priority;
- }
-
- public static @NonNull HandlingPriority of(final int priority) {
- return new HandlingPriority(priority);
- }
-
- /**
- * Get priority number.
- *
- * @return priority number or Optional.absent otherwise
- */
- public Optional<Integer> getPriority() {
- return Optional.ofNullable(priority);
- }
+public record HandlingPriority(int priority) implements Comparable<HandlingPriority> {
+ public static final @NonNull HandlingPriority HANDLE_WITH_DEFAULT_PRIORITY =
+ new HandlingPriority(Integer.MIN_VALUE);
+ public static final @NonNull HandlingPriority HANDLE_WITH_MAX_PRIORITY = new HandlingPriority(Integer.MAX_VALUE);
public HandlingPriority increasePriority(final int priorityIncrease) {
- checkState(priority != null, "Unable to increase priority for %s", this);
checkArgument(priorityIncrease > 0, "Negative increase");
checkArgument(Long.valueOf(priority) + priorityIncrease < Integer.MAX_VALUE,
"Resulting priority cannot be higher than %s", Integer.MAX_VALUE);
- return of(priority + priorityIncrease);
+ return new HandlingPriority(priority + priorityIncrease);
}
@Override
@SuppressWarnings("checkstyle:parameterName")
public int compareTo(final HandlingPriority o) {
- if (priority == null) {
- return o.priority == null ? 0 : -1;
- }
- return o.priority == null ? 1 : Integer.compare(priority, o.priority);
- }
-
- @Override
- public boolean equals(final Object obj) {
- return this == obj || obj instanceof HandlingPriority other && Objects.equals(priority, other.priority);
- }
-
- @Override
- public int hashCode() {
- return Objects.hashCode(priority) ;
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this).add("priority", priority).toString();
+ return Integer.compare(priority, o.priority);
}
}
package org.opendaylight.netconf.server.api.operations;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.netconf.api.DocumentedException;
import org.w3c.dom.Document;
* the results.
*/
public interface NetconfOperation {
-
/**
- * Singleton operations should return
- * HandlingPriority.HANDLE_WITH_MAX_PRIORITY, last operations
- * HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.
+ * Singleton operations should return {@link HandlingPriority#HANDLE_WITH_MAX_PRIORITY}, last operations
+ * {@link HandlingPriority#HANDLE_WITH_DEFAULT_PRIORITY}.
*
* @param message request message
- * @return {@code handling priority}
+ * @return A {@link HandlingPriority}, or {@code null} if the message is not handled
+ * @throws DocumentedException if the message is found to have structural errors
*/
- HandlingPriority canHandle(Document message) throws DocumentedException;
+ @Nullable HandlingPriority canHandle(Document message) throws DocumentedException;
/**
* Execute current netconf operation and trigger execution of subsequent
*/
package org.opendaylight.netconf.server.osgi;
-import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableSet;
private TreeMap<HandlingPriority, NetconfOperation> getSortedNetconfOperationsWithCanHandle(
final Document message, final NetconfServerSession session) throws DocumentedException {
- final TreeMap<HandlingPriority, NetconfOperation> sortedPriority = new TreeMap<>();
-
- for (final NetconfOperation netconfOperation : allNetconfOperations) {
- final HandlingPriority handlingPriority = netconfOperation.canHandle(message);
- if (netconfOperation instanceof DefaultNetconfOperation defaultOperation) {
- defaultOperation.setNetconfSession(session);
- }
- if (netconfOperation instanceof SessionAwareNetconfOperation sessionAwareOperation) {
- sessionAwareOperation.setSession(session);
- }
- if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
-
- checkState(!sortedPriority.containsKey(handlingPriority),
- "Multiple %s available to handle message %s with priority %s, %s and %s",
- NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, sortedPriority
- .get(handlingPriority));
- sortedPriority.put(handlingPriority, netconfOperation);
+ final var sortedPriority = new TreeMap<HandlingPriority, NetconfOperation>();
+ for (var netconfOperation : allNetconfOperations) {
+ final var handlingPriority = netconfOperation.canHandle(message);
+ if (handlingPriority != null) {
+ final var existing = sortedPriority.putIfAbsent(handlingPriority, netconfOperation);
+ if (existing != null) {
+ throw new IllegalStateException(
+ "Multiple %s available to handle message %s with priority %s, %s and %s".formatted(
+ NetconfOperation.class.getName(), message, handlingPriority, netconfOperation, existing));
+ }
+
+ if (netconfOperation instanceof DefaultNetconfOperation defaultOperation) {
+ defaultOperation.setNetconfSession(session);
+ }
+ if (netconfOperation instanceof SessionAwareNetconfOperation sessionAwareOperation) {
+ sessionAwareOperation.setSession(session);
+ }
}
}
return sortedPriority;
}
}
- assertEquals(CONCURRENCY, testingNetconfOperation.getMessageCount());
+ assertEquals(CONCURRENCY, testingNetconfOperation.counter.get());
}
public static Set<String> getOnlyExiServerCaps() {
@Override
public HandlingPriority canHandle(final Document message) {
return XmlUtil.toString(message).contains(NetconfStartExiMessageProvider.START_EXI)
- ? HandlingPriority.CANNOT_HANDLE :
- HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
+ ? null : HandlingPriority.HANDLE_WITH_MAX_PRIORITY;
}
@SuppressWarnings("checkstyle:IllegalCatch")
@Override
public Document handle(final Document requestMessage,
final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
+ LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage));
+ counter.getAndIncrement();
try {
- LOG.info("Handling netconf message from test {}", XmlUtil.toString(requestMessage));
- counter.getAndIncrement();
return XmlUtil.readXmlToDocument("<test/>");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
-
- public long getMessageCount() {
- return counter.get();
- }
}
/**
package org.opendaylight.netconf.server.api.operations;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
@Override
protected String getOperationName() {
- return null;
+ return "edit-config";
}
@Override
public void testAbstractNetconfOperation() throws Exception {
Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/edit_config.xml");
assertEquals(new SessionIdType(Uint32.ONE), netconfOperation.sessionId());
- assertNotNull(netconfOperation.canHandle(helloMessage));
- assertEquals(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY, netconfOperation.getHandlingPriority());
+ assertEquals(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY, netconfOperation.canHandle(helloMessage));
netconfOperation.handle(helloMessage, operation);
assertTrue(netconfOperation.handleRun);
public void testHandlingPriority() {
assertEquals(0,
HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.compareTo(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY));
- assertEquals(-1, HandlingPriority.CANNOT_HANDLE.compareTo(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY));
- assertEquals(1, HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.compareTo(HandlingPriority.CANNOT_HANDLE));
assertEquals(-1,
HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.compareTo(HandlingPriority.HANDLE_WITH_MAX_PRIORITY));
assertEquals(1,
HandlingPriority.HANDLE_WITH_MAX_PRIORITY.compareTo(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY));
- assertEquals(0, HandlingPriority.of(Integer.MIN_VALUE)
+ assertEquals(0, new HandlingPriority(Integer.MIN_VALUE)
.compareTo(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY));
- HandlingPriority prio = HandlingPriority.of(10);
- assertTrue(prio.increasePriority(1).compareTo(HandlingPriority.of(11)) == 0);
+ HandlingPriority prio = new HandlingPriority(10);
+ assertTrue(prio.increasePriority(1).compareTo(new HandlingPriority(11)) == 0);
- assertFalse(HandlingPriority.CANNOT_HANDLE.getPriority().isPresent());
assertFalse(HandlingPriority.HANDLE_WITH_MAX_PRIORITY.equals(new Object()));
- assertEquals(HandlingPriority.HANDLE_WITH_MAX_PRIORITY, HandlingPriority.of(Integer.MAX_VALUE));
+ assertEquals(HandlingPriority.HANDLE_WITH_MAX_PRIORITY, new HandlingPriority(Integer.MAX_VALUE));
assertEquals(HandlingPriority.HANDLE_WITH_MAX_PRIORITY.hashCode(),
- HandlingPriority.of(Integer.MAX_VALUE).hashCode());
+ new HandlingPriority(Integer.MAX_VALUE).hashCode());
}
}