add subplug for k8s to invoke Artifact forward support
[multicloud/framework.git] / artifactbroker / plugins / forwarding-plugins / src / main / java / org / onap / policy / distribution / forwarding / xacml / pdp / XacmlPdpArtifactForwarder.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.forwarding.xacml.pdp;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.reflect.TypeToken;
26
27 import java.io.File;
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;
34 import java.util.Map;
35
36
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.impl.client.CloseableHttpClient;
46 import org.apache.http.impl.client.HttpClients;
47 import org.apache.http.message.BasicNameValuePair;
48 import org.apache.http.entity.StringEntity;
49 import org.apache.http.util.EntityUtils;
50
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.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter;
56 import org.onap.policy.distribution.model.PolicyInput;
57 import org.onap.policy.distribution.model.CloudArtifact;
58 import org.onap.policy.distribution.model.VfModuleModel;
59 import org.onap.sdc.api.notification.IArtifactInfo;
60 import org.springframework.http.HttpStatus;
61
62
63 /**
64  * Forwards policies to the XACML PDP.
65  */
66 public class XacmlPdpArtifactForwarder implements ArtifactForwarder {
67
68     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpArtifactForwarder.class);
69     private static final String BASE_PATH = "http://localhost:8081/v1/rb/definition";
70     private static final String CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT = "CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT";
71     private Map<String, IArtifactInfo> artifactMap;
72
73     private XacmlPdpArtifactForwarderParameterGroup configurationParameters = null;
74     
75
76
77     @Override
78     public void forward(PolicyInput  policyInput) {
79         if (policyInput instanceof CloudArtifact) {
80             System.out.println("get a CloudArtifact !");  
81             CloudArtifact cloudArtifact = (CloudArtifact) policyInput;
82             artifactMap = cloudArtifact.getArtifactTypeMap();
83             System.out.println("the artifactMap = " + artifactMap);  
84             ArrayList<VfModuleModel> vfModuleModels = cloudArtifact.getVfModulePayload();
85             System.out.println("the size of vfModule = " + vfModuleModels.size());  
86                    
87             for (VfModuleModel vfModule : vfModuleModels) {
88                 forwardAndUpload(vfModule);
89             }
90         } else {
91             System.out.println("NOT a CloudArtifact type !");
92             return;
93         }
94     }
95
96     private void forwardAndUpload(VfModuleModel vfModule) {
97         
98         System.out.println("before create type !");
99         boolean definitionCreated = createDefinition(vfModule);
100         System.out.println(" after create type !");
101         if (definitionCreated) {
102             uploadArtifact(vfModule);
103         }
104     }
105
106     private boolean createDefinition(VfModuleModel vfModule) {
107         try {
108             HttpPost httpPost = new HttpPost(BASE_PATH);
109             httpPost.addHeader("Accept", "application/json");
110             httpPost.addHeader("Content-Type", "application/json");
111
112             Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
113                 .create();
114
115             Map<String, Object> map = new LinkedHashMap<String, Object>();
116             map.put("rb-name", vfModule.getVfModuleModelName());
117             map.put("rb-version", vfModule.getVfModuleModelVersion());
118             map.put("descritpion",vfModule.getVfModuleModelDescription());
119             Map<String, String> labelMap = new LinkedHashMap<String, String>();
120             labelMap.put("vnf_customization_uuid",vfModule.getVfModuleModelCustomizationUUID());
121             map.put("labels", labelMap);
122             String json = gson.toJson(map);
123
124             StringEntity entity = new StringEntity(json);
125             httpPost.setEntity(entity);
126             return invokeHttpPost("definition", httpPost);
127         } catch (Exception e) {
128             System.out.println("create definition error");
129             return false;
130         }
131
132     }
133
134     private boolean uploadArtifact(VfModuleModel vfModule) {
135         String url = BASE_PATH + "/" + vfModule.getVfModuleModelName() + "/" 
136             + vfModule.getVfModuleModelVersion() + "/content"; 
137         HttpPost httpPost = new HttpPost(url);
138         httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
139
140         List<String> artifacts = vfModule.getArtifacts();
141         System.out.println("artifacts = " + artifacts);
142
143         String cloudUuid = null;
144         IArtifactInfo cloudArtifact = null;
145         boolean found = false;
146
147         for (String artifact: artifacts) {
148             if ( artifactMap.get(artifact) != null && 
149                 artifactMap.get(artifact).getArtifactType().equals("CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT")) {
150                 cloudArtifact = artifactMap.get(artifact);
151                 cloudUuid = cloudArtifact.getArtifactUUID();
152                 found = true;
153                 break;
154             }
155         }
156         
157         if ( found == false ) {
158             System.out.println(" meets error , no CLOUD_TECHNOLOGY_SPECIFIC_ARTIFACT type found ");
159             return false;
160         }
161         String cloudArtifactPath = "/data/" + vfModule.getVfModuleModelCustomizationUUID() 
162             + "/" + cloudArtifact.getArtifactName();
163         File file = new File(cloudArtifactPath);
164         FileEntity entity = new FileEntity(file);
165         httpPost.setEntity(entity);
166         
167         return invokeHttpPost("uploading", httpPost);
168     }
169
170
171     @Override
172     public void configure(String parameterGroupName) {
173         configurationParameters = ParameterService.get(parameterGroupName);
174     }
175
176     protected static boolean invokeHttpPost(String action, HttpPost httpPost)  {
177         System.out.println("httpPost begin!");
178         boolean ret = false;
179
180         String errorMsg;
181         label1: {
182             try ( CloseableHttpClient httpClient = HttpClients.createDefault() ) {
183                 System.out.println("result1") ;
184                 CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost);
185                 System.out.println("result2") ;
186                 String result = EntityUtils.toString(closeableHttpResponse.getEntity());
187                 System.out.println("result = {}" + result);
188                 System.out.println("status  = {}"+ closeableHttpResponse.getStatusLine().getStatusCode());
189                 if ( closeableHttpResponse.getStatusLine().getStatusCode() != 200 ) {
190                     System.out.println("exception: ret= " + closeableHttpResponse.getStatusLine().getStatusCode());
191                 } else {
192                     ret = true;
193                 }
194
195                 closeableHttpResponse.close();
196                 break label1;
197             } catch (IOException var) {
198                 errorMsg = action + ":httpPostWithJSON connect faild";
199                 System.out.println("exception: POST_CONNECT_FAILD : {}" + errorMsg);
200             }
201         }
202
203         System.out.println("httpPost end!");
204         return ret;
205     }
206
207 }