Add junit coverage to RequestInfoBuilder class
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / ResourceUriExtractor.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved. 
6  * ================================================================================
7  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.appc.flow.controller.node;
23
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.HTTP;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_CONTEXT;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PROTOCOL;
32 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PORT;
33 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_CONTEXT_URL;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import java.util.Properties;
37 import org.apache.commons.lang.StringUtils;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39
40 /**
41  * Helper class for RestServiceNode
42  */
43 class ResourceUriExtractor {
44
45     private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
46
47     String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
48         String resourceUri = ctx.getAttribute(INPUT_URL);
49
50         if (StringUtils.isBlank(resourceUri)) {
51             resourceUri = getAddress(ctx, prop);
52             log.info("resourceUri= " + resourceUri);
53             resourceUri += getContext(ctx, prop);
54             log.info("resourceUri= " + resourceUri);
55             resourceUri += getSubContext(ctx, prop);
56         }
57         log.info("resourceUri= " + resourceUri);
58
59         return resourceUri;
60     }
61
62     private String getAddress(SvcLogicContext ctx, Properties prop) {
63         String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
64         String portPath = ctx.getAttribute(VNF_TYPE) + "." + (REST_PROTOCOL) + "."
65                 + ctx.getAttribute(INPUT_REQUEST_ACTION) + "." + (REST_PORT);
66         String port = prop.getProperty(portPath);
67         return HTTP + address + ":" + port;
68     }
69
70     private String getContext(SvcLogicContext ctx, Properties prop) throws Exception {
71         String context;
72         String urlPath = ctx.getAttribute(VNF_TYPE) + "." + REST_PROTOCOL + "." + ctx.getAttribute(INPUT_REQUEST_ACTION)
73                 + "." + REST_CONTEXT_URL;
74         if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
75             context = "/" + ctx.getAttribute(INPUT_CONTEXT);
76         } else if (prop.getProperty(urlPath) != null) {
77             context = "/" + prop.getProperty(urlPath);
78         } else {
79             throw new Exception("Could not find the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
80         }
81         return context;
82     }
83
84     private String getSubContext(SvcLogicContext ctx, Properties prop) throws Exception {
85         String subContext;
86         if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
87             subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
88         } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
89             subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
90         } else {
91             throw new Exception(
92                     "Could not find the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
93         }
94         return subContext;
95     }
96
97 }