Reduce technical debt, by removing some code smells 46/94046/7
authorJoeOLeary <joseph.o.leary@est.tech>
Thu, 22 Aug 2019 13:26:29 +0000 (13:26 +0000)
committerJoeOLeary <joseph.o.leary@est.tech>
Thu, 22 Aug 2019 13:26:29 +0000 (13:26 +0000)
Issue-ID: DCAEGEN2-1731
Change-Id: I3d0f8596d2b62300be452992d8020f4cee559ad4
Signed-off-by: JoeOLeary <joseph.o.leary@est.tech>
src/main/java/org/onap/dcaegen2/services/pmmapper/App.java
src/main/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfiguration.java
src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DeliveryHandler.java
src/main/java/org/onap/dcaegen2/services/pmmapper/healthcheck/HealthCheckHandler.java
src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerHandler.java [deleted file]
src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerResource.java [new file with mode: 0644]
src/main/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactory.java

index fdb4ee4..c7d56e5 100644 (file)
@@ -42,7 +42,7 @@ import org.onap.dcaegen2.services.pmmapper.messagerouter.VESPublisher;
 import org.onap.dcaegen2.services.pmmapper.model.Event;
 import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
 import org.onap.dcaegen2.services.pmmapper.healthcheck.HealthCheckHandler;
-import org.onap.dcaegen2.services.pmmapper.model.ServerHandler;
+import org.onap.dcaegen2.services.pmmapper.model.ServerResource;
 import org.onap.dcaegen2.services.pmmapper.ssl.SSLContextFactory;
 import org.onap.dcaegen2.services.pmmapper.utils.DataRouterUtils;
 import org.onap.dcaegen2.services.pmmapper.utils.MeasConverter;
@@ -89,7 +89,7 @@ public class App {
     private int httpsPort;
 
     private Undertow applicationServer;
-    private List<ServerHandler> serverHandlers;
+    private List<ServerResource> serverResources;
     private Flux<Event> flux;
     private FluxSink<Event> fluxSink;
 
@@ -137,9 +137,9 @@ public class App {
         this.healthCheckHandler = new HealthCheckHandler();
         this.deliveryHandler = new DeliveryHandler(fluxSink::next);
         this.dynamicConfiguration = new DynamicConfiguration(Arrays.asList(mapperConfig), mapperConfig);
-        this.serverHandlers = Arrays.asList(healthCheckHandler, deliveryHandler, dynamicConfiguration);
+        this.serverResources = Arrays.asList(healthCheckHandler, deliveryHandler, dynamicConfiguration);
         try {
-            this.applicationServer = server(this.mapperConfig, this.serverHandlers);
+            this.applicationServer = server(this.mapperConfig, this.serverResources);
         } catch (IOException e) {
             logger.unwrap().error("Failed to create server instance.", e);
             throw new IllegalStateException("Server instantiation failed");
@@ -160,7 +160,7 @@ public class App {
         this.applicationServer.stop();
     }
 
-    private Undertow server(MapperConfig config, List<ServerHandler> serverHandlers) throws IOException {
+    private Undertow server(MapperConfig config, List<ServerResource> serverResources) throws IOException {
         SSLContextFactory sslContextFactory = new SSLContextFactory(config);
         SSLContext sslContext = sslContextFactory.createSSLContext(config);
         SSLContext.setDefault(sslContext);
@@ -169,7 +169,7 @@ public class App {
             builder.addHttpListener(this.httpPort, "0.0.0.0");
         }
         RoutingHandler routes = new RoutingHandler();
-        serverHandlers.forEach(handler -> routes.add(handler.getMethod(), handler.getTemplate(), handler.getHandler()));
+        serverResources.forEach(handler -> routes.add(handler.getHTTPMethod(), handler.getEndpointTemplate(), handler.getHandler()));
         return builder.addHttpsListener(this.httpsPort, "0.0.0.0", sslContext)
                 .setHandler(routes)
                 .build();
index 4516183..420081a 100644 (file)
 
 package org.onap.dcaegen2.services.pmmapper.config;
 
-import io.undertow.server.HttpHandler;
 import io.undertow.server.HttpServerExchange;
 import io.undertow.util.StatusCodes;
 import java.util.List;
 import lombok.Data;
 import org.onap.dcaegen2.services.pmmapper.exceptions.ReconfigurationException;
 import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
-import org.onap.dcaegen2.services.pmmapper.model.ServerHandler;
+import org.onap.dcaegen2.services.pmmapper.model.ServerResource;
 import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter;
 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
 import org.slf4j.LoggerFactory;
 
 @Data
-public class DynamicConfiguration implements HttpHandler, ServerHandler {
+public class DynamicConfiguration extends ServerResource {
     private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(DynamicConfiguration.class));
-    private static final String METHOD = "get";
-    private static final String ENDPOINT_TEMPLATE = "/reconfigure";
+    private static final String RECONFIGURE_ENDPOINT = "/reconfigure";
     private List<Configurable> configurables;
     private MapperConfig originalConfig;
     private ConfigHandler configHandler;
@@ -47,6 +45,7 @@ public class DynamicConfiguration implements HttpHandler, ServerHandler {
      * @param originalConfig original config to compare against.
      */
     public DynamicConfiguration(List<Configurable> configurables, MapperConfig originalConfig) {
+        super(RECONFIGURE_ENDPOINT);
         this.configurables = configurables;
         this.originalConfig = originalConfig;
         this.configHandler = new ConfigHandler();
@@ -90,19 +89,4 @@ public class DynamicConfiguration implements HttpHandler, ServerHandler {
             logger.exiting();
         }
     }
-
-    @Override
-    public String getMethod() {
-        return METHOD;
-    }
-
-    @Override
-    public String getTemplate() {
-        return ENDPOINT_TEMPLATE;
-    }
-
-    @Override
-    public HttpHandler getHandler() {
-        return this;
-    }
 }
index ba218cb..1ba67ec 100644 (file)
@@ -20,6 +20,8 @@
 
 package org.onap.dcaegen2.services.pmmapper.datarouter;
 
+import static io.undertow.util.Methods.PUT_STRING;
+
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonParseException;
@@ -30,11 +32,10 @@ import lombok.NonNull;
 import org.onap.dcaegen2.services.pmmapper.exceptions.NoMetadataException;
 import org.onap.dcaegen2.services.pmmapper.model.EventMetadata;
 import org.onap.dcaegen2.services.pmmapper.model.Event;
-import io.undertow.server.HttpHandler;
 import io.undertow.server.HttpServerExchange;
 import io.undertow.util.StatusCodes;
 
-import org.onap.dcaegen2.services.pmmapper.model.ServerHandler;
+import org.onap.dcaegen2.services.pmmapper.model.ServerResource;
 import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter;
 import org.onap.dcaegen2.services.pmmapper.utils.RequiredFieldDeserializer;
 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
@@ -48,7 +49,7 @@ import java.util.Optional;
  * Provides an undertow HttpHandler to be used as an endpoint for data router to send events to.
  */
 @Data
-public class DeliveryHandler implements HttpHandler, ServerHandler {
+public class DeliveryHandler extends ServerResource {
 
     public static final String METADATA_HEADER = "X-DMAAP-DR-META";
     public static final String PUB_ID_HEADER = "X-DMAAP-DR-PUBLISH-ID";
@@ -57,8 +58,8 @@ public class DeliveryHandler implements HttpHandler, ServerHandler {
 
     private static final String BAD_METADATA_MESSAGE = "Malformed Metadata.";
     private static final String NO_METADATA_MESSAGE = "Missing Metadata.";
-    private static final String METHOD = "put";
-    private static final String ENDPOINT_TEMPLATE = "/delivery/{filename}";
+    private static final String HTTP_METHOD = PUT_STRING;
+    private static final String DELIVERY_ENDPOINT = "/delivery/{filename}";
 
     private Gson metadataBuilder;
 
@@ -69,6 +70,7 @@ public class DeliveryHandler implements HttpHandler, ServerHandler {
      * @param eventReceiver receiver for any inbound events.
      */
     public DeliveryHandler(EventReceiver eventReceiver) {
+        super(HTTP_METHOD, DELIVERY_ENDPOINT);
         this.eventReceiver = eventReceiver;
         this.metadataBuilder = new GsonBuilder()
                 .registerTypeAdapter(EventMetadata.class, new RequiredFieldDeserializer<EventMetadata>())
@@ -92,7 +94,7 @@ public class DeliveryHandler implements HttpHandler, ServerHandler {
      */
     @Override
     public void handleRequest(HttpServerExchange httpServerExchange) {
-        try{
+        try {
             logger.entering(new HttpServerExchangeAdapter(httpServerExchange));
             try {
                 Map<String,String> mdc = MDC.getCopyOfContextMap();
@@ -119,19 +121,4 @@ public class DeliveryHandler implements HttpHandler, ServerHandler {
             logger.exiting();
         }
     }
-
-    @Override
-    public String getMethod() {
-        return METHOD;
-    }
-
-    @Override
-    public String getTemplate() {
-        return ENDPOINT_TEMPLATE;
-    }
-
-    @Override
-    public HttpHandler getHandler() {
-        return this;
-    }
 }
index ddf55c7..719b3a9 100644 (file)
 
 package org.onap.dcaegen2.services.pmmapper.healthcheck;
 
-import org.onap.dcaegen2.services.pmmapper.model.ServerHandler;
+import org.onap.dcaegen2.services.pmmapper.model.ServerResource;
 import org.onap.dcaegen2.services.pmmapper.utils.HttpServerExchangeAdapter;
 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
 import org.slf4j.LoggerFactory;
 
-import io.undertow.server.HttpHandler;
 import io.undertow.server.HttpServerExchange;
 import io.undertow.util.StatusCodes;
 
-public class HealthCheckHandler implements HttpHandler, ServerHandler {
+public class HealthCheckHandler extends ServerResource {
     private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(HealthCheckHandler.class));
-    private static final String METHOD = "get";
-    private static final String ENDPOINT_TEMPLATE = "/healthcheck";
+    private static final String HEALTHCHECK_ENDPOINT = "/healthcheck";
+
+    public HealthCheckHandler() {
+        super(HEALTHCHECK_ENDPOINT);
+    }
 
     @Override
     public void handleRequest(HttpServerExchange exchange) {
@@ -48,19 +50,4 @@ public class HealthCheckHandler implements HttpHandler, ServerHandler {
             logger.exiting();
         }
     }
-
-    @Override
-    public String getMethod() {
-        return METHOD;
-    }
-
-    @Override
-    public String getTemplate() {
-        return ENDPOINT_TEMPLATE;
-    }
-
-    @Override
-    public HttpHandler getHandler() {
-        return this;
-    }
 }
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerHandler.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerHandler.java
deleted file mode 100644 (file)
index 9212375..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * -
- *  * ============LICENSE_START=======================================================
- *  *  Copyright (C) 2019 Nordix Foundation.
- *  * ================================================================================
- *  * Licensed under the Apache License, Version 2.0 (the "License");
- *  * you may not use this file except in compliance with the License.
- *  * You may obtain a copy of the License at
- *  *
- *  *      http://www.apache.org/licenses/LICENSE-2.0
- *  *
- *  * Unless required by applicable law or agreed to in writing, software
- *  * distributed under the License is distributed on an "AS IS" BASIS,
- *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  * See the License for the specific language governing permissions and
- *  * limitations under the License.
- *  *
- *  * SPDX-License-Identifier: Apache-2.0
- *  * ============LICENSE_END=========================================================
- *
- */
-
-package org.onap.dcaegen2.services.pmmapper.model;
-
-import io.undertow.server.HttpHandler;
-
-public interface ServerHandler {
-
-    String getMethod();
-
-    String getTemplate();
-
-    HttpHandler getHandler();
-}
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerResource.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/ServerResource.java
new file mode 100644 (file)
index 0000000..5c15280
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * -
+ *  * ============LICENSE_START=======================================================
+ *  *  Copyright (C) 2019 Nordix Foundation.
+ *  * ================================================================================
+ *  * Licensed under the Apache License, Version 2.0 (the "License");
+ *  * you may not use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *      http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.
+ *  *
+ *  * SPDX-License-Identifier: Apache-2.0
+ *  * ============LICENSE_END=========================================================
+ *
+ */
+
+package org.onap.dcaegen2.services.pmmapper.model;
+
+import static io.undertow.util.Methods.GET_STRING;
+
+import io.undertow.server.HttpHandler;
+
+public abstract class ServerResource implements HttpHandler {
+    protected String httpMethod;
+    protected String endpointTemplate;
+
+    /**
+     * Creates a new server resource with a custom method and endpoint.
+     * @param httpMethod Method that the resource should be accessible by.
+     * @param endpointTemplate Endpoint that the resource should be accessible by.
+     */
+    public ServerResource(String httpMethod, String endpointTemplate) {
+        this.httpMethod = httpMethod;
+        this.endpointTemplate = endpointTemplate;
+    }
+
+    /**
+     * Creates a new server resource with a custom endpoint and method 'get'.
+     * @param endpointTemplate Endpoint that the resource should be accessible by.
+     */
+    public ServerResource(String endpointTemplate) {
+        this.httpMethod = GET_STRING;
+        this.endpointTemplate = endpointTemplate;
+    }
+
+    public String getHTTPMethod() {
+        return this.httpMethod;
+    }
+
+    public String getEndpointTemplate() {
+        return this.endpointTemplate;
+    }
+
+     public HttpHandler getHandler() {
+        return this;
+    }
+}
index 68e63f5..0bfdaa6 100644 (file)
@@ -48,7 +48,7 @@ import static java.nio.file.Files.readAllBytes;
 
 public class SSLContextFactory {
     private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(SSLContextFactory.class));
-    private static MapperConfig mapperConfig;
+    private MapperConfig mapperConfig;
 
     public SSLContextFactory(MapperConfig config) {
         mapperConfig = config;