Improve coverage flow/controller/node #3
[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 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
17
18 /**
19  * Helper class for RestServiceNode
20  */
21 class ResourceUriExtractor {
22
23   private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
24
25   String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
26     String resourceUri = ctx.getAttribute(INPUT_URL);
27
28     if (StringUtils.isBlank(resourceUri)) {
29       resourceUri = getAddress(ctx);
30       log.info("resourceUri= " + resourceUri);
31       resourceUri += getContext(ctx, prop);
32       log.info("resourceUri= " + resourceUri);
33       resourceUri += getSubContext(ctx, prop);
34     }
35     log.info("resourceUri= " + resourceUri);
36
37     return resourceUri;
38   }
39
40   private String getAddress(SvcLogicContext ctx) {
41     String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
42     String port = ctx.getAttribute(INPUT_PORT_NUMBER);
43     return HTTP + address + ":" + port;
44   }
45
46   private String getContext(SvcLogicContext ctx, Properties prop) throws Exception {
47     String context;
48     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
49       context = "/" + ctx.getAttribute(INPUT_CONTEXT);
50     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context") != null) {
51       context = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context");
52     } else {
53       throw new Exception("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
54     }
55     return context;
56   }
57
58   private String getSubContext(SvcLogicContext ctx, Properties prop) throws Exception {
59     String subContext;
60     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
61       subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
62     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
63       subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
64     } else {
65       throw new Exception("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
66     }
67     return subContext;
68   }
69
70 }