package org.opendaylight.controller.netconf.impl.osgi;
import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
-import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
+import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
+import org.opendaylight.controller.netconf.impl.CommitNotifier;
import org.opendaylight.controller.netconf.impl.NetconfServerSession;
-import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCommit;
-import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultGetSchema;
import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultNetconfOperation;
import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStartExi;
import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultStopExi;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
-import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
+import org.opendaylight.controller.netconf.mapping.api.SessionAwareNetconfOperation;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NetconfOperationRouterImpl implements NetconfOperationRouter {
private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
+ private final NetconfOperationService netconfOperationServiceSnapshot;
+ private final Collection<NetconfOperation> allNetconfOperations;
- private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
- private Set<NetconfOperation> allNetconfOperations;
+ public NetconfOperationRouterImpl(final NetconfOperationService netconfOperationServiceSnapshot,
+ final CommitNotifier commitNotifier, final NetconfMonitoringService netconfMonitoringService, final String sessionId) {
+ this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
- private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
- this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
- }
-
- private synchronized void initNetconfOperations(final Set<NetconfOperation> allOperations) {
- allNetconfOperations = allOperations;
- }
-
- /**
- * Factory method to produce instance of NetconfOperationRouter
- */
- public static NetconfOperationRouter createOperationRouter(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
- final CapabilityProvider capabilityProvider, final DefaultCommitNotificationProducer commitNotifier) {
- NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot);
-
- Preconditions.checkNotNull(netconfOperationServiceSnapshot);
- Preconditions.checkNotNull(capabilityProvider);
-
- final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting();
-
- final Set<NetconfOperation> defaultNetconfOperations = Sets.newHashSet();
- defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, sessionId));
- defaultNetconfOperations.add(new DefaultCloseSession(sessionId, router));
- defaultNetconfOperations.add(new DefaultStartExi(sessionId));
- defaultNetconfOperations.add(new DefaultStopExi(sessionId));
- defaultNetconfOperations.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, router));
+ final Set<NetconfOperation> ops = new HashSet<>();
+ ops.add(new DefaultCloseSession(sessionId, this));
+ ops.add(new DefaultStartExi(sessionId));
+ ops.add(new DefaultStopExi(sessionId));
+ ops.add(new DefaultCommit(commitNotifier, netconfMonitoringService, sessionId, this));
- router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot));
+ ops.addAll(netconfOperationServiceSnapshot.getNetconfOperations());
- return router;
- }
-
- private static Set<NetconfOperation> getAllNetconfOperations(final Set<NetconfOperation> defaultNetconfOperations,
- final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
- Set<NetconfOperation> result = new HashSet<>();
- result.addAll(defaultNetconfOperations);
-
- for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
- final Set<NetconfOperation> netOpsFromService = netconfOperationService.getNetconfOperations();
- for (NetconfOperation netconfOperation : netOpsFromService) {
- Preconditions.checkState(!result.contains(netconfOperation),
- "Netconf operation %s already present", netconfOperation);
- result.add(netconfOperation);
- }
- }
- return Collections.unmodifiableSet(result);
+ allNetconfOperations = ImmutableSet.copyOf(ops);
}
@Override
- public synchronized Document onNetconfMessage(final Document message,
- final NetconfServerSession session) throws NetconfDocumentedException {
+ public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws NetconfDocumentedException {
Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly");
final NetconfOperationExecution netconfOperationExecution;
netconfOperationServiceSnapshot.close();
}
- private NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException {
- LOG.error(s, e);
-
- Map<String, String> info = Maps.newHashMap();
- info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString());
+ private static NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException {
+ LOG.error("{}", s, e);
return new NetconfDocumentedException("Unexpected error",
NetconfDocumentedException.ErrorType.application,
NetconfDocumentedException.ErrorTag.operation_failed,
- NetconfDocumentedException.ErrorSeverity.error, info);
+ NetconfDocumentedException.ErrorSeverity.error,
+ Collections.singletonMap(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString()));
}
private Document executeOperationWithHighestPriority(final Document message,
if (netconfOperation instanceof DefaultNetconfOperation) {
((DefaultNetconfOperation) netconfOperation).setNetconfSession(session);
}
+ if(netconfOperation instanceof SessionAwareNetconfOperation) {
+ ((SessionAwareNetconfOperation) netconfOperation).setSession(session);
+ }
if (!handlingPriority.equals(HandlingPriority.CANNOT_HANDLE)) {
Preconditions.checkState(!sortedPriority.containsKey(handlingPriority),