added isInterface test for listener registration 08/5308/17
authorMartin Bobak <mbobak@cisco.com>
Thu, 13 Feb 2014 17:18:09 +0000 (18:18 +0100)
committerMartin Bobak <mbobak@cisco.com>
Wed, 26 Feb 2014 11:50:25 +0000 (12:50 +0100)
AuthenticationHolder for rest authentication

Change-Id: Idf0dfe2ce40d862490eb52b2344e7974d14da148
Signed-off-by: Martin Bobak <mbobak@cisco.com>
restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/RestconfClientContext.java
restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/RestconfClientContextFactory.java
restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/AuthenticationHolder.java [new file with mode: 0644]
restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/RestAuthType.java [new file with mode: 0644]
restconf/restconf-client-impl/src/main/java/org/opendaylight/yangtools/restconf/client/BindingToRestRpc.java
restconf/restconf-client-impl/src/main/java/org/opendaylight/yangtools/restconf/client/RestconfClientFactory.java
restconf/restconf-client-impl/src/main/java/org/opendaylight/yangtools/restconf/client/RestconfClientImpl.java
restconf/restconf-client-impl/src/main/java/org/opendaylight/yangtools/restconf/client/to/RestListenableEventStreamContext.java
restconf/restconf-client-impl/src/test/java/org/opendaylight/yangtools/restconf/client/AuthProvider.java [new file with mode: 0644]

index 6c7c52cb76c661b3f8b3a46ddc63e9e3f5d27246..5fd3e27647161fc9848cd0c76979c7d85cefdc9d 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.restconf.client.api;
 
 import java.util.Set;
 
+import org.opendaylight.yangtools.restconf.client.api.auth.AuthenticationHolder;
 import org.opendaylight.yangtools.restconf.client.api.data.ConfigurationDatastore;
 import org.opendaylight.yangtools.restconf.client.api.data.OperationalDatastore;
 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamInfo;
index 9ed55907a22767dc12bcb90e9df3b0f3d092bc56..d5b345336a4a3dbad7f66bfa128d47414dbf14e8 100644 (file)
@@ -8,7 +8,7 @@
 package org.opendaylight.yangtools.restconf.client.api;
 
 import java.net.URL;
-
+import org.opendaylight.yangtools.restconf.client.api.auth.AuthenticationHolder;
 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
 import org.opendaylight.yangtools.yang.model.api.SchemaContextHolder;
 
@@ -23,4 +23,6 @@ public interface RestconfClientContextFactory {
         * @throws UnsupportedProtocolException if the factory cannot handle the protocol specified in the URL.
         */
        RestconfClientContext getRestconfClientContext(URL baseUrl,BindingIndependentMappingService mappingService,SchemaContextHolder holder) throws UnsupportedProtocolException;
+    void setAuthentication(AuthenticationHolder authenticationHolder);
+
 }
diff --git a/restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/AuthenticationHolder.java b/restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/AuthenticationHolder.java
new file mode 100644 (file)
index 0000000..c6031c6
--- /dev/null
@@ -0,0 +1,12 @@
+package org.opendaylight.yangtools.restconf.client.api.auth;
+
+/**
+ * Created by mbobak on 2/14/14.
+ */
+public interface AuthenticationHolder {
+
+    public RestAuthType getAuthType();
+    public String getUserName();
+    public String getPassword();
+    public boolean authenticationRequired();
+}
diff --git a/restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/RestAuthType.java b/restconf/restconf-client-api/src/main/java/org/opendaylight/yangtools/restconf/client/api/auth/RestAuthType.java
new file mode 100644 (file)
index 0000000..c858810
--- /dev/null
@@ -0,0 +1,9 @@
+package org.opendaylight.yangtools.restconf.client.api.auth;
+
+/**
+ * Created by mbobak on 2/17/14.
+ */
+public enum RestAuthType {
+
+    DIGEST,BASIC;
+}
index a13396f8a2f0885967d29d2e01fb5070688251ea..deacc8422d6689335cb2751cfdc79609b7a1c69a 100644 (file)
@@ -12,6 +12,7 @@ import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
 import java.io.InputStream;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
@@ -42,6 +43,7 @@ public class BindingToRestRpc implements InvocationHandler {
     public BindingToRestRpc(URI uri) {
         ClientConfig config = new DefaultClientConfig();
         this.client  = Client.create(config);
+        this.client.addFilter(new HTTPBasicAuthFilter("admin", "admin"));
         this.defaultUri = uri;
     }
 
@@ -73,7 +75,7 @@ public class BindingToRestRpc implements InvocationHandler {
 
 
                     if (response.getStatus() != 200) {
-                        throw new IllegalStateException("Can't get data from restconf ");
+                        throw new IllegalStateException("Can't get data from restconf. "+response.getClientResponseStatus());
                     }
                     return XmlTools.unmarshallXml(method.getReturnType(), response.getEntityInputStream());
                 }
index 829e57aac4b7136ba3895444a97cc4ac7ce4275d..c1fb969fc0cfc3196952b1f1b0be2f74b1b08dc7 100644 (file)
@@ -8,10 +8,10 @@
 package org.opendaylight.yangtools.restconf.client;
 
 import java.net.URL;
-
 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContextFactory;
 import org.opendaylight.yangtools.restconf.client.api.UnsupportedProtocolException;
+import org.opendaylight.yangtools.restconf.client.api.auth.AuthenticationHolder;
 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
 import org.opendaylight.yangtools.yang.model.api.SchemaContextHolder;
 import org.slf4j.Logger;
@@ -20,12 +20,22 @@ import org.slf4j.LoggerFactory;
 public class RestconfClientFactory implements RestconfClientContextFactory {
 
     private static final Logger logger = LoggerFactory.getLogger(RestconfClientFactory.class);
+    private AuthenticationHolder authenticationHolder;
 
     @Override
     public RestconfClientContext getRestconfClientContext(URL baseUrl, BindingIndependentMappingService mappingService, SchemaContextHolder schemaContextHolder) throws UnsupportedProtocolException {
         if (!baseUrl.getProtocol().equals("http")){
             throw new UnsupportedProtocolException("Unsupported protocol "+baseUrl.getProtocol());
         }
-        return new RestconfClientImpl(baseUrl,mappingService,schemaContextHolder);
+        RestconfClientImpl restconfClient = new RestconfClientImpl(baseUrl,mappingService,schemaContextHolder);
+        if (null!=authenticationHolder){
+            restconfClient.setAuthenticationHolder(authenticationHolder);
+        }
+        return restconfClient;
+    }
+
+    @Override
+    public void setAuthentication(AuthenticationHolder authenticationHolder) {
+        this.authenticationHolder = authenticationHolder;
     }
 }
index d1c45a8c953311155dc570f9a39212d345663026..9f3f2e3f401c30c8608cbc8007565adfc7dffb68 100644 (file)
@@ -17,6 +17,8 @@ import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
+import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
+import com.sun.jersey.api.client.filter.HTTPDigestAuthFilter;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -26,6 +28,7 @@ import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Executors;
 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
+import org.opendaylight.yangtools.restconf.client.api.auth.AuthenticationHolder;
 import org.opendaylight.yangtools.restconf.client.api.data.ConfigurationDatastore;
 import org.opendaylight.yangtools.restconf.client.api.data.OperationalDatastore;
 import org.opendaylight.yangtools.restconf.client.api.dto.RestEventStreamInfo;
@@ -64,8 +67,8 @@ public class RestconfClientImpl implements RestconfClientContext, SchemaContextL
     private OperationalDataStoreImpl operationalDatastoreAccessor;
     private ConfigurationDataStoreImpl configurationDatastoreAccessor;
 
-
-    public RestconfClientImpl(URL url,BindingIndependentMappingService mappingService, SchemaContextHolder schemaContextHolder){
+    public RestconfClientImpl(URL url,BindingIndependentMappingService mappingService,
+                              SchemaContextHolder schemaContextHolder){
         Preconditions.checkArgument(url != null,"Restconf endpoint URL must be supplied.");
         Preconditions.checkArgument(mappingService != null, "Mapping service must not be null.");
         Preconditions.checkNotNull(schemaContextHolder, "Schema Context Holder must not be null.");
@@ -173,6 +176,17 @@ public class RestconfClientImpl implements RestconfClientContext, SchemaContextL
         return operationalDatastoreAccessor;
     }
 
+    public void setAuthenticationHolder(AuthenticationHolder authenticationHolder) {
+        if(authenticationHolder.authenticationRequired()){
+            switch (authenticationHolder.getAuthType()){
+                case DIGEST: restClient.addFilter(new HTTPDigestAuthFilter(authenticationHolder.getUserName(), authenticationHolder.getPassword()));
+                    break;
+                default: restClient.addFilter(new HTTPBasicAuthFilter(authenticationHolder.getUserName(), authenticationHolder.getPassword()));
+                    break;
+            }
+        }
+    }
+
     @Override
     public void close() {
         this.pool.shutdown();
index 7916e4f7a31cdb748d5a8a3563e151b990a976f9..ee6e4b2d3ba3a9aae580fa203f24cde42fa3e916 100644 (file)
@@ -7,8 +7,17 @@
  */
 package org.opendaylight.yangtools.restconf.client.to;
 
+import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
-
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -17,13 +26,10 @@ import java.net.URLEncoder;
 import java.util.Date;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Executors;
-
 import javax.ws.rs.core.MediaType;
 import javax.xml.bind.annotation.XmlRootElement;
-
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.restconf.client.BindingToRestRpc;
 import org.opendaylight.yangtools.restconf.client.api.event.EventStreamReplay;
 import org.opendaylight.yangtools.restconf.client.api.event.ListenableEventStreamContext;
 import org.opendaylight.yangtools.restconf.common.ResourceUri;
@@ -35,21 +41,10 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.MoreExecutors;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.api.client.config.ClientConfig;
-import com.sun.jersey.api.client.config.DefaultClientConfig;
-
 
 
 @XmlRootElement
-public class RestListenableEventStreamContext<T extends NotificationListener> implements ListenableEventStreamContext,ClientMessageCallback {
+public class RestListenableEventStreamContext<L extends NotificationListener> implements ListenableEventStreamContext,ClientMessageCallback {
 
     private final URI defaultUri;
     private final Client client;
@@ -71,25 +66,16 @@ public class RestListenableEventStreamContext<T extends NotificationListener> im
     @Override
     public <L extends NotificationListener> ListenerRegistration<L> registerNotificationListener(L listener) {
 
-        try {
-            this.streamName = BindingReflections.getModuleInfo(listener.getClass()).getName();
-        } catch (Exception e) {
-            logger.trace("Error resolving stream name form listener class.");
-            throw new IllegalStateException("Error resolving stream name form listener class.");
-        }
-
         for (Method m:listener.getClass().getDeclaredMethods()){
             if (BindingReflections.isNotificationCallback(m)){
                 this.listenerCallbackMethod = m;
                 break;
             }
         }
-
-        final L listenerProxy = (L) BindingToRestRpc.getProxy(listener.getClass(), this.defaultUri);
-        return new AbstractListenerRegistration<L>(listenerProxy) {
+        return new AbstractListenerRegistration<L>(listener) {
             @Override
             protected void removeRegistration() {
-                // FIXME: implement this method
+                stopListening();
             }
         };
     }
@@ -157,13 +143,6 @@ public class RestListenableEventStreamContext<T extends NotificationListener> im
     private void createWebsocketClient(URI websocketServerUri){
         this.wsClient = new WebSocketIClient(websocketServerUri,this);
     }
-    private String getRpcInput(String path,String ns) {
-        StringBuilder sb = new StringBuilder();
-        sb.append("<input xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote\">");
-        sb.append("<path xmlns:int=\""+ns+"\">"+path+"</path>");
-        sb.append("</input>");
-        return sb.toString();
-    }
 
     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
diff --git a/restconf/restconf-client-impl/src/test/java/org/opendaylight/yangtools/restconf/client/AuthProvider.java b/restconf/restconf-client-impl/src/test/java/org/opendaylight/yangtools/restconf/client/AuthProvider.java
new file mode 100644 (file)
index 0000000..85935c0
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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.yangtools.restconf.client;
+
+import org.opendaylight.yangtools.restconf.client.api.auth.AuthenticationHolder;
+import org.opendaylight.yangtools.restconf.client.api.auth.RestAuthType;
+
+public class AuthProvider implements AuthenticationHolder {
+
+
+    @Override
+    public RestAuthType getAuthType() {
+        return RestAuthType.BASIC;
+    }
+
+    @Override
+    public String getUserName() {
+        return "admin";
+    }
+
+    @Override
+    public String getPassword() {
+        return "admin";
+    }
+
+    @Override
+    public boolean authenticationRequired() {
+        return false;
+    }
+}