From 7b8d18c2d8893e735e2809609361071bb702b53f Mon Sep 17 00:00:00 2001
From: "k.kedron" <k.kedron@partner.samsung.com>
Date: Thu, 18 Apr 2019 13:42:40 +0200
Subject: Fixed generic Sonar issue in the clamp project

Fixed Sonar issue:
- name convection for static field in AuthorizationController
- optimize condition flow in AuthorizationController
- remove not used import in BlueprintParserMappingConfiguration
- remove not used field in AuthorizationController, SdcControllersConfiguration, CldsService
- add private constructor to the AwtUtils
- change redundant EnableAutoConfiguration annotation to SpringBootApplication annotation
- remove duplications of code in the CldsDao

Change-Id: I49d8be9ec44b5930f76bace0fc9d8cf61ee000f5
Issue-ID: CLAMP-346
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
---
 .../authorization/AuthorizationController.java     | 62 +++++-----------
 src/main/java/org/onap/clamp/clds/Application.java |  5 +-
 .../clamp/clds/config/CldsUserJsonDecoder.java     |  8 +-
 .../sdc/BlueprintParserMappingConfiguration.java   |  3 -
 .../config/sdc/SdcControllersConfiguration.java    |  8 +-
 src/main/java/org/onap/clamp/clds/dao/CldsDao.java | 85 +++++++++++++---------
 .../org/onap/clamp/clds/service/CldsService.java   |  3 -
 .../org/onap/clamp/clds/util/drawing/AwtUtils.java |  5 ++
 8 files changed, 87 insertions(+), 92 deletions(-)

(limited to 'src/main/java/org')

diff --git a/src/main/java/org/onap/clamp/authorization/AuthorizationController.java b/src/main/java/org/onap/clamp/authorization/AuthorizationController.java
index 511b9509..4a35f458 100644
--- a/src/main/java/org/onap/clamp/authorization/AuthorizationController.java
+++ b/src/main/java/org/onap/clamp/authorization/AuthorizationController.java
@@ -5,6 +5,8 @@
  * Copyright (C) 2019 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -39,8 +41,6 @@ import org.onap.clamp.util.PrincipalUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.GrantedAuthority;
-import org.springframework.security.core.context.SecurityContext;
-import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Component;
 
 /**
@@ -57,12 +57,8 @@ public class AuthorizationController {
     @Autowired
     private ClampProperties refProp;
 
-    private SecurityContext securityContext = SecurityContextHolder.getContext();
-    private static final String permPrefix = "security.permission.type.";
-    private static final String permInstance = "security.permission.instance";
-
-    public AuthorizationController() {
-    }
+    private static final String PERM_PREFIX = "security.permission.type.";
+    private static final String PERM_INSTANCE = "security.permission.instance";
 
     /**
      * Insert authorize the api based on the permission
@@ -77,8 +73,8 @@ public class AuthorizationController {
      *        The action of the permissions. e.g. read
      */
     public void authorize(Exchange camelExchange, String typeVar, String instanceVar, String action) {
-        String type = refProp.getStringValue(permPrefix + typeVar);
-        String instance = refProp.getStringValue(permInstance);
+        String type = refProp.getStringValue(PERM_PREFIX + typeVar);
+        String instance = refProp.getStringValue(PERM_INSTANCE);
 
         if (null == type || type.isEmpty()) {
             //authorization is turned off, since the permission is not defined
@@ -93,9 +89,8 @@ public class AuthorizationController {
         LoggingUtils.setTargetContext("Clamp", "authorize");
         LoggingUtils.setTimeContext(startTime, new Date());
         securityLogger.debug("checking if {} has permission: {}", principalName, perm);
-        try {
-            isUserPermitted(perm);
-        } catch (NotAuthorizedException nae) {
+
+        if (!isUserPermitted(perm)){
             String msg = principalName + " does not have permission: " + perm;
             LoggingUtils.setErrorContext("100", "Authorization Error");
             securityLogger.warn(msg);
@@ -103,45 +98,26 @@ public class AuthorizationController {
         }
     }
 
-    private boolean isUserPermitted(SecureServicePermission inPermission) {
-        boolean authorized = false;
+    public boolean isUserPermitted(SecureServicePermission inPermission) {
+
         String principalName = PrincipalUtils.getPrincipalName();
         // check if the user has the permission key or the permission key with a
         // combination of  all instance and/or all action.
-        if (hasRole(inPermission.getKey())) {
-            auditLogger.info("{} authorized because user has permission with * for instance: {}", 
-                  principalName, inPermission.getKey());
-            authorized = true;
+        if (hasRole(inPermission.getKey()) || hasRole(inPermission.getKeyAllInstance())) {
+            auditLogger.info("{} authorized because user has permission with * for instance: {}",
+                    principalName, inPermission.getKey());
+            return true;
             // the rest of these don't seem to be required - isUserInRole method
             // appears to take * as a wildcard
-        } else if (hasRole(inPermission.getKeyAllInstance())) {
-            auditLogger.info("{} authorized because user has permission with * for instance: {}", 
-                  principalName, inPermission.getKey());
-            authorized = true;
         } else if (hasRole(inPermission.getKeyAllInstanceAction())) {
-            auditLogger.info("{} authorized because user has permission with * for instance and * for action: {}", 
-                  principalName, inPermission.getKey());
-            authorized = true;
+            auditLogger.info("{} authorized because user has permission with * for instance and * for action: {}",
+                    principalName, inPermission.getKey());
+            return true;
         } else if (hasRole(inPermission.getKeyAllAction())) {
             auditLogger.info("{} authorized because user has permission with * for action: {}",
-                  principalName, inPermission.getKey());
-            authorized = true;
+                    principalName, inPermission.getKey());
+            return true;
         } else {
-            throw new NotAuthorizedException("");
-        }
-        return authorized;
-    }
-
-    /**
-     * Verify whether the user has the permission.
-     *
-     * @param inPermission
-     *        The permissions to verify
-     */
-    public boolean isUserPermittedNoException(SecureServicePermission inPermission) {
-        try {
-            return i<style>.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span><span class="ch">#!/bin/bash</span>
<span class="c1">#</span>
<span class="c1"># Copyright 2017 ZTE Corporation.</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="c1"># Place the scripts in run order:</span>
<span class="c1"># Start all process required for executing test case</span>

<span class="nb">source</span> <span class="si">${</span><span class="nv">SCRIPTS</span><span class="si">}</span>/common_functions.sh

<span class="c1">#start msb</span>
docker run -d -p <span class="m">8500</span>:8500  --name msb_consul consul:0.9.3
<span class="nv">MSB_CONSUL_IP</span><span class="o">=</span><span class="sb">`</span>get-instance-ip.sh msb_consul<span class="sb">`</span>
<span class="nb">echo</span> <span class="nv">MSB_CONSUL_IP</span><span class="o">=</span><span class="si">${</span><span class="nv">MSB_CONSUL_IP</span><span class="si">}</span>

docker run -d  -p <span class="m">10081</span>:10081  -e <span class="nv">CONSUL_IP</span><span class="o">=</span><span class="nv">$MSB_CONSUL_IP</span> --name msb_discovery nexus3.onap.org:10001/onap/msb/msb_discovery
<span class="nv">MSB_DISCOVERY_IP</span><span class="o">=</span><span class="sb">`</span>get-instance-ip.sh msb_discovery<span class="sb">`</span>
<span class="nb">echo</span> <span class="nv">DISCOVERY_IP</span><span class="o">=</span><span class="si">${</span><span class="nv">MSB_DISCOVERY_IP</span><span class="si">}</span>

docker run -d -p <span class="m">80</span>:80 -e <span class="nv">CONSUL_IP</span><span class="o">=</span><span class="nv">$MSB_CONSUL_IP</span> -e <span class="nv">SDCLIENT_IP</span><span class="o">=</span><span class="nv">$MSB_DISCOVERY_IP</span> --name msb_internal_apigateway nexus3.onap.org:10001/onap/msb/msb_apigateway
<span class="nv">MSB_IAG_IP</span><span class="o">=</span><span class="sb">`</span>get-instance-ip.sh msb_internal_apigateway<span class="sb">`</span>
<span class="nb">echo</span> <span class="nv">MSB_IAG_IP</span><span class="o">=</span><span class="si">${</span><span class="nv">MSB_IAG_IP</span><span class="si">}</span>

<span class="c1"># Wait for initialization(8500 Consul, 10081 Service Registration &amp; Discovery, 80 api gateway)</span>
<span class="k">for</span> i in <span class="o">{</span><span class="m">1</span>..10<span class="o">}</span><span class="p">;</span> <span class="k">do</span>
    curl -sS -m <span class="m">1</span> <span class="si">${</span><span class="nv">MSB_CONSUL_IP</span><span class="si">}</span>:8500 <span class="o">&amp;&amp;</span> curl -sS -m <span class="m">1</span> <span class="si">${</span><span class="nv">MSB_DISCOVERY_IP</span><span class="si">}</span>:10081 <span class="o">&amp;&amp;</span> curl -sS -m <span class="m">1</span> <span class="si">${</span><span class="nv">MSB_IAG_IP</span><span class="si">}</span>:80 <span class="o">&amp;&amp;</span> <span class="nb">break</span>
    <span class="nb">echo</span> sleep <span class="nv">$i</span>
    sleep <span class="nv">$i</span>
<span class="k">done</span>

<span class="c1"># wait for container initalization</span>
<span class="nb">echo</span> sleep <span class="m">30</span>
sleep <span class="m">30</span>

<span class="c1"># start vfc-vnflcm</span>
docker run -d --name vfc-vnflcm -v /var/lib/mysql -e <span class="nv">MSB_ADDR</span><span class="o">=</span><span class="si">${</span><span class="nv">MSB_IAG_IP</span><span class="si">}</span>:80 nexus3.onap.org:10001/onap/vfc/vnflcm
<span class="nv">VNFLCM_IP</span><span class="o">=</span><span class="sb">`</span>get-instance-ip.sh vfc-vnflcm<span class="sb">`</span>

<span class="c1"># Wait for initialization</span>
<span class="k">for</span> i in <span class="o">{</span><span class="m">1</span>..10<span class="o">}</span><span class="p">;</span> <span class="k">do</span>
    curl -sS -m <span class="m">1</span> <span class="si">${</span><span class="nv">VNFLCM_IP</span><span class="si">}</span>:8801 <span class="o">&amp;&amp;</span> <span class="nb">break</span>
    <span class="nb">echo</span> sleep <span class="nv">$i</span>
    sleep <span class="nv">$i</span>
<span class="k">done</span>

docker cp vfc-vnflcm:/service/vfc/gvnfm/vnflcm/lcm/lcm/pub/config/config.py ./
cat config.py

docker cp vfc-vnflcm:/service/vfc/gvnfm/vnflcm/lcm/logs/runtime_lcm.log ./
cat runtime_lcm.log

<span class="c1"># Pass any variables required by Robot test suites in ROBOT_VARIABLES</span>
<span class="nv">ROBOT_VARIABLES</span><span class="o">=</span><span class="s2">&quot;-v MSB_IAG_IP:</span><span class="si">${</span><span class="nv">MSB_IAG_IP</span><span class="si">}</span><span class="s2"> -v VNFLCM_IP:</span><span class="si">${</span><span class="nv">VNFLCM_IP</span><span class="si">}</span><span class="s2"> -v SCRIPTS:</span><span class="si">${</span><span class="nv">SCRIPTS</span><span class="si">}</span><span class="s2">&quot;</span>
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div id="lfcollabprojects-footer">
  <div class="gray-diagonal">
    <div class="footer-inner">
      <p>
        &copy; 2017 <a href="https://www.onap.org/">ONAP</a> a Linux Foundation
        Collaborative Project. All Rights Reserved.
      </p>
      <p>
        Linux Foundation is a registered trademark of The Linux Foundation.
        Linux is a registered
        <a
          href="http://www.linuxfoundation.org/programs/legal/trademark"
          title="Linux Mark Institute"
          >trademark</a
        >
        of Linus Torvalds.
      </p>
      <p>
        Please see our
        <a href="http://www.linuxfoundation.org/privacy">privacy policy</a> and
        <a href="http://www.linuxfoundation.org/terms">terms of use</a>
      </p>
    </div>
  </div>
</div>
</div> <!-- id=cgit -->
</body>
</html>
);
         model.setTypeId((String) out.get("v_service_type_id"));
         model.setDeploymentId((String) out.get("v_deployment_id"));
         model.setDeploymentStatusUrl((String) out.get("v_deployment_status_url"));
+
+        setEventProp(model.getEvent(), out);
+    }
+
+    /**
+     * Helper method to setup the event prop to the CldsEvent class
+     *
+     * @param event
+     *  the clds event
+     * @param prop
+     *  collection with the configuration
+     */
+    private void setEventProp(CldsEvent event, Map prop) {
+        event.setId((String) prop.get("v_event_id"));
+        event.setActionCd((String) prop.get("v_action_cd"));
+        event.setActionStateCd((String) prop.get("v_action_state_cd"));
+        event.setProcessInstanceId((String) prop.get("v_event_process_instance_id"));
+        event.setUserid((String) prop.get("v_event_user_id"));
     }
 
     /**
@@ -555,7 +574,7 @@ public class CldsDao {
             .addValue("v_tosca_model_yaml", cldsToscaModel.getToscaModelYaml())
             .addValue("v_tosca_model_json", cldsToscaModel.getToscaModelJson()).addValue("v_user_id", userId);
         Map<String, Object> out = logSqlExecution(procInsertNewToscaModelVersion, in);
-        cldsToscaModel.setRevisionId((String) (out.get("v_revision_id")));
+        cldsToscaModel.setRevisionId((String) out.get("v_revision_id"));
         return cldsToscaModel;
     }
 
@@ -593,7 +612,7 @@ public class CldsDao {
             .addValue("v_dictionary_name", cldsDictionary.getDictionaryName())
             .addValue("v_user_id", cldsDictionary.getCreatedBy());
         Map<String, Object> out = logSqlExecution(procInsertDictionary, in);
-        cldsDictionary.setDictionaryId((String) (out.get("v_dictionary_id")));
+        cldsDictionary.setDictionaryId((String) out.get("v_dictionary_id"));
     }
 
     /**
@@ -677,7 +696,7 @@ public class CldsDao {
             .addValue("v_dict_element_description", cldsDictionaryItem.getDictElementDesc())
             .addValue("v_dict_element_type", cldsDictionaryItem.getDictElementType()).addValue("v_user_id", userId);
         Map<String, Object> out = logSqlExecution(procInsertDictionaryElement, in);
-        cldsDictionaryItem.setDictElementId((String) (out.get("v_dict_element_id")));
+        cldsDictionaryItem.setDictElementId((String) out.get("v_dict_element_id"));
     }
 
     /**
diff --git a/src/main/java/org/onap/clamp/clds/service/CldsService.java b/src/main/java/org/onap/clamp/clds/service/CldsService.java
index b9cce087..71dd5c4f 100644
--- a/src/main/java/org/onap/clamp/clds/service/CldsService.java
+++ b/src/main/java/org/onap/clamp/clds/service/CldsService.java
@@ -32,18 +32,15 @@ import com.google.gson.JsonObject;
 import com.google.gson.reflect.TypeToken;
 import java.io.IOException;
 import java.lang.reflect.Type;
-import java.security.GeneralSecurityException;
 import java.util.Date;
 import java.util.List;
 import java.util.UUID;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.BadRequestException;
-import javax.ws.rs.NotAuthorizedException;
 import javax.xml.transform.TransformerException;
 
 import org.apache.camel.Produce;
-import org.apache.commons.codec.DecoderException;
 import org.json.simple.parser.ParseException;
 import org.onap.clamp.clds.camel.CamelProxy;
 import org.onap.clamp.clds.client.DcaeDispatcherServices;
diff --git a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java
index e9e589e0..1ece484b 100755
--- a/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java
+++ b/src/main/java/org/onap/clamp/clds/util/drawing/AwtUtils.java
@@ -5,6 +5,8 @@
  * Copyright (C) 2019 Nokia. All rights
  *                             reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * 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
@@ -39,6 +41,9 @@ public class AwtUtils {
     private static final String FONT_FACE = "SansSerif";
     private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f);
 
+    private AwtUtils() {
+    }
+
     static void rectWithText(Graphics2D g2d, String text, Point point, int width, int height) {
         Rectangle rect = new Rectangle(point.x, point.y, width, height);
         g2d.draw(rect);
-- 
cgit 

