Merge changes I26ca5aba,Ia615dbd4
authorTony Tkacik <ttkacik@cisco.com>
Mon, 8 Dec 2014 14:58:28 +0000 (14:58 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 8 Dec 2014 14:58:29 +0000 (14:58 +0000)
* changes:
  BUG-2459: optimize NetconfOperationRouterImpl operations
  BUG-2459: Reuse SAXTransformerFactory in EXI decoder

opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionListenerFactory.java
opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java
opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfEXIToMessageDecoder.java

index 4be8b91a946ebbc115077d71e5d8751e8757f047..0537942487296c19d99ffa2dcc10cea4a64bd6af 100644 (file)
@@ -22,10 +22,10 @@ public class NetconfServerSessionListenerFactory implements SessionListenerFacto
     private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
     private final CapabilityProvider capabilityProvider;
 
-    public NetconfServerSessionListenerFactory(DefaultCommitNotificationProducer commitNotifier,
-                                               SessionMonitoringService monitor,
-                                               NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
-                                               CapabilityProvider capabilityProvider) {
+    public NetconfServerSessionListenerFactory(final DefaultCommitNotificationProducer commitNotifier,
+                                               final SessionMonitoringService monitor,
+                                               final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot,
+                                               final CapabilityProvider capabilityProvider) {
 
         this.commitNotifier = commitNotifier;
         this.monitor = monitor;
@@ -35,8 +35,7 @@ public class NetconfServerSessionListenerFactory implements SessionListenerFacto
 
     @Override
     public NetconfServerSessionListener getSessionListener() {
-        NetconfOperationRouter operationRouter = NetconfOperationRouterImpl.createOperationRouter(
-                netconfOperationServiceSnapshot, capabilityProvider, commitNotifier);
+        NetconfOperationRouter operationRouter = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot, capabilityProvider, commitNotifier);
         return new NetconfServerSessionListener(operationRouter, monitor, netconfOperationServiceSnapshot);
     }
 }
index e16c0c9d9d680b855fd4a2906169e9eeeab06076..aeab13f7e2aa93b9b7e889ed1232dad6868b4259 100644 (file)
@@ -8,11 +8,11 @@
 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;
@@ -39,61 +39,35 @@ import org.w3c.dom.Document;
 public class NetconfOperationRouterImpl implements NetconfOperationRouter {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class);
-
     private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
-    private Set<NetconfOperation> allNetconfOperations;
-
-    private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
-        this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
-    }
+    private final Collection<NetconfOperation> allNetconfOperations;
 
-    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);
+    public NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, final CapabilityProvider capabilityProvider,
+            final DefaultCommitNotificationProducer commitNotifier) {
+        this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot);
 
         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));
-
-        router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot));
-
-        return router;
-    }
-
-    private static Set<NetconfOperation> getAllNetconfOperations(final Set<NetconfOperation> defaultNetconfOperations,
-            final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
-        Set<NetconfOperation> result = new HashSet<>();
-        result.addAll(defaultNetconfOperations);
+        final Set<NetconfOperation> ops = new HashSet<>();
+        ops.add(new DefaultGetSchema(capabilityProvider, sessionId));
+        ops.add(new DefaultCloseSession(sessionId, this));
+        ops.add(new DefaultStartExi(sessionId));
+        ops.add(new DefaultStopExi(sessionId));
+        ops.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, this));
 
         for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
-            final Set<NetconfOperation> netOpsFromService = netconfOperationService.getNetconfOperations();
-            for (NetconfOperation netconfOperation : netOpsFromService) {
-                Preconditions.checkState(!result.contains(netconfOperation),
+            for (NetconfOperation netconfOperation : netconfOperationService.getNetconfOperations()) {
+                Preconditions.checkState(!ops.contains(netconfOperation),
                         "Netconf operation %s already present", netconfOperation);
-                result.add(netconfOperation);
+                ops.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;
@@ -131,15 +105,13 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter {
         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,
index 0d8f9eeec135af834a5dba471f466472745bc8be..77d33e18313d84b96202a9105453e7d06720ca3a 100644 (file)
@@ -17,7 +17,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
 import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.sax.SAXTransformerFactory;
 import javax.xml.transform.sax.TransformerHandler;
@@ -33,6 +32,7 @@ import org.xml.sax.SAXException;
 public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
+    private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
 
     private final NetconfEXICodec codec;
 
@@ -60,10 +60,7 @@ public final class NetconfEXIToMessageDecoder extends ByteToMessageDecoder {
         }
 
         final EXIReader r = codec.getReader();
-
-        final SAXTransformerFactory transformerFactory
-            = (SAXTransformerFactory) TransformerFactory.newInstance();
-        final TransformerHandler handler = transformerFactory.newTransformerHandler();
+        final TransformerHandler handler = FACTORY.newTransformerHandler();
         r.setContentHandler(handler);
 
         final DOMResult domResult = new DOMResult();