Merge "SO Monitoring UI"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / GenericVnfHealthCheck.java
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.MsoLogger;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class GenericVnfHealthCheck {
41
42         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenericVnfHealthCheck.class);
43         @Autowired
44         private ExceptionBuilder exceptionUtil;
45         @Autowired
46         private ExtractPojosForBB extractPojosForBB;
47         @Autowired
48         private CatalogDbClient catalogDbClient;
49         @Autowired
50         private ApplicationControllerAction appCClient;
51         
52         public void setParamsForGenericVnfHealthCheck(BuildingBlockExecution execution) {
53                 
54                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
55
56                 try {
57                         ControllerSelectionReference controllerSelectionReference;
58                         GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
59                         String vnfId = vnf.getVnfId();
60                         String vnfName = vnf.getVnfName();      
61                         String vnfType = vnf.getVnfType();
62                         String oamIpAddress = vnf.getIpv4OamAddress();
63                         String actionCategory = Action.HealthCheck.toString();
64                         controllerSelectionReference = catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(vnfType, actionCategory);
65                         String controllerName = controllerSelectionReference.getControllerName();
66                         
67                         execution.setVariable("vnfId", vnfId);
68                         execution.setVariable("vnfName", vnfName);
69                         execution.setVariable("oamIpAddress", oamIpAddress);
70                         execution.setVariable("vnfHostIpAddress", oamIpAddress);
71                         execution.setVariable("msoRequestId", gBBInput.getRequestContext().getMsoRequestId());
72                         execution.setVariable("action", actionCategory);
73                         execution.setVariable("controllerType", controllerName);
74                         
75                 } catch (Exception ex) {
76                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
77                 }
78         }
79         
80         public void callAppcClient(BuildingBlockExecution execution) {
81                 
82                 try {
83                         Action action = null;
84                         action = Action.valueOf(execution.getVariable("action"));
85                         String msoRequestId = execution.getVariable("msoRequestId");
86                         String vnfId = execution.getVariable("vnfId");
87                         Optional<String> payload = null;
88                         if(execution.getVariable("payload") != null){
89                                 String pay = execution.getVariable("payload");
90                                 payload =  Optional.of(pay);
91                         }
92                         String controllerType = execution.getVariable("controllerType");
93                         HashMap<String, String> payloadInfo = new HashMap<String, String>();
94                         payloadInfo.put("vnfName", execution.getVariable("vnfName"));
95                         payloadInfo.put("vfModuleId",execution.getVariable("vfModuleId"));
96                         payloadInfo.put("oamIpAddress",execution.getVariable("oamIpAddress"));
97                         payloadInfo.put("vnfHostIpAddress",execution.getVariable("vnfHostIpAddress"));
98                         //PayloadInfo contains extra information that adds on to payload before making request to appc
99                         appCClient.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
100                 
101                         } catch (Exception ex) {
102                                 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
103                         }
104         }
105 }