26b7addc4b4d4979f6aaf884f19a805599c3289e
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright 2022 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.usecaseui.intentanalysis.adapters.policy.impl;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.charset.StandardCharsets;
22 import okhttp3.MediaType;
23 import okhttp3.RequestBody;
24 import okhttp3.ResponseBody;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.ibatis.io.Resources;
27 import org.onap.usecaseui.intentanalysis.adapters.policy.PolicyService;
28 import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall;
29 import org.onap.usecaseui.intentanalysis.util.RestfulServices;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
33 import retrofit2.Response;
34
35 @Service
36 public class PolicyServiceImpl implements PolicyService {
37
38     private static final Logger logger = LoggerFactory.getLogger(PolicyServiceImpl.class);
39
40     private PolicyAPICall policyAPICall;
41
42     public PolicyServiceImpl() {
43         this.policyAPICall = RestfulServices.create(PolicyAPICall.class);
44     }
45
46     @Override
47     public boolean createAndDeployModifyCLLPolicy() {
48         try {
49             //Create policy
50             File policyFile = Resources.getResourceAsFile("intentPolicy/modifycll.json");
51             String policyBody = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8);
52             logger.info(String.format("Create policy, request body: %s", policyBody));
53             RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
54             Response<ResponseBody> policyResponse = policyAPICall.createPolicy(ModifyCLLPolicyConstants.policyType,
55                 ModifyCLLPolicyConstants.policyTypeVersion, policyReq).execute();
56             logger.info(
57                 String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
58             if (!policyResponse.isSuccessful()) {
59                 logger.error("Create modify cll policy failed.");
60                 return false;
61             }
62
63             //Deploy policy
64             File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_modifycll.json");
65             String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8);
66             logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
67             RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
68                 deployPolicyBody.toString());
69             Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
70             logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
71                 deployPolicyResponse.body()));
72             if (!deployPolicyResponse.isSuccessful()) {
73                 logger.error("Deploy modify cll policy failed.");
74                 return false;
75             }
76
77         } catch (IOException e) {
78             logger.error("Exception in create and deploy modify cll policy.", e);
79             return false;
80         }
81         return true;
82     }
83
84     @Override
85     public boolean undeployAndRemoveModifyCLLPolicy() {
86         return undeployAndRemovePolicyIfExist(ModifyCLLPolicyConstants.policyType,
87             ModifyCLLPolicyConstants.policyTypeVersion, ModifyCLLPolicyConstants.policyName,
88             ModifyCLLPolicyConstants.policyVersion);
89     }
90
91     @Override
92     public boolean updateIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
93         //the policy engine does not support update now. so we need to remove and recreate the policy now.
94         logger.info(String.format(
95             "Start to update the intent configuration policy, cllId: %s, originalBW: %s, closedLooopStatus:%b", cllId,
96             originalBW, closedLoopStatus));
97         //remove the configuration policy first
98         boolean res = undeployAndRemovePolicyIfExist(IntentConfigPolicyConstants.policyType,
99             IntentConfigPolicyConstants.policyTypeVersion, IntentConfigPolicyConstants.policyName,
100             IntentConfigPolicyConstants.policyVersion);
101         if (!res) {
102             logger.warn("Undeploy and remove the intent configuration policy failed.");
103         }
104         res = createAndDeployIntentConfigPolicy(cllId, originalBW, closedLoopStatus);
105         if (!res) {
106             logger.error("Create and deploy the intent configuration policy failed.");
107         }
108         logger.info(String.format("update intent configuration finished, result: %b", res));
109         return res;
110     }
111
112     /**
113      * Create and deploy the configuration policy
114      *
115      * @param cllId
116      * @param originalBW
117      * @param closedLoopStatus
118      * @return
119      */
120     public boolean createAndDeployIntentConfigPolicy(String cllId, String originalBW, boolean closedLoopStatus) {
121         try {
122             //Create policy type
123             File policyTypeFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy_type.json");
124             String policyTypeBody = FileUtils.readFileToString(policyTypeFile, StandardCharsets.UTF_8);
125             logger.info(String.format("Create policy type, request body: %s", policyTypeBody));
126             RequestBody policyTypeReq = RequestBody.create(MediaType.parse("application/json"),
127                 policyTypeBody.toString());
128             Response<ResponseBody> response = policyAPICall.createPolicyType(policyTypeReq).execute();
129             logger.info(
130                 String.format("Create policy type result, code: %d body: %s", response.code(), response.body()));
131             if (!response.isSuccessful()) {
132                 logger.error("Create intent configuration policy type failed.");
133                 return false;
134             }
135             //Create policy
136             File policyFile = Resources.getResourceAsFile("intentPolicy/intent_configs_policy.json");
137             String policyBodyTemplate = FileUtils.readFileToString(policyFile, StandardCharsets.UTF_8);
138             String policyBody = policyBodyTemplate.replace("${CLL_ID}", cllId)
139                 .replace("${CLOSED_LOOP_STATUS}", String.valueOf(closedLoopStatus))
140                 .replace("${ORIGINAL_BW}", originalBW);
141             logger.info(String.format("Create policy, request body: %s", policyBody));
142             RequestBody policyReq = RequestBody.create(MediaType.parse("application/json"), policyBody.toString());
143             Response<ResponseBody> policyResponse = policyAPICall.createPolicy(IntentConfigPolicyConstants.policyType,
144                 IntentConfigPolicyConstants.policyTypeVersion, policyReq).execute();
145             logger.info(
146                 String.format("Create policy result, code: %d body: %s", policyResponse.code(), policyResponse.body()));
147             if (!policyResponse.isSuccessful()) {
148                 logger.error("Create intent configuration policy failed.");
149                 return false;
150             }
151
152             //Deploy policy
153             File deployPolicyFile = Resources.getResourceAsFile("intentPolicy/deploy_intent_configs.json");
154             String deployPolicyBody = FileUtils.readFileToString(deployPolicyFile, StandardCharsets.UTF_8);
155             logger.info(String.format("Deploy policy, request body: %s", deployPolicyBody));
156             RequestBody deployPolicyReq = RequestBody.create(MediaType.parse("application/json"),
157                 deployPolicyBody.toString());
158             Response<ResponseBody> deployPolicyResponse = policyAPICall.deployPolicy(deployPolicyReq).execute();
159             logger.info(String.format("Deploy policy result, code: %d body: %s", deployPolicyResponse.code(),
160                 deployPolicyResponse.body()));
161             if (!deployPolicyResponse.isSuccessful()) {
162                 logger.error("Deploy intent configuration policy failed.");
163                 return false;
164             }
165
166         } catch (IOException e) {
167             logger.error("Exception in create and deploy intent config policy.", e);
168             return false;
169         }
170         return true;
171     }
172
173     /**
174      * undeploy and remove the configuration policy
175      *
176      * @return
177      */
178     private boolean undeployAndRemovePolicyIfExist(String policyType, String policyTypeVersion, String policyName,
179         String policyVersion) {
180         try {
181             //check if the policy exists
182             Response<ResponseBody> response = policyAPICall.getPolicy(policyType, policyTypeVersion, policyName,
183                 policyVersion).execute();
184             logger.info(String.format("The policy query result, code: %d body: %s", response.code(), response.body()));
185             // remove the policy if exists.
186             if (response.isSuccessful()) {
187                 logger.info("The policy exists, start to undeploy.");
188                 Response<ResponseBody> undeployResponse = policyAPICall.undeployPolicy(policyName).execute();
189                 logger.info(String.format("Undeploy policy result. code: %d body: %s", undeployResponse.code(),
190                     undeployResponse.body()));
191                 logger.info("Start to remove the policy.");
192                 Response<ResponseBody> removeResponse = policyAPICall.removePolicy(policyName, policyVersion).execute();
193                 logger.info(String.format("Remove policy result. code: %d body: %s", removeResponse.code(),
194                     removeResponse.body()));
195                 return true;
196             }
197             return true;
198
199         } catch (IOException e) {
200             logger.error("Exception in undeploy and remove policy", e);
201             return false;
202         }
203
204     }
205 }