2dae820e959ca561d98bb9ecf2b899f6db9a50f0
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import java.util.HashMap;
23 import java.util.Optional;
24
25 import org.onap.appc.client.lcm.model.Action;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
28 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
29 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
30 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
31 import org.onap.so.client.appc.ApplicationControllerAction;
32 import org.onap.so.client.exception.ExceptionBuilder;
33 import org.onap.so.db.catalog.client.CatalogDbClient;
34 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
35 import org.onap.so.logger.MessageEnum;
36 import org.onap.so.logger.MsoLogger;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class GenericVnfHealthCheck {
42
43         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenericVnfHealthCheck.class);
44         @Autowired
45         private ExceptionBuilder exceptionUtil;
46         @Autowired
47         private ExtractPojosForBB extractPojosForBB;
48         @Autowired
49         private CatalogDbClient catalogDbClient;
50         @Autowired
51         private ApplicationControllerAction appCClient;
52         
53         public void setParamsForGenericVnfHealthCheck(BuildingBlockExecution execution) {
54                 
55                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
56
57                 try {
58                         ControllerSelectionReference controllerSelectionReference;
59                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
60                         String vnfId = vnf.getVnfId();
61                         String vnfName = vnf.getVnfName();      
62                         String vnfType = vnf.getVnfType();
63                         String oamIpAddress = vnf.getIpv4OamAddress();
64                         String actionCategory = Action.HealthCheck.toString();
65                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
66                         String controllerName = controllerSelectionReference.getControllerName();
67                         
68                         execution.setVariable("vnfId", vnfId);
69                         execution.setVariable("vnfName", vnfName);
70                         execution.setVariable("oamIpAddress", oamIpAddress);
71                         execution.setVariable("vnfHostIpAddress", oamIpAddress);
72                         execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
73                         execution.setVariable("action", actionCategory);
74                         execution.setVariable("controllerType", controllerName);
75                         
76                 } catch (Exception ex) {
77                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
78                 }
79         }
80         
81         public void callAppcClient(BuildingBlockExecution execution) {
82                 msoLogger.trace("Start runAppcCommand ");
83                 String appcCode = "1002";
84                 String appcMessage = "";
85                 try {
86                         Action action = null;
87                         action = Action.valueOf(execution.getVariable("action"));
88                         String msoRequestId = execution.getVariable("msoRequestId");
89                         String vnfId = execution.getVariable("vnfId");
90                         Optional<String> payload = null;
91                         if(execution.getVariable("payload") != null){
92                                 String pay = execution.getVariable("payload");
93                                 payload =  Optional.of(pay);
94                         }
95                         String controllerType = execution.getVariable("controllerType");
96                         HashMap<String, String> payloadInfo = new HashMap<String, String>();
97                         payloadInfo.put("vnfName", execution.getVariable("vnfName"));
98                         payloadInfo.put("vfModuleId",execution.getVariable("vfModuleId"));
99                         payloadInfo.put("oamIpAddress",execution.getVariable("oamIpAddress"));
100                         payloadInfo.put("vnfHostIpAddress",execution.getVariable("vnfHostIpAddress"));
101                         
102                         msoLogger.debug("Running APP-C action: " + action.toString());
103                         msoLogger.debug("VNFID: " + vnfId);     
104                         //PayloadInfo contains extra information that adds on to payload before making request to appc
105                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
106                         appcCode = appCClient.getErrorCode();
107                         appcMessage = appCClient.getErrorMessage();
108                 
109                 } catch (Exception e) {
110                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "Caught exception in runAppcCommand in GenericVnfHealthCheck", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "APPC Error", e);
111                         appcMessage = e.getMessage();
112                 }
113                 msoLogger.error("Error Message: " + appcMessage);
114                 msoLogger.error("ERROR CODE: " + appcCode);
115                 msoLogger.trace("End of runAppCommand ");
116                 if (appcCode != null && !appcCode.equals("0")) {
117                         exceptionUtil.buildAndThrowWorkflowException(execution, Integer.parseInt(appcCode), appcMessage);
118                 }
119         }
120 }