2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.forwarding.k8s;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.reflect.TypeToken;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.LinkedHashMap;
33 import java.util.List;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.NameValuePair;
39 import org.apache.http.client.ClientProtocolException;
40 import org.apache.http.client.entity.UrlEncodedFormEntity;
41 import org.apache.http.client.methods.CloseableHttpResponse;
42 import org.apache.http.client.methods.HttpGet;
43 import org.apache.http.client.methods.HttpPost;
44 import org.apache.http.entity.FileEntity;
45 import org.apache.http.entity.StringEntity;
46 import org.apache.http.impl.client.CloseableHttpClient;
47 import org.apache.http.impl.client.HttpClients;
48 import org.apache.http.message.BasicNameValuePair;
49 import org.apache.http.util.EntityUtils;
51 import org.onap.policy.common.logging.flexlogger.FlexLogger;
52 import org.onap.policy.common.logging.flexlogger.Logger;
53 import org.onap.policy.common.parameters.ParameterService;
54 import org.onap.policy.distribution.forwarding.ArtifactForwarder;
55 import org.onap.policy.distribution.model.CloudArtifact;
56 import org.onap.policy.distribution.model.PolicyInput;
57 import org.onap.policy.distribution.model.VfModuleModel;
58 import org.onap.sdc.api.notification.IArtifactInfo;
62 * Forwards policies to the XACML PDP.
64 public class K8sArtifactForwarder implements ArtifactForwarder {
66 private static final Logger LOGGER = FlexLogger.getLogger(K8sArtifactForwarder.class);
67 private static final String BASE_PATH = "http://localhost:9015/v1/rb/definition";
68 private static final String CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT = "CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT";
69 private static final String HELM_ARTIFACT = "HELM";
70 private Map<String, IArtifactInfo> artifactMap;
72 private K8sArtifactForwarderParameterGroup configurationParameters = null;
77 public void forward(PolicyInput policyInput) {
78 if (policyInput instanceof CloudArtifact) {
79 System.out.println("get a CloudArtifact !");
80 CloudArtifact cloudArtifact = (CloudArtifact) policyInput;
81 artifactMap = cloudArtifact.getArtifactTypeMap();
82 System.out.println("the artifactMap = " + artifactMap);
83 ArrayList<VfModuleModel> vfModuleModels = cloudArtifact.getVfModulePayload();
84 System.out.println("the size of vfModule = " + vfModuleModels.size());
86 for (VfModuleModel vfModule : vfModuleModels) {
87 forwardAndUpload(vfModule);
90 System.out.println("NOT a CloudArtifact type !");
95 private void forwardAndUpload(VfModuleModel vfModule) {
97 System.out.println("before create type !");
98 boolean definitionCreated = createDefinition(vfModule);
99 System.out.println(" after create type !");
100 if (definitionCreated) {
101 uploadArtifact(vfModule);
105 private boolean createDefinition(VfModuleModel vfModule) {
107 HttpPost httpPost = new HttpPost(BASE_PATH);
108 httpPost.addHeader("Accept", "application/json");
109 httpPost.addHeader("Content-Type", "application/json");
111 Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
114 Map<String, Object> map = new LinkedHashMap<String, Object>();
115 map.put("rb-name", vfModule.getVfModuleModelInvariantUUID());
116 map.put("rb-version", vfModule.getVfModuleModelUUID());
117 map.put("descritpion",vfModule.getVfModuleModelDescription());
118 Map<String, String> labelMap = new LinkedHashMap<String, String>();
119 labelMap.put("vnf_customization_uuid",vfModule.getVfModuleModelCustomizationUUID());
120 map.put("labels", labelMap);
121 String json = gson.toJson(map);
123 StringEntity entity = new StringEntity(json);
124 httpPost.setEntity(entity);
125 return invokeHttpPost("definition", httpPost);
126 } catch (Exception e) {
127 System.out.println("create definition error");
133 private boolean uploadArtifact(VfModuleModel vfModule) {
134 String url = BASE_PATH + "/" + vfModule.getVfModuleModelInvariantUUID() + "/"
135 + vfModule.getVfModuleModelUUID() + "/content";
136 HttpPost httpPost = new HttpPost(url);
137 httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
139 List<String> artifacts = vfModule.getArtifacts();
140 System.out.println("artifacts = " + artifacts);
142 String vfNamePrefix = vfModule.getVfModuleModelName().toLowerCase();
143 if ( vfNamePrefix.indexOf("..") > 0 ) {
144 vfNamePrefix = vfNamePrefix.substring(vfNamePrefix.indexOf("..") + 2);
145 if ( vfNamePrefix.indexOf("..") > 0 )
146 vfNamePrefix = vfNamePrefix.substring(0, vfNamePrefix.indexOf("..")) + "_";
152 IArtifactInfo cloudArtifact = null;
153 IArtifactInfo firstCloudArtifact = null;
154 int cloudArtifactCount = 0;
155 boolean found = false;
157 for (String artifact: artifacts) {
158 if (artifactMap.get(artifact) != null
159 && artifactMap.get(artifact).getArtifactType().equals(HELM_ARTIFACT)) {
160 firstCloudArtifact = artifactMap.get(artifact);
161 cloudArtifact = firstCloudArtifact;
162 cloudArtifactCount = 1;
168 if ( found == false )
169 for (String artifact: artifacts) {
170 if ( artifactMap.get(artifact) != null
171 && artifactMap.get(artifact).getArtifactType().equals(CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT)) {
172 if ( cloudArtifactCount == 0 )
173 firstCloudArtifact = artifactMap.get(artifact);
174 cloudArtifactCount++;
175 IArtifactInfo tmpArtifact = artifactMap.get(artifact);
176 if ( tmpArtifact.getArtifactName().toLowerCase().startsWith(vfNamePrefix) ) {
177 cloudArtifact = tmpArtifact;
184 if ( found == false ) {
185 if ( firstCloudArtifact == null ) {
186 System.out.println(" meets error , no CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT or HELM type found ");
189 if ( cloudArtifactCount == 1 || vfNamePrefix == "" ) {
190 cloudArtifact = firstCloudArtifact;
192 System.out.println(" meets error , CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT artifacts mismatch ");
198 String cloudArtifactPath = "/data/" + vfModule.getVfModuleModelCustomizationUUID()
199 + "/" + cloudArtifact.getArtifactName();
200 File file = new File(cloudArtifactPath);
201 FileEntity entity = new FileEntity(file);
202 httpPost.setEntity(entity);
204 return invokeHttpPost("uploading", httpPost);
209 public void configure(String parameterGroupName) {
210 configurationParameters = ParameterService.get(parameterGroupName);
213 protected static boolean invokeHttpPost(String action, HttpPost httpPost) {
214 System.out.println("httpPost URI: " + httpPost);
219 try ( CloseableHttpClient httpClient = HttpClients.createDefault() ) {
220 System.out.println("Execute Post Body: " + EntityUtils.toString(httpPost.getEntity()));
221 CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost);
222 System.out.println("result2");
223 String result = EntityUtils.toString(closeableHttpResponse.getEntity());
224 System.out.println("result = {}" + result);
225 System.out.println("status = {}" + closeableHttpResponse.getStatusLine().getStatusCode());
226 int status = closeableHttpResponse.getStatusLine().getStatusCode();
227 // [200, 300] means pass
228 if ( (status >=200) && (status <= 300) ) {
231 System.out.println("exception: ret= " + status);
234 closeableHttpResponse.close();
236 } catch (IOException exp) {
237 errorMsg = action + " :invokeHttpPost failed with: " + exp.getMessage();
238 System.out.println("exception: POST FAILED : {}" + errorMsg);
242 System.out.println("httpPost end!");