Improve coverage in flow/controller/node
[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
27   static String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
28     String resourceUri = ctx.getAttribute(INPUT_URL);
29
30     if (StringUtils.isBlank(resourceUri)) {
31       resourceUri = getAddress(ctx);
32       log.info("resourceUri= " + resourceUri);
33       resourceUri += getContext(ctx, prop);
34       log.info("resourceUri= " + resourceUri);
35       resourceUri += getSubContext(ctx, prop);
36     }
37     log.info("resourceUri= " + resourceUri);
38
39     return resourceUri;
40   }
41
42   private static String getAddress(SvcLogicContext ctx) {
43     String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
44     String port = ctx.getAttribute(INPUT_PORT_NUMBER);
45     return HTTP + address + ":" + port;
46   }
47
48   private static String getContext(SvcLogicContext ctx, Properties prop) {
49     String context;
50     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
51       context = "/" + ctx.getAttribute(INPUT_CONTEXT);
52     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context") != null) {
53       context = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context");
54     } else {
55       throw new IllegalArgumentException("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
56     }
57     return context;
58   }
59
60   private static String getSubContext(SvcLogicContext ctx, Properties prop) {
61     String subContext;
62     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
63       subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
64     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
65       subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
66     } else {
67       throw new IllegalArgumentException("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
68     }
69     return subContext;
70   }
71
72 }