Refactor dblib
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / openecomp / sdnc / sli / SliPluginUtils / DME2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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
21 package org.openecomp.sdnc.sli.SliPluginUtils;
22
23 import java.util.Map;
24
25 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33 /**
34  * A SvcLogicJavaPlugin that generates DME2 proxy urls using parameters from context memory.
35  */
36 public class DME2 implements SvcLogicJavaPlugin {
37     String aafUserName;
38     String aafPassword;
39     String envContext;
40     String routeOffer;
41     String[] proxyUrls;
42     Integer index;
43     String commonServiceVersion;
44     String partner;
45
46     private static final Logger LOG = LoggerFactory.getLogger(DME2.class);
47
48     public void setPartner(String partner) {
49         if (partner != null && partner.length() > 0) {
50             this.partner = partner;
51         }
52     }
53
54     public DME2(String aafUserName, String aafPassword, String envContext, String routeOffer, String[] proxyUrls, String commonServiceVersion) {
55         this.aafUserName = aafUserName;
56         this.aafPassword = aafPassword;
57         this.envContext = envContext;
58         this.routeOffer = routeOffer;
59         this.proxyUrls = proxyUrls;
60         this.index = 0;
61         this.commonServiceVersion = commonServiceVersion;
62     }
63
64     // constructs a URL to contact the proxy which contacts a DME2 service
65     public String constructUrl(String service, String version, String subContext) {
66         StringBuilder sb = new StringBuilder();
67
68         // The hostname is assigned in a round robin fashion
69         sb.append(acquireHostName());
70         sb.append("/service=" + service);
71
72         //If the directedGraph passes an explicit version use that, if not use the commonServiceVersion found in the properties file
73         if (version == null) {
74             version = this.commonServiceVersion;
75         }
76         sb.append("/version=" + version);
77
78         sb.append("/envContext=" + this.envContext);
79         if (this.routeOffer != null && this.routeOffer.length() > 0) {
80             sb.append("/routeOffer=" + this.routeOffer);
81         }
82         if (subContext != null && subContext.length() > 0) {
83             sb.append("/subContext=" + subContext);
84         }
85         sb.append("?dme2.password=" + this.aafPassword);
86         sb.append("&dme2.username=" + this.aafUserName);
87         if (this.partner != null) {
88             sb.append("&dme2.partner=" + this.partner);
89         }
90         sb.append("&dme2.allowhttpcode=true");
91         return (sb.toString());
92     }
93
94     public synchronized String acquireHostName() {
95         String retVal = proxyUrls[index];
96         index++;
97         if (index == this.proxyUrls.length) {
98             index = 0;
99         }
100         return retVal;
101     }
102
103     // Node entry point
104     public void constructUrl(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
105         SliPluginUtils.checkParameters(parameters, new String[] { "service", "outputPath" }, LOG);
106         String completeProxyUrl = constructUrl(parameters.get("service"), parameters.get("version"), parameters.get("subContext"));
107         ctx.setAttribute(parameters.get("outputPath"), completeProxyUrl);
108     }
109
110 }