/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.netconf.impl.osgi; import com.google.common.base.Preconditions; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfOperationRouter; import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; 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.mapping.api.HandlingPriority; import org.opendaylight.controller.netconf.mapping.api.NetconfOperation; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; public class NetconfOperationRouterImpl implements NetconfOperationRouter { private static final Logger logger = LoggerFactory.getLogger(NetconfOperationRouterImpl.class); private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; private final Set allNetconfOperations; private final TreeSet allSortedFilters; private final CapabilityProvider capabilityProvider; public NetconfOperationRouterImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, CapabilityProvider capabilityProvider, DefaultCommitNotificationProducer commitNotifier) { this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot; this.capabilityProvider = capabilityProvider; Set defaultNetconfOperations = Sets.newHashSet(); defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, netconfOperationServiceSnapshot .getNetconfSessionIdForReporting())); defaultNetconfOperations.add(new DefaultCloseSession(netconfOperationServiceSnapshot .getNetconfSessionIdForReporting())); allNetconfOperations = getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot); DefaultCommit defaultCommit = new DefaultCommit(commitNotifier, capabilityProvider, netconfOperationServiceSnapshot.getNetconfSessionIdForReporting()); Set defaultFilters = Sets. newHashSet(defaultCommit); allSortedFilters = getAllNetconfFilters(defaultFilters, netconfOperationServiceSnapshot); } private static Set getAllNetconfOperations(Set defaultNetconfOperations, NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { Set result = new HashSet<>(); result.addAll(defaultNetconfOperations); for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { final Set netOpsFromService = netconfOperationService.getNetconfOperations(); for (NetconfOperation netconfOperation : netOpsFromService) { Preconditions.checkState(result.contains(netconfOperation) == false, "Netconf operation %s already present", netconfOperation); result.add(netconfOperation); } } return Collections.unmodifiableSet(result); } private static TreeSet getAllNetconfFilters(Set defaultFilters, NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { TreeSet result = new TreeSet<>(defaultFilters); for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { final Set filtersFromService = netconfOperationService.getFilters(); for (NetconfOperationFilter filter : filtersFromService) { Preconditions.checkState(result.contains(filter) == false, "Filter %s already present", filter); result.add(filter); } } return result; } public CapabilityProvider getCapabilityProvider() { return capabilityProvider; } @Override public synchronized Document onNetconfMessage(Document message) throws NetconfDocumentedException { NetconfOperationExecution netconfOperationExecution = getNetconfOperationWithHighestPriority(message); logger.debug("Forwarding netconf message {} to {}", XmlUtil.toString(message), netconfOperationExecution.operationWithHighestPriority); final LinkedList chain = new LinkedList<>(); chain.push(netconfOperationExecution); for (Iterator it = allSortedFilters.descendingIterator(); it.hasNext();) { final NetconfOperationFilter filter = it.next(); final NetconfOperationFilterChain prevItem = chain.getFirst(); NetconfOperationFilterChain currentItem = new NetconfOperationFilterChain() { @Override public Document execute(Document message, NetconfOperationRouter operationRouter) throws NetconfDocumentedException { logger.trace("Entering {}", filter); return filter.doFilter(message, operationRouter, prevItem); } }; chain.push(currentItem); } return chain.getFirst().execute(message, this); } private NetconfOperationExecution getNetconfOperationWithHighestPriority(Document message) { // TODO test TreeMap> sortedPriority = getSortedNetconfOperationsWithCanHandle(message); Preconditions.checkState(sortedPriority.isEmpty() == false, "No %s available to handle message %s", NetconfOperation.class.getName(), XmlUtil.toString(message)); HandlingPriority highestFoundPriority = sortedPriority.lastKey(); int netconfOperationsWithHighestPriority = sortedPriority.get(highestFoundPriority).size(); Preconditions.checkState(netconfOperationsWithHighestPriority == 1, "Multiple %s available to handle message %s", NetconfOperation.class.getName(), message); return new NetconfOperationExecution(sortedPriority, highestFoundPriority); } private TreeMap> getSortedNetconfOperationsWithCanHandle(Document message) { TreeMap> sortedPriority = Maps.newTreeMap(); for (NetconfOperation netconfOperation : allNetconfOperations) { final HandlingPriority handlingPriority = netconfOperation.canHandle(message); if (handlingPriority.equals(HandlingPriority.CANNOT_HANDLE) == false) { Set netconfOperations = sortedPriority.get(handlingPriority); netconfOperations = checkIfNoOperationsOnPriority(sortedPriority, handlingPriority, netconfOperations); netconfOperations.add(netconfOperation); } } return sortedPriority; } private Set checkIfNoOperationsOnPriority( TreeMap> sortedPriority, HandlingPriority handlingPriority, Set netconfOperations) { if (netconfOperations == null) { netconfOperations = Sets.newHashSet(); sortedPriority.put(handlingPriority, netconfOperations); } return netconfOperations; } @Override public void close() { netconfOperationServiceSnapshot.close(); } private class NetconfOperationExecution implements NetconfOperationFilterChain { private final NetconfOperation operationWithHighestPriority; private NetconfOperationExecution(NetconfOperation operationWithHighestPriority) { this.operationWithHighestPriority = operationWithHighestPriority; } public NetconfOperationExecution(TreeMap> sortedPriority, HandlingPriority highestFoundPriority) { operationWithHighestPriority = sortedPriority.get(highestFoundPriority).iterator().next(); sortedPriority.remove(highestFoundPriority); } @Override public Document execute(Document message, NetconfOperationRouter router) throws NetconfDocumentedException { return operationWithHighestPriority.handle(message, router); } } @Override public String toString() { return "NetconfOperationRouterImpl{" + "netconfOperationServiceSnapshot=" + netconfOperationServiceSnapshot + '}'; } }