Split sequence generation to classess
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.flow.controller.node;
21
22 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.HTTP;
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_CONTEXT;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_HOST_IP_ADDRESS;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PORT_NUMBER;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_SUB_CONTEXT;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_URL;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import java.util.Properties;
33 import org.apache.commons.lang.StringUtils;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36
37 /**
38  * Helper class for RestServiceNode
39  */
40 class ResourceUriExtractor {
41
42   private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
43
44   String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
45     String resourceUri = ctx.getAttribute(INPUT_URL);
46
47     if (StringUtils.isBlank(resourceUri)) {
48       resourceUri = getAddress(ctx);
49       log.info("resourceUri= " + resourceUri);
50       resourceUri += getContext(ctx, prop);
51       log.info("resourceUri= " + resourceUri);
52       resourceUri += getSubContext(ctx, prop);
53     }
54     log.info("resourceUri= " + resourceUri);
55
56     return resourceUri;
57   }
58
59   private String getAddress(SvcLogicContext ctx) {
60     String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
61     String port = ctx.getAttribute(INPUT_PORT_NUMBER);
62     return HTTP + address + ":" + port;
63   }
64
65   private String getContext(SvcLogicContext ctx, Properties prop) throws Exception {
66     String context;
67     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
68       context = "/" + ctx.getAttribute(INPUT_CONTEXT);
69     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context") != null) {
70       context = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context");
71     } else {
72       throw new Exception("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
73     }
74     return context;
75   }
76
77   private String getSubContext(SvcLogicContext ctx, Properties prop) throws Exception {
78     String subContext;
79     if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
80       subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
81     } else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
82       subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
83     } else {
84       throw new Exception("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
85     }
86     return subContext;
87   }
88
89 }