39612716e5d7fb357f0a6aa3b4868a057fd3d01b
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / main / java / org / openecomp / sdnc / sli / SliPluginUtils / DME2.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.SliPluginUtils;
23
24 import java.util.Map;
25
26 import org.openecomp.sdnc.sli.SvcLogicContext;
27 import org.openecomp.sdnc.sli.SvcLogicException;
28 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34 /**
35  * A SvcLogicJavaPlugin that generates DME2 proxy urls using parameters from context memory.
36  */
37 public class DME2 implements SvcLogicJavaPlugin {
38     String aafUserName;
39     String aafPassword;
40     String envContext;
41     String routeOffer;
42     String[] proxyUrls;
43     Integer index;
44     String commonServiceVersion;
45     String partner;
46
47     private static final Logger LOG = LoggerFactory.getLogger(DME2.class);
48
49     public void setPartner(String partner) {
50         if (partner != null && partner.length() > 0) {
51             this.partner = partner;
52         }
53     }
54
55     public DME2(String aafUserName, String aafPassword, String envContext, String routeOffer, String[] proxyUrls, String commonServiceVersion) {
56         this.aafUserName = aafUserName;
57         this.aafPassword = aafPassword;
58         this.envContext = envContext;
59         this.routeOffer = routeOffer;
60         this.proxyUrls = proxyUrls;
61         this.index = 0;
62         this.commonServiceVersion = commonServiceVersion;
63     }
64
65     // constructs a URL to contact the proxy which contacts a DME2 service
66     public String constructUrl(String service, String version, String subContext) {
67         StringBuilder sb = new StringBuilder();
68
69         // The hostname is assigned in a round robin fashion
70         sb.append(acquireHostName());
71         sb.append("/service=" + service);
72
73         //If the directedGraph passes an explicit version use that, if not use the commonServiceVersion found in the properties file
74         if (version == null) {
75             version = this.commonServiceVersion;
76         }
77         sb.append("/version=" + version);
78
79         sb.append("/envContext=" + this.envContext);
80         if (this.routeOffer != null && this.routeOffer.length() > 0) {
81             sb.append("/routeOffer=" + this.routeOffer);
82         }
83         if (subContext != null && subContext.length() > 0) {
84             sb.append("/subContext=" + subContext);
85         }
86         sb.append("?dme2.password=" + this.aafPassword);
87         sb.append("&dme2.username=" + this.aafUserName);
88         if (this.partner != null) {
89             sb.append("&dme2.partner=" + this.partner);
90         }
91         sb.append("&dme2.allowhttpcode=true");
92         return (sb.toString());
93     }
94
95     public synchronized String acquireHostName() {
96         String retVal = proxyUrls[index];
97         index++;
98         if (index == this.proxyUrls.length) {
99             index = 0;
100         }
101         return retVal;
102     }
103
104     // Node entry point
105     public void constructUrl(Map<String, String> parameters, SvcLogicContext ctx) throws SvcLogicException {
106         SliPluginUtils.checkParameters(parameters, new String[] { "service", "outputPath" }, LOG);
107         String completeProxyUrl = constructUrl(parameters.get("service"), parameters.get("version"), parameters.get("subContext"));
108         ctx.setAttribute(parameters.get("outputPath"), completeProxyUrl);
109     }
110
111 }