Changes needed for MultiStep Actions
[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_URL;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_PORT;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.REST_CONTEXT_URL;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import org.apache.commons.lang.StringUtils;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35
36 /**
37  * Helper class for RestServiceNode
38  */
39 class ResourceUriExtractor {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
42
43     String extractResourceUri(SvcLogicContext ctx) throws Exception {
44         String resourceUri = ctx.getAttribute(INPUT_URL);
45
46         if (StringUtils.isBlank(resourceUri)) {
47             resourceUri = getAddress(ctx);
48             log.info("resourceUri= " + resourceUri);
49             resourceUri += getContext(ctx);
50             log.info("resourceUri= " + resourceUri);
51
52         }
53         log.info("resourceUri= " + resourceUri);
54
55         return resourceUri;
56     }
57
58     private String getAddress(SvcLogicContext ctx) {
59         String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
60         String port = ctx.getAttribute(REST_PORT);
61         return HTTP + address + ":" + port;
62     }
63
64     private String getContext(SvcLogicContext ctx) throws Exception {
65         String context;
66         if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
67             context = "/" + ctx.getAttribute(INPUT_CONTEXT);
68         } else if (StringUtils.isNotBlank(ctx.getAttribute(REST_CONTEXT_URL))) {
69             context = "/" + ctx.getAttribute(REST_CONTEXT_URL);
70         } else {
71             throw new Exception("Could not find the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
72         }
73         return context;
74     }
75
76 }