Improve coverage flow/controller/node #2
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / ResourceUriExtractor.java
1 package org.onap.appc.flow.controller.node;
2
3 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.HTTP;
4 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_CONTEXT;
5 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
6 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PORT_NUMBER;
7 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
8 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
9 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
10
11 import com.att.eelf.configuration.EELFLogger;
12 import com.att.eelf.configuration.EELFManager;
13 import java.util.Properties;
14 import org.apache.commons.lang.StringUtils;
15 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
16
17 /**
18  * Helper class for RestServiceNode
19  */
20 class ResourceUriExtractor {
21
22   private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
23
24   private ResourceUriExtractor() {}
25
26   static String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
27     String resourceUri = ctx.getAttribute(INPUT_URL);
28
29     if (StringUtils.isBlank(resourceUri)) {
30       resourceUri = getAddress(ctx);
31       log.info("resourceUri= " + resourceUri);
32       resourceUri += getContext(ctx, prop);
33       log.info("resourceUri= " + resourceUri);
34       resourceUri += getSubContext(ctx, prop);
35     }
36     log.info("resourceUri= " + resourceUri);
37
38     return resourceUri;
39   }
40
41   private static String getAddress(SvcLogicContext ctx) {
42     String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
43     String port = ctx.getAttribute(INPUT_PORT_NUMBER);
44     return HTTP + address + ":" + port;
45   }
46
47   private static String getContext(SvcLogicContext ctx, Properties prop) {
48     String context;
49     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
50       context = "/" + ctx.getAttribute(INPUT_CONTEXT);
51     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context") != null) {
52       context = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context");
53     } else {
54       throw new IllegalArgumentException("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
55     }
56     return context;
57   }
58
59   private static String getSubContext(SvcLogicContext ctx, Properties prop) {
60     String subContext;
61     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
62       subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
63     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
64       subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
65     } else {
66       throw new IllegalArgumentException("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
67     }
68     return subContext;
69   }
70
71 }