Merge "Introduce checked sendMessage() method"
authorTony Tkacik <ttkacik@cisco.com>
Tue, 18 Feb 2014 12:23:33 +0000 (12:23 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 18 Feb 2014 12:23:33 +0000 (12:23 +0000)
62 files changed:
opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryNotificationProvider.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/codegen/impl/SingletonHolder.java
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationBrokerImpl.xtend
opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/MapUtils.xtend
opendaylight/md-sal/sal-binding-broker/src/test/java/org/opendaylight/controller/sal/binding/test/mock/SimpleInput.java
opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java
opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/DataServiceTest.java
opendaylight/md-sal/sal-binding-it/src/test/java/org/opendaylight/controller/test/sal/binding/it/RoutedServiceTest.java
opendaylight/md-sal/sal-common-impl/src/main/java/org/opendaylight/controller/md/sal/common/impl/routing/RoutingUtils.java
opendaylight/md-sal/sal-common-impl/src/main/java/org/opendaylight/controller/md/sal/common/impl/service/AbstractDataBroker.java
opendaylight/md-sal/sal-common-impl/src/main/java/org/opendaylight/controller/md/sal/common/impl/service/TwoPhaseCommit.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.xtend
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/impl/DataReaderRouter.xtend
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/impl/SchemaAwareDataStoreAdapter.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/util/YangDataOperations.xtend
opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/NetconfMapping.xtend
opendaylight/netconf/config-persister-impl/src/main/java/org/opendaylight/controller/netconf/persist/impl/ConfigPersisterNotificationHandler.java
opendaylight/netconf/netconf-api/src/main/java/org/opendaylight/controller/netconf/api/AbstractNetconfSession.java
opendaylight/netconf/netconf-api/src/main/java/org/opendaylight/controller/netconf/api/NetconfSessionPreferences.java
opendaylight/netconf/netconf-api/src/main/java/org/opendaylight/controller/netconf/api/jmx/CommitJMXNotification.java
opendaylight/netconf/netconf-api/src/main/java/org/opendaylight/controller/netconf/api/jmx/DefaultCommitOperationMXBean.java
opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/DefaultCommitNotificationProducer.java
opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiator.java
opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/MessageHeaderTest.java
opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/controller/netconf/impl/MessageParserTest.java
opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java
opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfMonitoringITTest.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractChannelInitializer.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractNetconfSessionNegotiator.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/NetconfUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ChunkedFramingMechanismEncoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/EOMFramingMechanismEncoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/FramingMechanismHandlerFactory.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfChunkAggregator.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfEOMAggregator.java [moved from opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageAggregator.java with 79% similarity]
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfHelloMessageToXMLEncoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageChunkDecoder.java [deleted file]
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageToXMLEncoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToHelloMessageDecoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfXMLToMessageDecoder.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/SshHandler.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/authentication/LoginPassword.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/Invoker.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClient.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshClientAdapter.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/client/SshSession.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/virtualsocket/ChannelInputStream.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/virtualsocket/ChannelOutputStream.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/ssh/virtualsocket/VirtualSocket.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfHelloMessageAdditionalHeader.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageConstants.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageHeader.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/messages/SendErrorExceptionUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/osgi/NetconfConfigUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XMLNetconfUtil.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlElement.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlNetconfConstants.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlNetconfValidator.java
opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/xml/XmlUtil.java
opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/messages/NetconfMessageFactoryTest.java
third-party/ganymed/pom.xml

index 23a98ff39aedff07e034573b0398735a21da7110..6a9db37dbbfabf8a7fcbd82c3c08130de1690ab8 100644 (file)
@@ -1,5 +1,7 @@
 package org.opendaylight.controller.sal.compatibility;
 
+import java.util.List;
+
 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
@@ -11,8 +13,6 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.List;
-
 public class InventoryNotificationProvider implements AutoCloseable{
 
     private ListenerRegistration<DataChangeListener> nodeConnectorDataChangeListenerRegistration;
@@ -33,7 +33,7 @@ public class InventoryNotificationProvider implements AutoCloseable{
                 && inventoryPublisher!= null){
 
             if(nodeConnectorDataChangeListener == null){
-                InstanceIdentifier nodeConnectorPath = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build();
+                InstanceIdentifier<NodeConnector> nodeConnectorPath = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build();
                 nodeConnectorDataChangeListener = new NodeConnectorDataChangeListener();
                 nodeConnectorDataChangeListener.setInventoryPublisher(inventoryPublisher);
                 nodeConnectorDataChangeListenerRegistration = dataProviderService.registerDataChangeListener(nodeConnectorPath, nodeConnectorDataChangeListener);
index a0bbb28d9e07624e23ad0afd094c347c27d2fce2..790c1fcd4c61cb95f7f624392ab9fcc60b7d2089 100644 (file)
@@ -9,7 +9,10 @@ package org.opendaylight.controller.sal.binding.codegen.impl;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 import javassist.ClassPool;
 
@@ -27,13 +30,26 @@ public class SingletonHolder {
             CLASS_POOL);
     public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
     public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
+
+    public static final int CORE_NOTIFICATION_THREADS = 4;
+    public static final int MAX_NOTIFICATION_THREADS = 32;
+    public static final int NOTIFICATION_THREAD_LIFE = 15;
+
     private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
     private static ListeningExecutorService COMMIT_EXECUTOR = null;
     private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
 
+    /**
+     * @deprecated This method is only used from configuration modules and thus callers of it
+     *             should use service injection to make the executor configurable.
+     */
+    @Deprecated
     public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
         if (NOTIFICATION_EXECUTOR == null) {
-            NOTIFICATION_EXECUTOR = createNamedExecutor("md-sal-binding-notification-%d");
+            ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
+            ExecutorService executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
+                    NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), factory);
+            NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
         }
         return NOTIFICATION_EXECUTOR;
     }
@@ -46,13 +62,13 @@ public class SingletonHolder {
     public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
         if (COMMIT_EXECUTOR == null) {
             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
-           /*
-            * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
-            *        ordering guarantees, which means that using a concurrent threadpool results
-            *        in application data being committed in random order, potentially resulting
-            *        in inconsistent data being present. Once proper primitives are introduced,
-            *        concurrency can be reintroduced.
-            */
+            /*
+             * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
+             *        ordering guarantees, which means that using a concurrent threadpool results
+             *        in application data being committed in random order, potentially resulting
+             *        in inconsistent data being present. Once proper primitives are introduced,
+             *        concurrency can be reintroduced.
+             */
             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
             COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
         }
@@ -60,12 +76,6 @@ public class SingletonHolder {
         return COMMIT_EXECUTOR;
     }
 
-    private static ListeningExecutorService createNamedExecutor(String format) {
-        ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(format).build();
-        ExecutorService executor = Executors.newCachedThreadPool(factory);
-        return MoreExecutors.listeningDecorator(executor);
-    }
-
     public static ExecutorService getDefaultChangeEventExecutor() {
         if (CHANGE_EVENT_EXECUTOR == null) {
             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
index 9a431fec74f4a3cf7c3f4d694f6c2000df3de718..bf82302e548404cb4969cb01046dfae5b726c330 100644 (file)
@@ -8,25 +8,25 @@
 package org.opendaylight.controller.sal.binding.impl\r
 \r
 import com.google.common.collect.HashMultimap\r
+import com.google.common.collect.ImmutableSet\r
 import com.google.common.collect.Multimap\r
-import java.util.Collection\r
+import com.google.common.collect.Multimaps\r
 import java.util.Collections\r
 import java.util.concurrent.Callable\r
 import java.util.concurrent.ExecutorService\r
+import java.util.concurrent.Future\r
+import java.util.Set\r
 import org.opendaylight.controller.sal.binding.api.NotificationListener\r
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService\r
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService.NotificationInterestListener\r
+import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\r
 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker\r
 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration\r
 import org.opendaylight.yangtools.concepts.ListenerRegistration\r
 import org.opendaylight.yangtools.concepts.Registration\r
+import org.opendaylight.yangtools.concepts.util.ListenerRegistry\r
 import org.opendaylight.yangtools.yang.binding.Notification\r
 import org.slf4j.LoggerFactory\r
-import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\rimport com.google.common.collect.Multimaps\r
-import org.opendaylight.yangtools.concepts.util.ListenerRegistry\r
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService.NotificationInterestListener\rimport java.util.Set\r
-import java.util.Set\r
-import com.google.common.collect.ImmutableSet\r
-import java.util.concurrent.Future\r
 \r
 class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
     \r
@@ -69,11 +69,6 @@ class NotificationBrokerImpl implements NotificationProviderService, AutoCloseab
         notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
     }\r
 \r
-    @SuppressWarnings("unchecked")\r
-    private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
-        listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
-    }\r
-\r
     @Deprecated\r
     override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
         throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
index 95de575b518f698fab1f0dce58da2c6a3992b14f..c60686d209cecbee4aeb13ae513fe6082c889cdf 100644 (file)
@@ -7,13 +7,11 @@
  */
 package org.opendaylight.controller.sal.binding.impl.util
 
-import java.util.Map.Entry
-import org.opendaylight.yangtools.concepts.Path
-import java.util.Map
-import java.util.Set
+import com.google.common.collect.Multimap
 import java.util.Collection
 import java.util.HashSet
-import com.google.common.collect.Multimap
+import java.util.Map.Entry
+import org.opendaylight.yangtools.concepts.Path
 
 class MapUtils {
 
index b56b94a67b8ea8c63f7f5f3bd8b976761683c699..0a38c90a6af6b3f7763ac08437c381168ca03fa1 100644 (file)
@@ -16,5 +16,5 @@ import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 public interface SimpleInput extends DataObject,Augmentable<SimpleInput> {
 
     @RoutingContext(BaseIdentity.class)
-    InstanceIdentifier getIdentifier();
+    InstanceIdentifier<?> getIdentifier();
 }
index 4e4416777b8eaed7f48aa1ddbb102a3a2b6c84ec..e8a52d069d72825c1cf71a6e9511abc70e900edd 100644 (file)
@@ -23,8 +23,6 @@ public class TestHelper {
 
     public static final String CONTROLLER_MODELS = "org.opendaylight.controller.model";
     public static final String YANGTOOLS_MODELS = "org.opendaylight.yangtools.model";
-    private static final String OPENDAYLIGHT_SNAPSHOT = "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/";
-    private static final String OPENDAYLIGHT_RELEASE = "http://nexus.opendaylight.org/content/repositories/opendaylight.release/";
 
     public static Option mdSalCoreBundles() {
         return new DefaultCompositeOption( //
index 105f6e59a3d25def3de74d039e0c3dc794d94a37..8a390b337e1849ae8cf2c15f7799073ace928ee2 100644 (file)
@@ -7,15 +7,17 @@
  */
 package org.opendaylight.controller.test.sal.binding.it;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 
 import java.util.concurrent.Future;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
+import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
 import org.opendaylight.controller.sal.core.api.Broker;
@@ -34,8 +36,8 @@ import com.google.inject.Inject;
 public class DataServiceTest extends AbstractTest {
 
     protected DataBrokerService consumerDataService;
-    
-    
+
+
     @Inject
     Broker broker2;
 
@@ -56,59 +58,58 @@ public class DataServiceTest extends AbstractTest {
 
         assertNotNull(consumerDataService);
 
-        
+
         DataModificationTransaction transaction = consumerDataService.beginTransaction();
         assertNotNull(transaction);
-        
+
         NodeRef node1 = createNodeRef("0");
         DataObject  node = consumerDataService.readConfigurationData(node1.getValue());
         assertNull(node);
         Node nodeData1 = createNode("0");
-        
+
         transaction.putConfigurationData(node1.getValue(), nodeData1);
         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
         assertNotNull(commitResult);
-        
+
         RpcResult<TransactionStatus> result = commitResult.get();
-        
+
         assertNotNull(result);
         assertNotNull(result.getResult());
         assertEquals(TransactionStatus.COMMITED, result.getResult());
-        
+
         Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
         assertNotNull(readedData);
         assertEquals(nodeData1.getKey(), readedData.getKey());
-        
-        
+
+
         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
         assertNotNull(transaction);
-        
+
         transaction2.removeConfigurationData(node1.getValue());
-        
+
         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
         assertNotNull(commitResult2);
-        
+
         RpcResult<TransactionStatus> result2 = commitResult2.get();
-        
+
         assertNotNull(result2);
         assertNotNull(result2.getResult());
         assertEquals(TransactionStatus.COMMITED, result2.getResult());
-    
+
         DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
         assertNull(readedData2);
-        
-    
+
+
     }
 
-    
+
     private static NodeRef createNodeRef(String string) {
         NodeKey key = new NodeKey(new NodeId(string));
-        InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
-                .toInstance();
+        InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
 
         return new NodeRef(path);
     }
-    
+
     private static Node createNode(String string) {
         NodeBuilder ret = new NodeBuilder();
         NodeId id = new NodeId(string);
index befe70376468eef0d9687817268c312638698506..9c24ca08303b4f524198808b174ce0af67469bc6 100644 (file)
@@ -192,8 +192,7 @@ public class RoutedServiceTest extends AbstractTest {
      */
     private static NodeRef createNodeRef(String string) {
         NodeKey key = new NodeKey(new NodeId(string));
-        InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
-                .toInstance();
+        InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
 
         return new NodeRef(path);
     }
index 274f084f01d420cf291723d96949382da5c0cd7d..9001aaa4dede9ded757c68b737446ea11a09db79 100644 (file)
@@ -16,28 +16,28 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 public class RoutingUtils {
-    
+
     public static <C,P> RouteChange<C,P> removalChange(C context,P path) {
         final ImmutableMap<C, Set<P>> announcements = ImmutableMap.<C,Set<P>>of();
         final ImmutableMap<C, Set<P>> removals = ImmutableMap.<C,Set<P>>of(context, ImmutableSet.of(path));
         return new RouteChangeImpl<C,P>(announcements, removals);
     }
-    
+
     public static <C,P> RouteChange<C,P> announcementChange(C context,P path) {
         final ImmutableMap<C, Set<P>> announcements = ImmutableMap.<C,Set<P>>of(context, ImmutableSet.of(path));
         final ImmutableMap<C, Set<P>> removals = ImmutableMap.<C,Set<P>>of();
         return new RouteChangeImpl<C,P>(announcements, removals);
     }
-    
-    
+
+
     public static <C,P> RouteChange<C,P> change(Map<C, Set<P>> announcements,
             Map<C, Set<P>> removals) {
         final ImmutableMap<C, Set<P>> immutableAnnouncements = ImmutableMap.<C,Set<P>>copyOf(announcements);
         final ImmutableMap<C, Set<P>> immutableRemovals = ImmutableMap.<C,Set<P>>copyOf(removals);
         return new RouteChangeImpl<C,P>(immutableAnnouncements, immutableRemovals);
     }
-    
-    
+
+
     private static class RouteChangeImpl<C,P> implements RouteChange<C, P> {
         private final Map<C, Set<P>> removal;
         private final Map<C, Set<P>> announcement;
@@ -52,7 +52,7 @@ public class RoutingUtils {
         public Map<C, Set<P>> getAnnouncements() {
             return announcement;
         }
-        
+
         @Override
         public Map<C, Set<P>> getRemovals() {
             return removal;
@@ -78,7 +78,7 @@ public class RoutingUtils {
             if (getClass() != obj.getClass()) {
                 return false;
             }
-            RouteChangeImpl other = (RouteChangeImpl) obj;
+            RouteChangeImpl<?, ?> other = (RouteChangeImpl<?, ?>) obj;
             if (announcement == null) {
                 if (other.announcement != null)
                     return false;
@@ -95,5 +95,5 @@ public class RoutingUtils {
     }
 
 
-    
+
 }
index bfffb594cb6fd73d9654d402be5a66aa076eaa30..ed186dcf314fd0ea075ad81bc3b3e19fb942a48b 100644 (file)
@@ -408,7 +408,7 @@ public abstract class AbstractDataBroker<P extends Path<P>, D extends Object, DC
         Preconditions.checkNotNull(transaction);
         transaction.changeStatus(TransactionStatus.SUBMITED);
         final TwoPhaseCommit<P, D, DCL> task = new TwoPhaseCommit<P, D, DCL>(transaction, this);
-        ;
+
         this.getSubmittedTransactionsCount().getAndIncrement();
         return this.getExecutor().submit(task);
     }
index e99fc0f24c16d424524c2d1642d2109a5193c173..33de1d83dae3cf40ff875625b979a3182783cf20 100644 (file)
@@ -61,6 +61,17 @@ public class TwoPhaseCommit<P extends Path<P>, D extends Object, DCL extends Dat
 
         log.trace("Transaction: {} Affected Subtrees:", transactionId, changedPaths);
 
+        // The transaction has no effects, let's just shortcut it
+        if (changedPaths.isEmpty()) {
+            dataBroker.getFinishedTransactionsCount().getAndIncrement();
+            transaction.changeStatus(TransactionStatus.COMMITED);
+
+            log.trace("Transaction: {} Finished successfully (no effects).", transactionId);
+
+            return Rpcs.<TransactionStatus> getRpcResult(true, TransactionStatus.COMMITED,
+                    Collections.<RpcError> emptySet());
+        }
+
         final ImmutableList.Builder<ListenerStateCapture<P, D, DCL>> listenersBuilder = ImmutableList.builder();
         listenersBuilder.addAll(dataBroker.affectedListeners(changedPaths));
         filterProbablyAffectedListeners(dataBroker.probablyAffectedListeners(changedPaths),listenersBuilder);
@@ -211,14 +222,12 @@ public class TwoPhaseCommit<P extends Path<P>, D extends Object, DCL extends Dat
             @Override
             public void run() {
                 for (final ListenerStateCapture<P, D, DCL> listenerSet : listeners) {
-                    {
-                        DataChangeEvent<P, D> changeEvent = listenerSet.createEvent(transaction);
-                        for (final DataChangeListenerRegistration<P, D, DCL> listener : listenerSet.getListeners()) {
-                            try {
-                                listener.getInstance().onDataChanged(changeEvent);
-                            } catch (Exception e) {
-                                log.error("Unhandled exception when invoking listener {}", listener);
-                            }
+                    DataChangeEvent<P, D> changeEvent = listenerSet.createEvent(transaction);
+                    for (final DataChangeListenerRegistration<P, D, DCL> listener : listenerSet.getListeners()) {
+                        try {
+                            listener.getInstance().onDataChanged(changeEvent);
+                        } catch (Exception e) {
+                            log.error("Unhandled exception when invoking listener {}", listener, e);
                         }
                     }
                 }
index 8f734d7d4c3934a22c1ca91af146534a0a5fac5b..3bbdab2c0722d656aba74035186faa4bc0d843aa 100644 (file)
@@ -8,32 +8,27 @@
 package org.opendaylight.controller.sal.dom.broker;
 
 import java.util.Collections
-import java.util.HashMap
 import java.util.HashSet
-import java.util.Map
 import java.util.Set
 import java.util.concurrent.Callable
 import java.util.concurrent.ExecutorService
 import java.util.concurrent.Executors
 import java.util.concurrent.Future
+import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener
 import org.opendaylight.controller.sal.core.api.Broker
-import org.opendaylight.controller.sal.core.api.BrokerService
 import org.opendaylight.controller.sal.core.api.Consumer
 import org.opendaylight.controller.sal.core.api.Provider
-import org.opendaylight.controller.sal.core.spi.BrokerModule
+import org.opendaylight.controller.sal.core.api.RpcImplementation
+import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
+import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
+import org.opendaylight.controller.sal.core.api.RpcRoutingContext
+import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter
 import org.opendaylight.yangtools.yang.common.QName
 import org.opendaylight.yangtools.yang.common.RpcResult
 import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
 import org.osgi.framework.BundleContext
 import org.slf4j.LoggerFactory
-import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter
-import org.opendaylight.yangtools.concepts.ListenerRegistration
-import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
-import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
-import org.opendaylight.controller.sal.core.api.RpcImplementation
-import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener
-import org.opendaylight.controller.sal.core.api.RpcRoutingContext
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
 
 public class BrokerImpl implements Broker, RpcProvisionRegistry, AutoCloseable {
     private static val log = LoggerFactory.getLogger(BrokerImpl);
index cc44613e448ad520063120f31c481e987f032ee5..95d0018b2117c8c970a61c7902bbc44f2b3d8396 100644 (file)
@@ -7,29 +7,26 @@
  */
 package org.opendaylight.controller.sal.dom.broker.impl
 
-import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import org.opendaylight.yangtools.yang.data.api.CompositeNode
-import org.opendaylight.controller.md.sal.common.api.data.DataReader
-import org.opendaylight.yangtools.yang.common.QName
 import java.net.URI
-import java.util.List
-import org.opendaylight.yangtools.yang.data.api.Node
 import java.util.ArrayList
-import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
-import java.util.Map
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier
-import org.opendaylight.yangtools.yang.data.api.SimpleNode
+import java.util.Collection
 import java.util.Collections
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
 import java.util.HashMap
-import static com.google.common.base.Preconditions.*;
-import java.util.Collection
-import java.util.Set
+import java.util.Map
 import java.util.Map.Entry
-import org.slf4j.LoggerFactory
+import java.util.Set
+import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
+import org.opendaylight.yangtools.yang.data.api.Node
+import org.opendaylight.yangtools.yang.data.api.SimpleNode
 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
+import org.slf4j.LoggerFactory
+
+import static com.google.common.base.Preconditions.*
 
 class DataReaderRouter extends AbstractDataReadRouter<InstanceIdentifier, CompositeNode> {
     private static val LOG = LoggerFactory.getLogger(DataReaderRouter);
@@ -42,7 +39,6 @@ class DataReaderRouter extends AbstractDataReadRouter<InstanceIdentifier, Compos
         var name = pathArgument?.nodeType;
         val nodes = new ArrayList<Node<?>>();
         val keyNodes = new HashMap<QName, SimpleNode<?>>();
-        val iterator = data.iterator;
         for(dataBit : data) {
             try {
                 if(pathArgument != null && dataBit != null) {
index b02a37c3003e594b728554f6c5a0124961872ce3..602afd7c0cf01d4bccc810e0b0dc2d1e5d9a0fd4 100644 (file)
@@ -150,7 +150,7 @@ public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataS
 
     protected CompositeNode mergeData(InstanceIdentifier path, CompositeNode stored, CompositeNode modified,
             boolean config) {
-        long startTime = System.nanoTime();
+        // long startTime = System.nanoTime();
         try {
             DataSchemaNode node = schemaNodeFor(path);
             return YangDataOperations.merge(node, stored, modified, config);
index 1e89281df498b34d82bea1f3e6b08574c0941a06..d80e405b4e05dc7df3bd9ed5c9de3610644e7b18 100644 (file)
@@ -38,12 +38,6 @@ class YangDataOperations {
         throw new IllegalArgumentException("Supplied node is not data node container.");
     }
 
-    private def static checkConfigurational(DataSchemaNode node, boolean config) {
-        if (config) {
-            checkArgument(node.configuration, "Supplied composite node is not configurational.");
-        }
-    }
-
     private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafSchemaNode node, List<Node<?>> original,
         List<Node<?>> modified, boolean configurational) {
         checkArgument(original.size === 1);
index f409ecdade553e91065e85d9fc2024150e21eba5..e5a24fcf63914f35492f50e7186ff2ed844761e4 100644 (file)
@@ -16,30 +16,23 @@ import java.util.Collections
 import java.util.List
 import java.util.Set
 import java.util.concurrent.atomic.AtomicInteger
+import org.opendaylight.controller.netconf.api.NetconfMessage
 import org.opendaylight.controller.sal.common.util.Rpcs
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.RpcResult
 import org.opendaylight.yangtools.yang.data.api.CompositeNode
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
-import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
-import java.util.Collections
-import java.util.List
-import java.util.Set
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
 import org.opendaylight.yangtools.yang.data.api.Node
 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
+import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
 import org.opendaylight.yangtools.yang.model.api.SchemaContext
 import org.w3c.dom.Document
 import org.w3c.dom.Element
-import org.opendaylight.yangtools.yang.common.QName
-import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
-import org.opendaylight.controller.netconf.api.NetconfMessage
-import org.opendaylight.yangtools.yang.common.RpcResult
 
 class NetconfMapping {
 
index e319d2cb679260718c9eade75234b9996170007c..2d89bbc55c03d59d3ad7e648426df91562fc6fde 100644 (file)
@@ -49,7 +49,7 @@ public class ConfigPersisterNotificationHandler implements Closeable {
     private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
         logger.trace("Called registerAsJMXListener");
         try {
-            mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, listener, null, null);
+            mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
         } catch (InstanceNotFoundException | IOException e) {
             throw new RuntimeException("Cannot register as JMX listener to netconf", e);
         }
@@ -58,7 +58,7 @@ public class ConfigPersisterNotificationHandler implements Closeable {
     @Override
     public synchronized void close() {
         // unregister from JMX
-        ObjectName on = DefaultCommitOperationMXBean.objectName;
+        ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
         try {
             if (mBeanServerConnection.isRegistered(on)) {
                 mBeanServerConnection.removeNotificationListener(on, listener);
index bd75c27dd6abf36c794f85459accdcaa2bb3b35d..f85d9b9f304f8cf8de63e90f185d63fb64e82811 100644 (file)
@@ -22,13 +22,13 @@ public abstract class AbstractNetconfSession<S extends NetconfSession, L extends
     private final long sessionId;
     private boolean up = false;
 
-    protected final Channel channel;
+    private final Channel channel;
 
     protected AbstractNetconfSession(L sessionListener, Channel channel, long sessionId) {
         this.sessionListener = sessionListener;
         this.channel = channel;
         this.sessionId = sessionId;
-        logger.debug("Session {} created", toString());
+        logger.debug("Session {} created", sessionId);
     }
 
     protected abstract S thisInstance();
index 1ec46c8d6f1e58912e76c57bd4674f7d70e8c37c..be3040802cdfdc4ab7eaf3ac266914e936de82b0 100644 (file)
@@ -9,7 +9,7 @@
 package org.opendaylight.controller.netconf.api;
 
 public class NetconfSessionPreferences {
-    protected final NetconfMessage helloMessage;
+    private final NetconfMessage helloMessage;
 
     public NetconfSessionPreferences(final NetconfMessage helloMessage) {
         this.helloMessage = helloMessage;
index 0c4174000fe80dc231efd9aeb5702a2832834580..74cd4a4e05a5186cf6c4df5543b03a106c61fa3e 100644 (file)
@@ -17,12 +17,12 @@ public class CommitJMXNotification extends NetconfJMXNotification {
 
     private final Element configSnapshot;
 
-    private static final String afterCommitMessageTemplate = "Commit successful: %s";
+    private static final String AFTER_COMMIT_MESSAGE_TEMPLATE = "Commit successful: %s";
     private final Set<String> capabilities;
 
     CommitJMXNotification(NotificationBroadcasterSupport source, String message, Element cfgSnapshot,
             Set<String> capabilities) {
-        super(TransactionProviderJMXNotificationType.commit, source, String.format(afterCommitMessageTemplate, message));
+        super(TransactionProviderJMXNotificationType.commit, source, String.format(AFTER_COMMIT_MESSAGE_TEMPLATE, message));
         this.configSnapshot = cfgSnapshot;
         this.capabilities = capabilities;
     }
index a170d2905fee69135ab904525d266aa638d61331..e45d3c38a209ee26f0ba2b92e63637ad155f6c01 100644 (file)
@@ -14,7 +14,7 @@ import javax.management.ObjectName;
 
 public interface DefaultCommitOperationMXBean {
 
-    static String typeName = "NetconfNotificationProvider";
-    public static ObjectName objectName = ObjectNameUtil.createONWithDomainAndType(typeName);
+    String TYPE_NAME = "NetconfNotificationProvider";
+    ObjectName OBJECT_NAME = ObjectNameUtil.createONWithDomainAndType(TYPE_NAME);
 
 }
index 693fc1378390abcd89c0f582e75a055d10580c98..4461054437ce61ef47fd2eea76a9b23ae15b5ef3 100644 (file)
@@ -31,7 +31,7 @@ public class DefaultCommitNotificationProducer extends NotificationBroadcasterSu
 
     private final MBeanServer mbeanServer;
 
-    private final ObjectName on = DefaultCommitOperationMXBean.objectName;
+    private final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
 
     public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
         this.mbeanServer = mBeanServer;
index 1b4dfff42baeb0423597af3c90168592db146724..f8024922cfc5a5ba021e97b519493ae80a02c4d2 100644 (file)
@@ -49,7 +49,7 @@ public class NetconfServerSessionNegotiator extends
 
         logger.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
 
-        return new NetconfServerSession(sessionListener, channel, sessionPreferences.getSessionId(), parsedHeader);
+        return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader);
     }
 
    }
index aa3c5d4d9ba539167dd08aa3117dc45fbb7cc9b8..959e2ff144810c2017131e8041229e86b1cabd64 100644 (file)
@@ -8,33 +8,24 @@
 
 package org.opendaylight.controller.netconf.impl;
 
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
-
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
-public class MessageHeaderTest {
-
-    private NetconfMessageHeader header = null;
-
-    @Before
-    public void setUp() {
-        this.header = new NetconfMessageHeader();
-    }
+import org.junit.Test;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
 
+public class MessageHeaderTest {
     @Test
     public void testFromBytes() {
         final byte[] raw = new byte[] { (byte) 0x0a, (byte) 0x23, (byte) 0x35, (byte) 0x38, (byte) 0x0a };
-        this.header.fromBytes(raw);
-        assertEquals(58, this.header.getLength());
+        NetconfMessageHeader header = NetconfMessageHeader.fromBytes(raw);
+        assertEquals(58, header.getLength());
     }
 
     @Test
     public void testToBytes() {
-        this.header.setLength(123);
+        NetconfMessageHeader header = new NetconfMessageHeader(123);
         assertArrayEquals(new byte[] { (byte) 0x0a, (byte) 0x23, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x0a },
-                this.header.toBytes());
+                header.toBytes());
     }
 }
index 85bccba14ff01da5d559ad83ff6080e0ef5b6b42..d1c0b066d7a0a5ef15399a0544deb380177b0b01 100644 (file)
@@ -22,9 +22,10 @@ import java.util.Queue;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.util.handler.ChunkedFramingMechanismEncoder;
 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
+import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
 import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
@@ -47,7 +48,7 @@ public class MessageParserTest {
                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
                 new NetconfMessageToXMLEncoder(),
 
-                new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
+                new NetconfChunkAggregator(),
                 new NetconfXMLToMessageDecoder());
 
         testChunkChannel.writeOutbound(this.msg);
@@ -59,26 +60,25 @@ public class MessageParserTest {
         enc.encode(null, msg, out);
         int msgLength = out.readableBytes();
 
-        int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
-        if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
+        int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
+        if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
             chunkCount++;
         }
         for (int i = 1; i <= chunkCount; i++) {
             ByteBuf recievedOutbound = (ByteBuf) messages.poll();
-            int exptHeaderLength = NetconfMessageConstants.MAX_CHUNK_SIZE;
+            int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
             if (i == chunkCount) {
-                exptHeaderLength = msgLength - (NetconfMessageConstants.MAX_CHUNK_SIZE * (i - 1));
-                byte[] eom = new byte[NetconfMessageConstants.endOfChunk.length];
-                recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfChunk.length,
+                exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
+                byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
+                recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
                         eom);
-                assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
+                assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
             }
 
             byte[] header = new byte[String.valueOf(exptHeaderLength).length()
                     + NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
             recievedOutbound.getBytes(0, header);
-            NetconfMessageHeader messageHeader = new NetconfMessageHeader();
-            messageHeader.fromBytes(header);
+            NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
             assertEquals(exptHeaderLength, messageHeader.getLength());
 
             testChunkChannel.writeInbound(recievedOutbound);
@@ -94,15 +94,14 @@ public class MessageParserTest {
     public void testEOMFramingMechanismOnPipeline() throws Exception {
         EmbeddedChannel testChunkChannel = new EmbeddedChannel(
                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
-                new NetconfMessageToXMLEncoder(), new NetconfMessageAggregator(
-                        FramingMechanism.EOM), new NetconfXMLToMessageDecoder());
+                new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
 
         testChunkChannel.writeOutbound(this.msg);
         ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
 
-        byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
-        recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
-        assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
+        byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
+        recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
+        assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
 
         testChunkChannel.writeInbound(recievedOutbound);
         NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
index 0037981a3e0103f95cd83a945af5aa83c11dd477..1035c9f9fe3afd9c8a0bda0e3b3709ff2800df1d 100644 (file)
@@ -7,9 +7,27 @@
  */
 package org.opendaylight.controller.netconf.it;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import io.netty.channel.ChannelFuture;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.Notification;
+import javax.management.NotificationListener;
+
 import org.apache.commons.lang3.StringUtils;
 import org.junit.After;
 import org.junit.Assert;
@@ -44,33 +62,13 @@ import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOper
 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
 
-import javax.management.InstanceNotFoundException;
-import javax.management.Notification;
-import javax.management.NotificationListener;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.InetSocketAddress;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-import java.util.regex.Pattern;
-
-import static junit.framework.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 
 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
 
-    private static final Logger logger =  LoggerFactory.getLogger(NetconfConfigPersisterITTest.class);
-
     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
 
 
@@ -163,7 +161,7 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
 
     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
         VerifyingNotificationListener listener = new VerifyingNotificationListener();
-        platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.objectName, listener, null, null);
+        platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
         return listener;
     }
 
@@ -230,7 +228,7 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
         public VerifyingPersister() throws IOException {
             Persister mockedAggregator = mock(Persister.class);
 
-            doAnswer(new Answer() {
+            doAnswer(new Answer<Object>() {
                 @Override
                 public Object answer(InvocationOnMock invocation) throws Throwable {
                     ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
index be2c32e34fd8816b28ed1a844437b5d0c133ea56..7a840be02af697f1cd73dad423c8b610b2a664da 100644 (file)
@@ -7,11 +7,23 @@
  */
 package org.opendaylight.controller.netconf.it;
 
-import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
-import com.google.common.collect.Sets;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import io.netty.channel.ChannelFuture;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
 import junit.framework.Assert;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.matchers.JUnitMatchers;
@@ -41,19 +53,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
 
 public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
 
@@ -162,6 +164,8 @@ public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
             }
         }
 
+        sock.close();
+
         org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>"));
         org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<username>tomas</username>"));
     }
index 0910d9403ad98af67427d9dc818e1ddcff216878..48a45845a4a6fe7f57a722bc5678d8fc8447b54d 100644 (file)
@@ -13,8 +13,8 @@ import io.netty.util.concurrent.Promise;
 
 import org.opendaylight.controller.netconf.api.NetconfSession;
 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
 import org.opendaylight.controller.netconf.util.handler.NetconfHelloMessageToXMLEncoder;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToHelloMessageDecoder;
 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
 
@@ -27,7 +27,7 @@ public abstract class AbstractChannelInitializer<S extends NetconfSession> {
     public static final String NETCONF_SESSION_NEGOTIATOR = "negotiator";
 
     public void initialize(SocketChannel ch, Promise<S> promise) {
-        ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfMessageAggregator(FramingMechanism.EOM));
+        ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
         initializeMessageDecoder(ch);
         ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
         initializeMessageEncoder(ch);
index 9986b82bd8d6902a084e77a1a11a6421125ccb17..7f2d8c30f045fb6830458aa7fd461cc7885e8349 100644 (file)
@@ -8,19 +8,30 @@
 
 package org.opendaylight.controller.netconf.util;
 
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.ssl.SslHandler;
+import io.netty.util.Timeout;
+import io.netty.util.Timer;
+import io.netty.util.TimerTask;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
+import io.netty.util.concurrent.Promise;
+
 import java.util.concurrent.TimeUnit;
 
+import io.netty.channel.ChannelInboundHandlerAdapter;
 import org.opendaylight.controller.netconf.api.AbstractNetconfSession;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
 import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
 import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
-import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
 import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
 import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
 import org.slf4j.Logger;
@@ -31,25 +42,13 @@ import org.w3c.dom.NodeList;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.ssl.SslHandler;
-import io.netty.util.Timeout;
-import io.netty.util.Timer;
-import io.netty.util.TimerTask;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
-import io.netty.util.concurrent.Promise;
-
 public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionPreferences, S extends AbstractNetconfSession<S, L>, L extends NetconfSessionListener<S>>
 extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
 
     private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfSessionNegotiator.class);
     public static final String NAME_OF_EXCEPTION_HANDLER = "lastExceptionHandler";
-    public static final String CHUNK_DECODER_CHANNEL_HANDLER_KEY = "chunkDecoder";
 
-    protected final P sessionPreferences;
+    private final P sessionPreferences;
 
     private final L sessionListener;
     private Timeout timeout;
@@ -81,14 +80,15 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
             Future<Channel> future = sslHandler.get().handshakeFuture();
             future.addListener(new GenericFutureListener<Future<? super Channel>>() {
                 @Override
-                public void operationComplete(Future<? super Channel> future) throws Exception {
+                public void operationComplete(Future<? super Channel> future) {
                     Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful");
                     logger.debug("Ssl handshake complete");
                     start();
                 }
             });
-        } else
+        } else {
             start();
+        }
     }
 
     private static Optional<SslHandler> getSslHandler(Channel channel) {
@@ -96,31 +96,19 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
         return sslHandler == null ? Optional.<SslHandler> absent() : Optional.of(sslHandler);
     }
 
+    public P getSessionPreferences() {
+        return sessionPreferences;
+    }
+
     private void start() {
         final NetconfMessage helloMessage = this.sessionPreferences.getHelloMessage();
         logger.debug("Session negotiation started with hello message {}", XmlUtil.toString(helloMessage.getDocument()));
 
-        channel.pipeline().addLast(NAME_OF_EXCEPTION_HANDLER, new ChannelHandler() {
-            @Override
-            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
-            }
-
-            @Override
-            public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
-            }
-
-            @Override
-            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-                logger.warn("An exception occurred during negotiation on channel {}", channel.localAddress(), cause);
-                cancelTimeout();
-                negotiationFailed(cause);
-                changeState(State.FAILED);
-            }
-        });
+        channel.pipeline().addLast(NAME_OF_EXCEPTION_HANDLER, new ExceptionHandlingInboundChannelHandler());
 
         timeout = this.timer.newTimeout(new TimerTask() {
             @Override
-            public void run(final Timeout timeout) throws Exception {
+            public void run(final Timeout timeout) {
                 synchronized (this) {
                     if (state != State.ESTABLISHED) {
                         logger.debug("Connection timeout after {}, session is in state {}", timeout, state);
@@ -140,8 +128,9 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
     }
 
     private void cancelTimeout() {
-        if(timeout!=null)
+        if(timeout!=null) {
             timeout.cancel();
+        }
     }
 
     private void sendMessage(NetconfMessage message) {
@@ -150,27 +139,20 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
 
     @Override
     protected void handleMessage(NetconfHelloMessage netconfMessage) {
-        final Document doc = netconfMessage.getDocument();
+        Preconditions.checkNotNull(netconfMessage != null, "netconfMessage");
 
-        // Only Hello message should arrive during negotiation
-        if (netconfMessage instanceof NetconfHelloMessage) {
+        final Document doc = netconfMessage.getDocument();
 
-            replaceHelloMessageHandlers();
+        replaceHelloMessageHandlers();
 
-            if (shouldUseChunkFraming(doc)) {
-                insertChunkFramingToPipeline();
-            }
+        if (shouldUseChunkFraming(doc)) {
+            insertChunkFramingToPipeline();
+        }
 
-            changeState(State.ESTABLISHED);
-            S session = getSession(sessionListener, channel, (NetconfHelloMessage)netconfMessage);
+        changeState(State.ESTABLISHED);
+        S session = getSession(sessionListener, channel, netconfMessage);
 
-            negotiationSuccessful(session);
-        } else {
-            final IllegalStateException cause = new IllegalStateException(
-                    "Received message was not hello as expected, but was " + XmlUtil.toString(doc));
-            logger.warn("Negotiation of netconf session failed", cause);
-            negotiationFailed(cause);
-        }
+        negotiationSuccessful(session);
     }
 
     /**
@@ -180,9 +162,7 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
         replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER,
                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK));
         replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR,
-                new NetconfMessageAggregator(FramingMechanism.CHUNK));
-        channel.pipeline().addAfter(AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR,
-                CHUNK_DECODER_CHANNEL_HANDLER_KEY, new NetconfMessageChunkDecoder());
+                new NetconfChunkAggregator());
     }
 
     private boolean shouldUseChunkFraming(Document doc) {
@@ -222,14 +202,31 @@ extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
     }
 
     private static boolean isStateChangePermitted(State state, State newState) {
-        if (state == State.IDLE && newState == State.OPEN_WAIT)
+        if (state == State.IDLE && newState == State.OPEN_WAIT) {
             return true;
-        if (state == State.OPEN_WAIT && newState == State.ESTABLISHED)
+        }
+        if (state == State.OPEN_WAIT && newState == State.ESTABLISHED) {
             return true;
-        if (state == State.OPEN_WAIT && newState == State.FAILED)
+        }
+        if (state == State.OPEN_WAIT && newState == State.FAILED) {
             return true;
+        }
 
         logger.debug("Transition from {} to {} is not allowed", state, newState);
         return false;
     }
+
+    /**
+     * Handler to catch exceptions in pipeline during negotiation
+     */
+    private final class ExceptionHandlingInboundChannelHandler extends ChannelInboundHandlerAdapter {
+
+        @Override
+        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+            logger.warn("An exception occurred during negotiation on channel {}", channel.localAddress(), cause);
+            cancelTimeout();
+            negotiationFailed(cause);
+            changeState(State.FAILED);
+        }
+    }
 }
index aae2f71b86b6f67aa08d57bd374407c59d17651d..796ab91a504040e87197f9032d85e4699d4c80b5 100644 (file)
@@ -27,10 +27,12 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 
-public class NetconfUtil {
+public final class NetconfUtil {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
 
+    private NetconfUtil() {}
+
     public static NetconfMessage createMessage(final File f) {
         Preconditions.checkNotNull(f, "File parameter was null");
         try {
index d8dd7881653b466b24886da068f1d6bab529027b..ccc80a7b71248758cc2ce9a3c00225a0dab3fc49 100644 (file)
@@ -8,36 +8,51 @@
 
 package org.opendaylight.controller.netconf.util.handler;
 
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
+
+import com.google.common.base.Preconditions;
+
 public class ChunkedFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
+    public static final int DEFAULT_CHUNK_SIZE = 8192;
+    public static final int MIN_CHUNK_SIZE = 128;
+    public static final int MAX_CHUNK_SIZE = 16 * 1024 * 1024;
 
-    private NetconfMessageHeader messageHeader = new NetconfMessageHeader();
+    private final int chunkSize;
 
-    private final static int MAX_CHUNK_SIZE = NetconfMessageConstants.MAX_CHUNK_SIZE;
+    public ChunkedFramingMechanismEncoder() {
+        this(DEFAULT_CHUNK_SIZE);
+    }
+
+    public ChunkedFramingMechanismEncoder(int chunkSize) {
+        Preconditions.checkArgument(chunkSize > MIN_CHUNK_SIZE);
+        Preconditions.checkArgument(chunkSize < MAX_CHUNK_SIZE);
+        this.chunkSize = chunkSize;
+    }
+
+    public final int getChunkSize() {
+        return chunkSize;
+    }
 
     @Override
     protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
-        while (msg.readableBytes() > MAX_CHUNK_SIZE) {
-            ByteBuf chunk = Unpooled.buffer(MAX_CHUNK_SIZE);
-            chunk.writeBytes(createChunkHeader(MAX_CHUNK_SIZE));
-            chunk.writeBytes(msg.readBytes(MAX_CHUNK_SIZE));
+        while (msg.readableBytes() > chunkSize) {
+            ByteBuf chunk = Unpooled.buffer(chunkSize);
+            chunk.writeBytes(createChunkHeader(chunkSize));
+            chunk.writeBytes(msg.readBytes(chunkSize));
             ctx.write(chunk);
         }
         out.writeBytes(createChunkHeader(msg.readableBytes()));
         out.writeBytes(msg.readBytes(msg.readableBytes()));
-        out.writeBytes(NetconfMessageConstants.endOfChunk);
+        out.writeBytes(NetconfMessageConstants.END_OF_CHUNK);
     }
 
     private ByteBuf createChunkHeader(int chunkSize) {
-        messageHeader.setLength(chunkSize);
-        return Unpooled.wrappedBuffer(messageHeader.toBytes());
+        return Unpooled.wrappedBuffer(NetconfMessageHeader.toBytes(chunkSize));
     }
-
 }
index 7ec5a8f8dcd6c7304b8e7f5edbf7d292acb70199..a3efe8a16bd0c541971e7a4c0ca172b83144d3d5 100644 (file)
@@ -8,20 +8,16 @@
 
 package org.opendaylight.controller.netconf.util.handler;
 
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 
-public class EOMFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
-
-    private byte[] eom = NetconfMessageConstants.endOfMessage;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
 
+public class EOMFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
     @Override
     protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
         out.writeBytes(msg);
-        out.writeBytes(eom);
+        out.writeBytes(NetconfMessageConstants.END_OF_MESSAGE);
     }
-
 }
index 0d98084343a49355e22210868d5c08310dca460a..1f7a32dc704ca2698112b0e5e9d90c9cdb503a87 100644 (file)
@@ -17,7 +17,9 @@ import io.netty.handler.codec.MessageToByteEncoder;
 
 public class FramingMechanismHandlerFactory {
 
-    private final static Logger logger = LoggerFactory.getLogger(FramingMechanismHandlerFactory.class);
+    private static final Logger logger = LoggerFactory.getLogger(FramingMechanismHandlerFactory.class);
+
+    private FramingMechanismHandlerFactory() {}
 
     public static MessageToByteEncoder<ByteBuf> createHandler(FramingMechanism framingMechanism) {
         logger.debug("{} framing mechanism was selected.", framingMechanism);
diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfChunkAggregator.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfChunkAggregator.java
new file mode 100644 (file)
index 0000000..f7045c3
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * 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.util.handler;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NetconfChunkAggregator extends ByteToMessageDecoder {
+    private final static Logger logger = LoggerFactory.getLogger(NetconfChunkAggregator.class);
+    public static final int DEFAULT_MAXIMUM_CHUNK_SIZE = 16 * 1024 * 1024;
+
+    private static enum State {
+        HEADER_ONE, // \n
+        HEADER_TWO, // #
+        HEADER_LENGTH_FIRST, // [1-9]
+        HEADER_LENGTH_OTHER, // [0-9]*\n
+        DATA,
+        FOOTER_ONE, // \n
+        FOOTER_TWO, // #
+        FOOTER_THREE, // #
+        FOOTER_FOUR, // \n
+    }
+
+    private final int maxChunkSize = DEFAULT_MAXIMUM_CHUNK_SIZE;
+    private State state = State.HEADER_ONE;
+    private long chunkSize;
+    private ByteBuf chunk;
+
+    @Override
+    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+        while (in.isReadable()) {
+            switch (state) {
+            case HEADER_ONE:
+            {
+                final byte b = in.readByte();
+                if (b != '\n') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+                    throw new IllegalStateException("Malformed chunk header encountered (byte 0)");
+                }
+
+                state = State.HEADER_TWO;
+                break;
+            }
+            case HEADER_TWO:
+            {
+                final byte b = in.readByte();
+                if (b != '#') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'#');
+                    throw new IllegalStateException("Malformed chunk header encountered (byte 1)");
+                }
+
+                state = State.HEADER_LENGTH_FIRST;
+                break;
+            }
+            case HEADER_LENGTH_FIRST:
+            {
+                final byte b = in.readByte();
+                if (b < '1' || b > '9') {
+                    logger.debug("Got byte {} while waiting for {}-{}", b, (byte)'1', (byte)'9');
+                    throw new IllegalStateException("Invalid chunk size encountered (byte 0)");
+                }
+
+                chunkSize = b - '0';
+                state = State.HEADER_LENGTH_OTHER;
+                break;
+            }
+            case HEADER_LENGTH_OTHER:
+            {
+                final byte b = in.readByte();
+                if (b == '\n') {
+                    state = State.DATA;
+                    break;
+                }
+
+                if (b < '0' || b > '9') {
+                    logger.debug("Got byte {} while waiting for {}-{}", b, (byte)'0', (byte)'9');
+                    throw new IllegalStateException("Invalid chunk size encountered");
+                }
+
+                chunkSize *= 10;
+                chunkSize += b - '0';
+
+                if (chunkSize > maxChunkSize) {
+                    logger.debug("Parsed chunk size {}, maximum allowed is {}", chunkSize, maxChunkSize);
+                    throw new IllegalStateException("Maximum chunk size exceeded");
+                }
+                break;
+            }
+            case DATA:
+                /*
+                 * FIXME: this gathers all data into one big chunk before passing
+                 *        it on. Make sure the pipeline can work with partial data
+                 *        and then change this piece to pass the data on as it
+                 *        comes through.
+                 */
+                if (in.readableBytes() < chunkSize) {
+                    logger.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize);
+                    in.discardReadBytes();
+                    return;
+                }
+
+                chunk = in.readBytes((int)chunkSize);
+                state = State.FOOTER_ONE;
+                break;
+            case FOOTER_ONE:
+            {
+                final byte b = in.readByte();
+                if (b != '\n') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+                    throw new IllegalStateException("Malformed chunk footer encountered (byte 0)");
+                }
+
+                state = State.FOOTER_TWO;
+                break;
+            }
+            case FOOTER_TWO:
+            {
+                final byte b = in.readByte();
+                if (b != '#') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'#');
+                    throw new IllegalStateException("Malformed chunk footer encountered (byte 1)");
+                }
+
+                state = State.FOOTER_THREE;
+                break;
+            }
+            case FOOTER_THREE:
+            {
+                final byte b = in.readByte();
+                if (b != '#') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'#');
+                    throw new IllegalStateException("Malformed chunk footer encountered (byte 2)");
+                }
+
+                state = State.FOOTER_FOUR;
+                break;
+            }
+            case FOOTER_FOUR:
+            {
+                final byte b = in.readByte();
+                if (b != '\n') {
+                    logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+                    throw new IllegalStateException("Malformed chunk footer encountered (byte 3)");
+                }
+
+                state = State.HEADER_ONE;
+                out.add(chunk);
+                chunkSize = 0;
+                chunk = null;
+                break;
+            }
+            }
+        }
+
+        in.discardReadBytes();
+    }
+}
@@ -8,32 +8,24 @@
 
 package org.opendaylight.controller.netconf.util.handler;
 
-import com.google.common.base.Charsets;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.ByteToMessageDecoder;
-import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.util.List;
 
-public class NetconfMessageAggregator extends ByteToMessageDecoder {
-
-    private final static Logger logger = LoggerFactory.getLogger(NetconfMessageAggregator.class);
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-    private byte[] eom = NetconfMessageConstants.endOfMessage;
+import com.google.common.base.Charsets;
 
-    public NetconfMessageAggregator(FramingMechanism framingMechanism) {
-        if (framingMechanism == FramingMechanism.CHUNK) {
-            eom = NetconfMessageConstants.endOfChunk;
-        }
-    }
+public class NetconfEOMAggregator extends ByteToMessageDecoder {
+    private final static Logger logger = LoggerFactory.getLogger(NetconfEOMAggregator.class);
 
     @Override
     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
-        int index = indexOfSequence(in, eom);
+        int index = indexOfSequence(in, NetconfMessageConstants.END_OF_MESSAGE);
         if (index == -1) {
             logger.debug("Message is not complete, read again.");
             if (logger.isTraceEnabled()) {
@@ -43,7 +35,7 @@ public class NetconfMessageAggregator extends ByteToMessageDecoder {
             ctx.read();
         } else {
             ByteBuf msg = in.readBytes(index);
-            in.readBytes(eom.length);
+            in.readBytes(NetconfMessageConstants.END_OF_MESSAGE.length);
             in.discardReadBytes();
             logger.debug("Message is complete.");
             out.add(msg);
index a87d175d78e2f901d70de1efd5a7466223a21001..65d5b270254dcbd9503a98791c4c9f878d3442f9 100644 (file)
@@ -7,12 +7,18 @@
  */
 package org.opendaylight.controller.netconf.util.handler;
 
-import java.nio.ByteBuffer;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+
+import java.io.IOException;
+
+import javax.xml.transform.TransformerException;
 
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
@@ -39,9 +45,9 @@ import com.google.common.base.Preconditions;
  * </pre>
  */
 public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEncoder {
-
     @Override
-    protected ByteBuffer encodeMessage(NetconfMessage msg) {
+    @VisibleForTesting
+    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
         Preconditions.checkState(msg instanceof NetconfHelloMessage, "Netconf message of type %s expected, was %s",
                 NetconfHelloMessage.class, msg.getClass());
         Optional<NetconfHelloMessageAdditionalHeader> headerOptional = ((NetconfHelloMessage) msg)
@@ -50,15 +56,9 @@ public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEn
         // If additional header present, serialize it along with netconf hello
         // message
         if (headerOptional.isPresent()) {
-            byte[] bytesFromHeader = headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8);
-            byte[] bytesFromMessage = xmlToString(msg.getDocument()).getBytes(Charsets.UTF_8);
-
-            ByteBuffer byteBuffer = ByteBuffer.allocate(bytesFromHeader.length + bytesFromMessage.length)
-                    .put(bytesFromHeader).put(bytesFromMessage);
-            byteBuffer.flip();
-            return byteBuffer;
+            out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8));
         }
 
-        return super.encodeMessage(msg);
+        super.encode(ctx, msg, out);
     }
 }
diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageChunkDecoder.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/handler/NetconfMessageChunkDecoder.java
deleted file mode 100644 (file)
index 5c00b9b..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * 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.util.handler;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageDecoder;
-
-import java.nio.charset.Charset;
-import java.util.List;
-
-import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class NetconfMessageChunkDecoder extends ByteToMessageDecoder {
-
-    private final static Logger logger = LoggerFactory.getLogger(NetconfMessageChunkDecoder.class);
-
-    @Override
-    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
-        ByteBuf byteBufMsg = Unpooled.buffer(in.readableBytes());
-        int chunkSize = -1;
-        boolean isParsed = false;
-        while (in.isReadable()) {
-            try {
-                if (!isParsed) {
-                    chunkSize = readHeader(in);
-                    isParsed = true;
-                }
-                if (chunkSize != -1 && isParsed) {
-                    in.readBytes(byteBufMsg, chunkSize);
-                    isParsed = false;
-                } else {
-                    throw new NetconfDeserializerException("Unable to parse chunked data or header.");
-                }
-            } catch (Exception e) {
-                logger.error("Failed to decode chunked message.", e);
-                this.exceptionCaught(ctx, e);
-            }
-        }
-        out.add(byteBufMsg);
-        isParsed = false;
-    }
-
-    private int readHeader(ByteBuf in) {
-        ByteBuf chunkSize = Unpooled.buffer(NetconfMessageConstants.MIN_HEADER_LENGTH,
-                NetconfMessageConstants.MAX_HEADER_LENGTH);
-        byte b = in.readByte();
-        if (b != 10)
-            return -1;
-        b = in.readByte();
-        if (b != 35)
-            return -1;
-        while ((b = in.readByte()) != 10) {
-            chunkSize.writeByte(b);
-        }
-        return Integer.parseInt(chunkSize.toString(Charset.forName("UTF-8")));
-    }
-
-}
index df0f7ef46a72b30318cfa2a75e5c8e8d6564c4b8..f9792a6c5bf0e2965bead5df9b1c8671ce7d600e 100644 (file)
@@ -8,24 +8,32 @@
 package org.opendaylight.controller.netconf.util.handler;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufOutputStream;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToByteEncoder;
 
-import java.nio.ByteBuffer;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 
 import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
 
 public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
+    private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
 
     private final Optional<String> clientId;
 
@@ -39,7 +47,7 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
 
     @Override
     @VisibleForTesting
-    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws Exception {
+    public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
         LOG.debug("Sent to encode : {}", msg);
 
         if (clientId.isPresent()) {
@@ -47,17 +55,14 @@ public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMess
             msg.getDocument().appendChild(comment);
         }
 
-        final ByteBuffer msgBytes = encodeMessage(msg);
-
-        LOG.trace("Putting message \n{}", xmlToString(msg.getDocument()));
-        out.writeBytes(msgBytes);
-    }
-
-    protected ByteBuffer encodeMessage(NetconfMessage msg) {
-        return Charsets.UTF_8.encode(xmlToString(msg.getDocument()));
-    }
+        try (OutputStream os = new ByteBufOutputStream(out)) {
+            Transformer transformer = FACTORY.newTransformer();
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 
-    protected String xmlToString(Document doc) {
-        return XmlUtil.toString(doc, false);
+            StreamResult result = new StreamResult(new OutputStreamWriter(os));
+            DOMSource source = new DOMSource(msg.getDocument());
+            transformer.transform(source, result);
+        }
     }
 }
index 42586a5ecc60433d28a8fa89bbe57d3e958fdee1..c4808e0868d0f59b1378f5900b5151933edf696e 100644 (file)
@@ -7,6 +7,12 @@
  */
 package org.opendaylight.controller.netconf.util.handler;
 
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.io.ByteArrayInputStream;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.List;
@@ -14,8 +20,12 @@ import java.util.List;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableList;
 
@@ -25,7 +35,8 @@ import com.google.common.collect.ImmutableList;
  * {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
  * . Used by netconf server to retrieve information about session metadata.
  */
-public class NetconfXMLToHelloMessageDecoder extends NetconfXMLToMessageDecoder {
+public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
 
     private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
             new byte[] { ']', '\n' },
@@ -35,34 +46,46 @@ public class NetconfXMLToHelloMessageDecoder extends NetconfXMLToMessageDecoder
             new byte[] { '\r', '\n', '[' },
             new byte[] { '\n', '[' });
 
-    private String additionalHeaderCache;
-
     @Override
-    protected byte[] preprocessMessageBytes(byte[] bytes) {
-        // Extract bytes containing header with additional metadata
-
-        if (startsWithAdditionalHeader(bytes)) {
-            // Auth information containing username, ip address... extracted for monitoring
-            int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
-            if (endOfAuthHeader > -1) {
-                byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
-                additionalHeaderCache = additionalHeaderToString(additionalHeaderBytes);
-                bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
-            }
+    @VisibleForTesting
+    public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+        if (in.readableBytes() == 0) {
+            LOG.debug("No more content in incoming buffer.");
+            return;
         }
 
-        return bytes;
-    }
+        in.markReaderIndex();
+        try {
+            LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
+            byte[] bytes = new byte[in.readableBytes()];
+            in.readBytes(bytes);
+
+            logMessage(bytes);
+
+            // Extract bytes containing header with additional metadata
+            String additionalHeader = null;
+            if (startsWithAdditionalHeader(bytes)) {
+                // Auth information containing username, ip address... extracted for monitoring
+                int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
+                if (endOfAuthHeader > -1) {
+                    byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
+                    additionalHeader = additionalHeaderToString(additionalHeaderBytes);
+                    bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
+                }
+            }
 
-    @Override
-    protected void cleanUpAfterDecode() {
-        additionalHeaderCache = null;
-    }
+            Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
 
-    @Override
-    protected NetconfMessage buildNetconfMessage(Document doc) {
-        return new NetconfHelloMessage(doc, additionalHeaderCache == null ? null
-                : NetconfHelloMessageAdditionalHeader.fromString(additionalHeaderCache));
+            final NetconfMessage message;
+            if (additionalHeader != null) {
+                message = new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
+            } else {
+                message = new NetconfHelloMessage(doc);
+            }
+            out.add(message);
+        } finally {
+            in.discardReadBytes();
+        }
     }
 
     private int getAdditionalHeaderEndIndex(byte[] bytes) {
@@ -102,15 +125,23 @@ public class NetconfXMLToHelloMessageDecoder extends NetconfXMLToMessageDecoder
         return -1;
     }
 
+
+    private void logMessage(byte[] bytes) {
+        String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
+        LOG.debug("Parsing message \n{}", s);
+    }
+
     private boolean startsWithAdditionalHeader(byte[] bytes) {
         for (byte[] possibleStart : POSSIBLE_STARTS) {
             int i = 0;
             for (byte b : possibleStart) {
-                if(bytes[i++] != b)
+                if(bytes[i++] != b) {
                     break;
+                }
 
-                if(i == possibleStart.length)
+                if(i == possibleStart.length) {
                     return true;
+                }
             }
         }
 
index b697edfb053229fd75f602798c10150f0152b86c..06a4dc7207204d573f5abb2cc28c32318fee891d 100644 (file)
@@ -7,73 +7,32 @@
  */
 package org.opendaylight.controller.netconf.util.handler;
 
-import java.io.ByteArrayInputStream;
-import java.nio.ByteBuffer;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufInputStream;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
 import java.util.List;
 
-import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.w3c.dom.Document;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageDecoder;
-
-public class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
+public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);
 
     @Override
     @VisibleForTesting
     public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
-        if (in.readableBytes() == 0) {
-            LOG.debug("No more content in incoming buffer.");
-            return;
-        }
-
-        in.markReaderIndex();
-        try {
+        if (in.readableBytes() != 0) {
             LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
-            byte[] bytes = new byte[in.readableBytes()];
-            in.readBytes(bytes);
-
-            logMessage(bytes);
-
-            bytes = preprocessMessageBytes(bytes);
-            NetconfMessage message;
-            try {
-                Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
-                message = buildNetconfMessage(doc);
-            } catch (Exception e) {
-                throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
-            }
-
-            out.add(message);
-        } finally {
-            in.discardReadBytes();
-            cleanUpAfterDecode();
+            out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in))));
+        } else {
+            LOG.debug("No more content in incoming buffer.");
         }
     }
-
-    protected void cleanUpAfterDecode() {}
-
-    protected NetconfMessage buildNetconfMessage(Document doc) {
-        return new NetconfMessage(doc);
-    }
-
-    protected byte[] preprocessMessageBytes(byte[] bytes) {
-        return bytes;
-    }
-
-    private void logMessage(byte[] bytes) {
-        String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
-        LOG.debug("Parsing message \n{}", s);
-    }
-
 }
index 0d9096c02a3abd1b33204665cb72dc4cbd10e805..49d5a05ae22a6939f992c4637f123f550fb3a4e7 100644 (file)
@@ -29,6 +29,8 @@ import org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.Virtua
  * stops at instance of this class. All downstream events are handed of to wrapped {@link org.opendaylight.controller.netconf.util.handler.ssh.client.SshClientAdapter};
  */
 public class SshHandler extends ChannelOutboundHandlerAdapter {
+    private static final String SOCKET = "socket";
+
     private final VirtualSocket virtualSocket = new VirtualSocket();
     private final SshClientAdapter sshClientAdapter;
 
@@ -39,20 +41,20 @@ public class SshHandler extends ChannelOutboundHandlerAdapter {
 
     @Override
     public void handlerAdded(ChannelHandlerContext ctx){
-        if (ctx.channel().pipeline().get("socket") == null) {
-            ctx.channel().pipeline().addFirst("socket", virtualSocket);
+        if (ctx.channel().pipeline().get(SOCKET) == null) {
+            ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
         }
     }
 
     @Override
-    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
-        if (ctx.channel().pipeline().get("socket") != null) {
-            ctx.channel().pipeline().remove("socket");
+    public void handlerRemoved(ChannelHandlerContext ctx) {
+        if (ctx.channel().pipeline().get(SOCKET) != null) {
+            ctx.channel().pipeline().remove(SOCKET);
         }
     }
 
     @Override
-    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
+    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
         this.sshClientAdapter.write((ByteBuf) msg);
     }
 
@@ -60,18 +62,18 @@ public class SshHandler extends ChannelOutboundHandlerAdapter {
     public void connect(final ChannelHandlerContext ctx,
                         SocketAddress remoteAddress,
                         SocketAddress localAddress,
-                        ChannelPromise promise) throws Exception {
+                        ChannelPromise promise) {
         ctx.connect(remoteAddress, localAddress, promise);
 
         promise.addListener(new ChannelFutureListener() {
-            public void operationComplete(ChannelFuture channelFuture) throws Exception {
+            public void operationComplete(ChannelFuture channelFuture) {
                 sshClientAdapter.start(ctx);
             }}
         );
     }
 
     @Override
-    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
+    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
         sshClientAdapter.stop(promise);
     }
 }
index 2f1b260bd087fd0f48dccdb0af1911157395d7cf..4aa274c6df34f2f2e9d4151454c4b84f53a41bdb 100644 (file)
@@ -29,7 +29,8 @@ public class LoginPassword extends AuthenticationHandler {
     public void authenticate(Connection connection) throws IOException {
         boolean isAuthenticated = connection.authenticateWithPassword(username, password);
 
-        if (isAuthenticated == false)
+        if (isAuthenticated == false) {
             throw new IOException("Authentication failed.");
+        }
     }
 }
index 8636e5ecb5fe55f22ce01cf9a19fb537397c9b0e..fffb304d26be7b3b32787fa1ae43e5392023bbd8 100644 (file)
@@ -19,6 +19,7 @@ public abstract class Invoker {
     private Invoker(){}
 
     protected boolean isInvoked() {
+        // TODO invoked is always false
         return invoked;
     }
 
@@ -34,7 +35,9 @@ public abstract class Invoker {
         return new Invoker() {
             @Override
             void invoke(SshSession session) throws IOException {
-                if (isInvoked() == true) throw new IllegalStateException("Already invoked.");
+                if (isInvoked()) {
+                    throw new IllegalStateException("Already invoked.");
+                }
 
                 session.startSubSystem(subsystem);
             }
index 3cb608db6a87c3ef121cc6a4edf0f0676fab9912..7beee649abfe0501e60850fda0db79dd96c7bb86 100644 (file)
@@ -23,7 +23,7 @@ import java.util.Map;
  */
 public class SshClient {
     private final VirtualSocket socket;
-    private final Map<Integer, SshSession> openSessions = new HashMap();
+    private final Map<Integer, SshSession> openSessions = new HashMap<>();
     private final AuthenticationHandler authenticationHandler;
     private Connection connection;
 
@@ -33,8 +33,9 @@ public class SshClient {
     }
 
     public SshSession openSession() throws IOException {
-        if (connection == null)
+        if (connection == null) {
             connect();
+        }
 
         Session session = connection.openSession();
         SshSession sshSession = new SshSession(session);
@@ -52,7 +53,7 @@ public class SshClient {
 
     public void closeSession(SshSession session) {
         if (session.getState() == Channel.STATE_OPEN || session.getState() == Channel.STATE_OPENING) {
-            session.session.close();
+            session.close();
         }
     }
 
@@ -62,7 +63,8 @@ public class SshClient {
 
         openSessions.clear();
 
-        if (connection != null)
+        if (connection != null) {
             connection.close();
+        }
     }
 }
index 4213fe3e0642db6257b3e655c407a4394785fca3..6350dd154415b510515f919d94c88d06ecac5314 100644 (file)
@@ -27,16 +27,14 @@ import org.opendaylight.controller.netconf.util.handler.ssh.virtualsocket.Virtua
  * pipeline.
  */
 public class SshClientAdapter implements Runnable {
+    private static final int BUFFER_SIZE = 1024;
+
     private final SshClient sshClient;
     private final Invoker invoker;
 
-    private SshSession session;
-    private InputStream stdOut;
-    private InputStream stdErr;
     private OutputStream stdIn;
 
-    private Queue<ByteBuf> postponned = new LinkedList<>();
-
+    private Queue<ByteBuf> postponed = new LinkedList<>();
 
     private ChannelHandlerContext ctx;
     private ChannelPromise disconnectPromise;
@@ -52,22 +50,22 @@ public class SshClientAdapter implements Runnable {
 
     public void run() {
         try {
-            session = sshClient.openSession();
+            SshSession session = sshClient.openSession();
             invoker.invoke(session);
-            stdOut = session.getStdout();
-            stdErr = session.getStderr();
+            InputStream stdOut = session.getStdout();
+            session.getStderr();
 
             synchronized (lock) {
 
                 stdIn = session.getStdin();
-                ByteBuf message = null;
-                while ((message = postponned.poll()) != null) {
+                ByteBuf message;
+                while ((message = postponed.poll()) != null) {
                     writeImpl(message);
                 }
             }
 
             while (stopRequested.get() == false) {
-                byte[] readBuff = new byte[1024];
+                byte[] readBuff = new byte[BUFFER_SIZE];
                 int c = stdOut.read(readBuff);
                 if (c == -1) {
                     continue;
@@ -89,8 +87,9 @@ public class SshClientAdapter implements Runnable {
             sshClient.close();
 
             synchronized (lock) {
-                if (disconnectPromise != null)
+                if (disconnectPromise != null) {
                     ctx.disconnect(disconnectPromise);
+                }
             }
         }
     }
@@ -99,7 +98,7 @@ public class SshClientAdapter implements Runnable {
     public void write(ByteBuf message) throws IOException {
         synchronized (lock) {
             if (stdIn == null) {
-                postponned.add(message);
+                postponed.add(message);
                 return;
             }
             writeImpl(message);
@@ -119,8 +118,10 @@ public class SshClientAdapter implements Runnable {
     }
 
     public void start(ChannelHandlerContext ctx) {
-        if (this.ctx != null)
-            return; // context is already associated.
+        if (this.ctx != null) {
+            // context is already associated.
+            return;
+        }
         this.ctx = ctx;
         new Thread(this).start();
     }
index df400aa1413e64108dab66190dd763b4778dc0ce..411036c975baf2f29b9f98e4c69b8956acf64474 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.controller.netconf.util.handler.ssh.client;
 import ch.ethz.ssh2.Session;
 import ch.ethz.ssh2.StreamGobbler;
 
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -18,8 +19,8 @@ import java.io.OutputStream;
 /**
  * Wrapper class for proprietary SSH sessions implementations
  */
-public class SshSession {
-    final Session session;
+public class SshSession implements Closeable {
+    private final Session session;
 
     public SshSession(Session session) {
         this.session = session;
@@ -61,8 +62,8 @@ public class SshSession {
         return session.waitUntilDataAvailable(timeout);
     }
 
-    public int waitForCondition(int condition_set, long timeout) {
-        return session.waitForCondition(condition_set, timeout);
+    public int waitForCondition(int conditionSet, long timeout) {
+        return session.waitForCondition(conditionSet, timeout);
     }
 
     public Integer getExitStatus() {
@@ -72,4 +73,9 @@ public class SshSession {
     public String getExitSignal() {
         return session.getExitSignal();
     }
+
+    @Override
+    public void close() {
+        session.close();
+    }
 }
index 4b9bfbf5f826ee13c34127d902c2ad583d5f8eb4..313ea932413b63b36c5d20cc6cb1a9570205ab2a 100644 (file)
@@ -39,7 +39,9 @@ public class ChannelInputStream extends InputStream implements ChannelInboundHan
 
             b[off] = (byte)c;
 
-            if(this.bb.readableBytes() == 0) return bytesRead;
+            if(this.bb.readableBytes() == 0) {
+                return bytesRead;
+            }
 
             int ltr = len-1;
             ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
@@ -72,28 +74,23 @@ public class ChannelInputStream extends InputStream implements ChannelInboundHan
         }
     }
 
-    public void channelRegistered(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelRegistered(ChannelHandlerContext ctx) {
         ctx.fireChannelRegistered();
     }
 
-    public void channelUnregistered(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelUnregistered(ChannelHandlerContext ctx) {
         ctx.fireChannelUnregistered();
     }
 
-    public void channelActive(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelActive(ChannelHandlerContext ctx) {
         ctx.fireChannelActive();
     }
 
-    public void channelInactive(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelInactive(ChannelHandlerContext ctx) {
         ctx.fireChannelInactive();
     }
 
-    public void channelRead(ChannelHandlerContext ctx, Object o)
-            throws Exception {
+    public void channelRead(ChannelHandlerContext ctx, Object o) {
         synchronized(lock) {
             this.bb.discardReadBytes();
             this.bb.writeBytes((ByteBuf) o);
@@ -101,31 +98,25 @@ public class ChannelInputStream extends InputStream implements ChannelInboundHan
         }
     }
 
-    public void channelReadComplete(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelReadComplete(ChannelHandlerContext ctx) {
         ctx.fireChannelReadComplete();
     }
 
-    public void userEventTriggered(ChannelHandlerContext ctx, Object o)
-            throws Exception {
+    public void userEventTriggered(ChannelHandlerContext ctx, Object o) {
         ctx.fireUserEventTriggered(o);
     }
 
-    public void channelWritabilityChanged(ChannelHandlerContext ctx)
-            throws Exception {
+    public void channelWritabilityChanged(ChannelHandlerContext ctx) {
         ctx.fireChannelWritabilityChanged();
     }
 
-    public void handlerAdded(ChannelHandlerContext ctx)
-            throws Exception {
+    public void handlerAdded(ChannelHandlerContext ctx) {
     }
 
-    public void handlerRemoved(ChannelHandlerContext ctx)
-            throws Exception {
+    public void handlerRemoved(ChannelHandlerContext ctx) {
     }
 
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable)
-            throws Exception {
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
         ctx.fireExceptionCaught(throwable);
     }
 }
index b1314a6eedfde68f71ccb26e27b0c6d73b8e51cd..cd7ec5439bc422e492b874b64e1be89211213d88 100644 (file)
@@ -14,7 +14,6 @@ import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelOutboundHandler;
 import io.netty.channel.ChannelPromise;
 
-import java.io.IOException;
 import java.io.OutputStream;
 import java.net.SocketAddress;
 
@@ -27,7 +26,7 @@ public class ChannelOutputStream extends OutputStream implements ChannelOutbound
     private ChannelHandlerContext ctx;
 
     @Override
-    public void flush() throws IOException {
+    public void flush() {
         synchronized(lock) {
             ctx.writeAndFlush(buff).awaitUninterruptibly();
             buff = Unpooled.buffer();
@@ -35,51 +34,44 @@ public class ChannelOutputStream extends OutputStream implements ChannelOutbound
     }
 
     @Override
-    public void write(int b) throws IOException {
+    public void write(int b) {
         synchronized(lock) {
             buff.writeByte(b);
         }
     }
 
     public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
-                     ChannelPromise promise) throws Exception {
+                     ChannelPromise promise) {
         ctx.bind(localAddress, promise);
     }
 
     public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
-                        SocketAddress localAddress, ChannelPromise promise)
-            throws Exception {
+                        SocketAddress localAddress, ChannelPromise promise) {
         this.ctx = ctx;
         ctx.connect(remoteAddress, localAddress, promise);
     }
 
-    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
-            throws Exception {
+    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
         ctx.disconnect(promise);
     }
 
-    public void close(ChannelHandlerContext ctx, ChannelPromise promise)
-            throws Exception {
+    public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
         ctx.close(promise);
     }
 
-    public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise)
-            throws Exception {
+    public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise) {
         ctx.deregister(channelPromise);
     }
 
-    public void read(ChannelHandlerContext ctx)
-            throws Exception {
+    public void read(ChannelHandlerContext ctx) {
         ctx.read();
     }
 
-    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
-            throws Exception {
+    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
         // pass
     }
 
-    public void flush(ChannelHandlerContext ctx)
-            throws Exception {
+    public void flush(ChannelHandlerContext ctx) {
         // pass
     }
 
@@ -91,8 +83,7 @@ public class ChannelOutputStream extends OutputStream implements ChannelOutbound
             throws Exception {
     }
 
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
-            throws Exception {
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
         ctx.fireExceptionCaught(cause);
     }
 }
index 1011ca16be84803d4bfbe6f8f083335782de6538..36c2c1c3295736bf9cc183fbe53256ac847ff15b 100644 (file)
@@ -26,6 +26,9 @@ import java.nio.channels.SocketChannel;
  * are able to use full potential of NIO environment.
  */
 public class VirtualSocket extends Socket implements ChannelHandler {
+    private static final String INPUT_STREAM = "inputStream";
+    private static final String OUTPUT_STREAM = "outputStream";
+
     private final ChannelInputStream chis = new ChannelInputStream();
     private final ChannelOutputStream chos = new ChannelOutputStream();
     private ChannelHandlerContext ctx;
@@ -39,29 +42,30 @@ public class VirtualSocket extends Socket implements ChannelHandler {
         return this.chos;
     }
 
-    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
+    public void handlerAdded(ChannelHandlerContext ctx) {
         this.ctx = ctx;
 
-        if (ctx.channel().pipeline().get("outputStream") == null) {
-            ctx.channel().pipeline().addFirst("outputStream", chos);
+        if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
+            ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chos);
         }
 
-        if (ctx.channel().pipeline().get("inputStream") == null) {
-            ctx.channel().pipeline().addFirst("inputStream", chis);
+        if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
+            ctx.channel().pipeline().addFirst(INPUT_STREAM, chis);
         }
     }
 
-    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
-        if (ctx.channel().pipeline().get("outputStream") != null) {
-            ctx.channel().pipeline().remove("outputStream");
+    public void handlerRemoved(ChannelHandlerContext ctx) {
+        if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
+            ctx.channel().pipeline().remove(OUTPUT_STREAM);
         }
 
-        if (ctx.channel().pipeline().get("inputStream") != null) {
-            ctx.channel().pipeline().remove("inputStream");
+        if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
+            ctx.channel().pipeline().remove(INPUT_STREAM);
         }
     }
 
-    public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
+        // TODO exceptionCaught is deprecated transform this handler
         ctx.fireExceptionCaught(throwable);
     }
 
@@ -80,7 +84,9 @@ public class VirtualSocket extends Socket implements ChannelHandler {
     public InetAddress getInetAddress() {
         InetSocketAddress isa = getInetSocketAddress();
 
-        if (isa == null) throw new VirtualSocketException();
+        if (isa == null) {
+            throw new VirtualSocketException();
+        }
 
         return getInetSocketAddress().getAddress();
     }
index f3ca30d2c40a4f924b0b35b5d196ba53295acb57..d1e064eaeb51cac6ed5462e11ccf0600214fd12d 100644 (file)
@@ -18,7 +18,7 @@ import java.util.regex.Pattern;
  * session's connection. Provided information can be reported via netconf
  * monitoring.
  * <pre>
- * It has pattern "[username; host-address:port; transport; session-identifier;]"
+ * It has PATTERN "[username; host-address:port; transport; session-identifier;]"
  * username - name of account on a remote
  * host-address - client's IP address
  * port - port number
@@ -92,20 +92,20 @@ public class NetconfHelloMessageAdditionalHeader {
     }
 
     // TODO IPv6
-    private static final Pattern pattern = Pattern
+    private static final Pattern PATTERN = Pattern
             .compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+)[^\\]]+\\]");
-    private static final Pattern customHeaderPattern = Pattern
+    private static final Pattern CUSTOM_HEADER_PATTERN = Pattern
             .compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+);(?<sessionIdentifier>[a-z]+)[^\\]]+\\]");
 
     /**
      * Parse additional header from a formatted string
      */
     public static NetconfHelloMessageAdditionalHeader fromString(String additionalHeader) {
-        additionalHeader = additionalHeader.trim();
-        Matcher matcher = pattern.matcher(additionalHeader);
-        Matcher matcher2 = customHeaderPattern.matcher(additionalHeader);
+        String additionalHeaderTrimmed = additionalHeader.trim();
+        Matcher matcher = PATTERN.matcher(additionalHeaderTrimmed);
+        Matcher matcher2 = CUSTOM_HEADER_PATTERN.matcher(additionalHeaderTrimmed);
         Preconditions.checkArgument(matcher.matches(), "Additional header in wrong format %s, expected %s",
-                additionalHeader, pattern);
+                additionalHeaderTrimmed, PATTERN);
 
         String username = matcher.group("username");
         String address = matcher.group("address");
index 0d83a72bd39b9f8e7fecdd7d9eb39d87f18ed6ef..8bd6b8865933d7191a063055834ce3fb98b0bbe4 100644 (file)
@@ -11,14 +11,19 @@ package org.opendaylight.controller.netconf.util.messages;
 import com.google.common.base.Charsets;
 
 public class NetconfMessageConstants {
+    /**
+     * The NETCONF 1.0 old-style message separator. This is framing mechanism
+     * is used by default.
+     */
+    public static final byte[] END_OF_MESSAGE = "]]>]]>".getBytes(Charsets.UTF_8);
 
-    public static final byte[] endOfMessage = "]]>]]>".getBytes(Charsets.UTF_8);
+    // bytes
 
-    public static final byte[] endOfChunk = "\n##\n".getBytes(Charsets.UTF_8);
+    public static final int MIN_HEADER_LENGTH = 4;
 
-    public static final int MAX_CHUNK_SIZE = 1024; // bytes
+    // bytes
 
-    public static final int MIN_HEADER_LENGTH = 4; // bytes
+    public static final int MAX_HEADER_LENGTH = 13;
 
-    public static final int MAX_HEADER_LENGTH = 13; // bytes
-}
\ No newline at end of file
+    public static final byte[] END_OF_CHUNK = "\n##\n".getBytes(Charsets.UTF_8);
+}
index a61541b9146556f7b71e0766e0c8b99a65a81fd9..fa26455c9875d7245429ef72f2ce11b57c56acac 100644 (file)
@@ -8,49 +8,29 @@
 
 package org.opendaylight.controller.netconf.util.messages;
 
+import java.nio.ByteBuffer;
+
 import com.google.common.base.Charsets;
 import com.google.common.base.Preconditions;
 
-import java.nio.ByteBuffer;
-
 /**
  * Netconf message header is used only when chunked framing mechanism is
  * supported. The header consists of only the length field.
  */
+@Deprecated
 public final class NetconfMessageHeader {
-
-    private long length;
-
     // \n#<length>\n
-    private static final byte[] headerBegin = new byte[] { (byte) 0x0a, (byte) 0x23 };
+    private static final byte[] HEADER_START = new byte[] { (byte) 0x0a, (byte) 0x23 };
+    private static final byte HEADER_END = (byte) 0x0a;
+    private final long length;
 
-    private static final byte headerEnd = (byte) 0x0a;
-
-    private boolean parsed = false;
-
-    public NetconfMessageHeader() {
-
-    }
-
-    public NetconfMessageHeader fromBytes(final byte[] bytes) {
-        // the length is variable therefore bytes between headerBegin and
-        // headerEnd mark the length
-        // the length should be only numbers and therefore easily parsed with
-        // ASCII
-        this.length = Long.parseLong(Charsets.US_ASCII.decode(
-                ByteBuffer.wrap(bytes, headerBegin.length, bytes.length - headerBegin.length - 1)).toString());
-        Preconditions.checkState(this.length < Integer.MAX_VALUE && this.length > 0);
-        this.parsed = true;
-        return this;
+    public NetconfMessageHeader(final long length) {
+        Preconditions.checkArgument(length < Integer.MAX_VALUE && length > 0);
+        this.length = length;
     }
 
     public byte[] toBytes() {
-        final byte[] l = String.valueOf(this.length).getBytes(Charsets.US_ASCII);
-        final byte[] h = new byte[headerBegin.length + l.length + 1];
-        System.arraycopy(headerBegin, 0, h, 0, headerBegin.length);
-        System.arraycopy(l, 0, h, headerBegin.length, l.length);
-        System.arraycopy(new byte[] { headerEnd }, 0, h, headerBegin.length + l.length, 1);
-        return h;
+        return toBytes(this.length);
     }
 
     // FIXME: improve precision to long
@@ -58,22 +38,23 @@ public final class NetconfMessageHeader {
         return (int) this.length;
     }
 
-    public void setLength(final int length) {
-        this.length = length;
-    }
+    public static NetconfMessageHeader fromBytes(final byte[] bytes) {
+        // the length is variable therefore bytes between headerBegin and
+        // headerEnd mark the length
+        // the length should be only numbers and therefore easily parsed with
+        // ASCII
+        long length = Long.parseLong(Charsets.US_ASCII.decode(
+                ByteBuffer.wrap(bytes, HEADER_START.length, bytes.length - HEADER_START.length - 1)).toString());
 
-    /**
-     * @return the parsed
-     */
-    public boolean isParsed() {
-        return this.parsed;
+        return new NetconfMessageHeader(length);
     }
 
-    /**
-     * @param parsed
-     *            the parsed to set
-     */
-    public void setParsed() {
-        this.parsed = false;
+    public static byte[] toBytes(final long length) {
+        final byte[] l = String.valueOf(length).getBytes(Charsets.US_ASCII);
+        final byte[] h = new byte[HEADER_START.length + l.length + 1];
+        System.arraycopy(HEADER_START, 0, h, 0, HEADER_START.length);
+        System.arraycopy(l, 0, h, HEADER_START.length, l.length);
+        System.arraycopy(new byte[] { HEADER_END }, 0, h, HEADER_START.length + l.length, 1);
+        return h;
     }
 }
index 46053e734ebf006154e26d5c8b7564ff6a36eedd..91eb86908b169ca9ecff8a77fb803b2d7ee60184 100644 (file)
@@ -13,7 +13,9 @@ import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
 import org.w3c.dom.Document;
 
-public class NetconfMessageUtil {
+public final class NetconfMessageUtil {
+
+    private NetconfMessageUtil() {}
 
     public static boolean isOKMessage(NetconfMessage message) {
         return isOKMessage(message.getDocument());
index 6dc00bb1509e97b0a485b89e90b0248a642e7a1a..de2d6d7e0c354ada3bcf1fb4f1b630b186c34e3c 100644 (file)
@@ -29,9 +29,11 @@ import javax.xml.xpath.XPathExpression;
 import java.io.InputStream;
 import java.util.Map.Entry;
 
-public class SendErrorExceptionUtil {
+public final class SendErrorExceptionUtil {
     private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
 
+    private SendErrorExceptionUtil() {}
+
     public static void sendErrorMessage(final NetconfSession session,
             final NetconfDocumentedException sendErrorException) {
         logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
@@ -68,8 +70,9 @@ public class SendErrorExceptionUtil {
             for (int i = 0; i < incomingAttributes.getLength(); i++) {
                 final Attr attr = (Attr) incomingAttributes.item(i);
                 // skip namespace
-                if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
+                if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
                     continue;
+                }
                 rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
             }
         } catch (final Exception e) {
index 38563cba93eef1f727330b17ff72879a012f70ff..80eaa26de184ddc29993bd847d1c34b0e7becd09 100644 (file)
@@ -17,12 +17,12 @@ import java.net.InetSocketAddress;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
-public class NetconfConfigUtil {
+public final class NetconfConfigUtil {
     private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
 
     private static final String PREFIX_PROP = "netconf.";
 
-
+    private NetconfConfigUtil() {}
 
     private enum InfixProp {
         tcp, ssh
index d9303228f95b55773c3a15e7ef6e0bc7b01cef71..3247e0f7a0e034402fcedda91b2b1e5ece7a1fe8 100644 (file)
@@ -13,7 +13,9 @@ import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
-public class XMLNetconfUtil {
+public final class XMLNetconfUtil {
+
+    private XMLNetconfUtil() {}
 
     public static XPathExpression compileXPath(String xPath) {
         final XPathFactory xPathfactory = XPathFactory.newInstance();
index 18a94c6d07ff7cf74289bfb5d3e057967a61e86c..1fbae1ee2d6e52c404742655ccd059e9038fd803 100644 (file)
@@ -31,9 +31,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-public class XmlElement {
+public final class XmlElement {
 
-    public final Element element;
+    private final Element element;
 
     private XmlElement(Element element) {
         this.element = element;
@@ -123,9 +123,6 @@ public class XmlElement {
 
     public void appendChild(Element element) {
         this.element.appendChild(element);
-        // Element newElement = (Element) element.cloneNode(true);
-        // newElement.appendChild(configElement);
-        // return XmlElement.fromDomElement(newElement);
     }
 
     public Element getDomElement() {
@@ -153,10 +150,12 @@ public class XmlElement {
         final List<XmlElement> result = new ArrayList<>();
         for (int i = 0; i < childNodes.getLength(); i++) {
             Node item = childNodes.item(i);
-            if (item instanceof Element == false)
+            if (item instanceof Element == false) {
                 continue;
-            if (strat.accept((Element) item))
+            }
+            if (strat.accept((Element) item)) {
                 result.add(new XmlElement((Element) item));
+            }
         }
 
         return result;
@@ -290,12 +289,12 @@ public class XmlElement {
     public String getNamespace() {
         String namespaceURI = element.getNamespaceURI();
         Preconditions.checkState(namespaceURI != null, "No namespace defined for %s", this);
-        return namespaceURI.toString();
+        return namespaceURI;
     }
 
     @Override
     public String toString() {
-        final StringBuffer sb = new StringBuffer("XmlElement{");
+        final StringBuilder sb = new StringBuilder("XmlElement{");
         sb.append("name='").append(getName()).append('\'');
         if (element.getNamespaceURI() != null) {
             sb.append(", namespace='").append(getNamespace()).append('\'');
@@ -320,7 +319,7 @@ public class XmlElement {
     public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() {
         Map<String, String> namespaces = extractNamespaces(element);
         String textContent = getTextContent();
-        int indexOfColon = textContent.indexOf(":");
+        int indexOfColon = textContent.indexOf(':');
         String prefix;
         if (indexOfColon > -1) {
             prefix = textContent.substring(0, indexOfColon);
@@ -360,15 +359,18 @@ public class XmlElement {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o)
+        if (this == o) {
             return true;
-        if (o == null || getClass() != o.getClass())
+        }
+        if (o == null || getClass() != o.getClass()) {
             return false;
+        }
 
         XmlElement that = (XmlElement) o;
 
-        if (!element.isEqualNode(that.element))
+        if (!element.isEqualNode(that.element)) {
             return false;
+        }
 
         return true;
     }
@@ -392,7 +394,7 @@ public class XmlElement {
         return true;
     }
 
-    private static interface ElementFilteringStrategy {
+    private interface ElementFilteringStrategy {
         boolean accept(Element e);
     }
 }
index c410cf30b0abd00290bbc178c5ea3c9cbc7c0a21..4a6586cd1d8bf8ee0e9e4cbef66e62048cf1e5e6 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.controller.netconf.util.xml;
 
-public class XmlNetconfConstants {
+public final class XmlNetconfConstants {
+
+    private XmlNetconfConstants() {}
 
     public static final String MOUNTPOINTS = "mountpoints";
     public static final String MOUNTPOINT = "mountpoint";
index de0ebccdca428d8285d26d74b2f4e9fd54b5d353..8108c8e7402fac29469cf7804efd96e1dc6455df 100644 (file)
@@ -21,8 +21,11 @@ import org.xml.sax.SAXException;
 
 import com.google.common.base.Preconditions;
 
-public class XmlNetconfValidator {
-    static final Schema schema;
+public final class XmlNetconfValidator {
+
+    private static final Schema SCHEMA;
+
+    private XmlNetconfValidator() {}
 
     static {
         final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
@@ -30,11 +33,11 @@ public class XmlNetconfValidator {
 
         final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
         Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
-        schema = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
+        SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
     }
 
     public static void validate(Document inputDocument) throws SAXException, IOException {
-        final Validator validator = schema.newValidator();
+        final Validator validator = SCHEMA.newValidator();
         final Source source = new DOMSource(inputDocument);
         validator.validate(source);
     }
index 137e215a31eb13423b6438671faed529a2fc7ce3..238249dbbd8d49086b38c1696cb6d0cb685909fe 100644 (file)
@@ -41,9 +41,21 @@ import org.xml.sax.SAXException;
 
 import com.google.common.base.Charsets;
 
-public class XmlUtil {
+public final class XmlUtil {
 
     public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
+    private static final DocumentBuilderFactory BUILDERFACTORY;
+
+    static {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setCoalescing(true);
+        factory.setIgnoringElementContentWhitespace(true);
+        factory.setIgnoringComments(true);
+        BUILDERFACTORY = factory;
+    }
+
+    private XmlUtil() {}
 
     public static Element readXmlToElement(String xmlContent) throws SAXException, IOException {
         Document doc = readXmlToDocument(xmlContent);
@@ -59,13 +71,15 @@ public class XmlUtil {
         return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
     }
 
+    // TODO improve exceptions throwing
+    // along with XmlElement
+
     public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException {
-        DocumentBuilderFactory factory = getDocumentBuilderFactory();
         DocumentBuilder dBuilder;
         try {
-            dBuilder = factory.newDocumentBuilder();
+            dBuilder = BUILDERFACTORY.newDocumentBuilder();
         } catch (ParserConfigurationException e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException("Failed to parse XML document", e);
         }
         Document doc = dBuilder.parse(xmlContent);
 
@@ -77,23 +91,13 @@ public class XmlUtil {
         return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
     }
 
-    private static final DocumentBuilderFactory getDocumentBuilderFactory() {
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setNamespaceAware(true);
-        factory.setCoalescing(true);
-        factory.setIgnoringElementContentWhitespace(true);
-        factory.setIgnoringComments(true);
-        return factory;
-    }
-
     public static Document newDocument() {
-        DocumentBuilderFactory factory = getDocumentBuilderFactory();
         try {
-            DocumentBuilder builder = factory.newDocumentBuilder();
+            DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder();
             Document document = builder.newDocument();
             return document;
         } catch (ParserConfigurationException e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException("Failed to create document", e);
         }
     }
 
@@ -135,14 +139,13 @@ public class XmlUtil {
         try {
             Transformer transformer = TransformerFactory.newInstance().newTransformer();
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration == true ? "no" : "yes");
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
 
             StreamResult result = new StreamResult(new StringWriter());
             DOMSource source = new DOMSource(xml);
             transformer.transform(source, result);
 
-            String xmlString = result.getWriter().toString();
-            return xmlString;
+            return result.getWriter().toString();
         } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
             throw new RuntimeException("Unable to serialize xml element " + xml, e);
         }
index 6b7bffcd862394c678b1dfa69e65bbaad9481f89..18830f85ddcdc8595bc7be40dc8f439d1dc3bec4 100644 (file)
@@ -16,14 +16,13 @@ import java.util.List;
 
 import org.junit.Test;
 import org.opendaylight.controller.netconf.util.handler.NetconfXMLToHelloMessageDecoder;
-import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
 
 import com.google.common.io.Files;
 
 public class NetconfMessageFactoryTest {
     @Test
     public void testAuth() throws Exception {
-        NetconfXMLToMessageDecoder parser = new NetconfXMLToHelloMessageDecoder();
+        NetconfXMLToHelloMessageDecoder parser = new NetconfXMLToHelloMessageDecoder();
         File authHelloFile = new File(getClass().getResource("/netconfMessages/client_hello_with_auth.xml").getFile());
 
         final List<Object> out = new ArrayList<>();
index b601eef4b5ef134423d563c86327aabd201bd495..f67828bdab68fb678a67c038568b34d3a87321af 100644 (file)
     <version>1.1-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
-    <properties>
-        <ganymed.version>build209</ganymed.version>
-    </properties>
-
     <dependencies>
         <dependency>
             <groupId>org.osgi</groupId>