8276c690c31054a422942800fd5687bbd1379f49
[appc.git] / appc-provider / appc-provider-bundle / src / main / java / org / onap / appc / provider / lcm / mock / query / MockQueryHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.provider.lcm.mock.query;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.VmState;
29 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.VmStatus;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.query.output.QueryResults;
31 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.query.output.QueryResultsBuilder;
32 import org.onap.appc.executor.objects.LCMCommandStatus;
33 import org.onap.appc.provider.lcm.mock.AbstractMockHelper;
34 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
35 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
36
37 import java.io.File;
38 import java.io.FileInputStream;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 /**
47  * This class is here because LCM query backend is not implemented.
48  * Hence this class is here to mock the handling response of LCM query REST API.
49  *
50  * When backend is implemented, this file should be removed.
51  */
52 public class MockQueryHelper extends AbstractMockHelper {
53     private final String MOCK_QUERY_FILENAME = "/tmp/lcm/query";
54
55     /** VF_STATE value are listed at https://wiki.openstack.org/wiki/VMState#vm_state */
56     private final Map<VmState, List<String>> VF_STATE_MAP = new HashMap<VmState, List<String>>() {
57         {
58             put(VmState.Inactive, Arrays.asList(
59                 "INITIALIZED",
60                 "PAUSED",
61                 "SUSPENDED",
62                 "STOPPED",
63                 "SOFT_DELETED",
64                 "HARD_DELETED",
65                 "ERROR"));
66             put(VmState.Active, Arrays.asList(
67                 "ACTIVE",
68                 "RESCUED",
69                 "RESIZED"));
70             put(VmState.Standby, Arrays.asList(
71                 "STANDBY"
72             ));
73         }
74     };
75
76     private final EELFLogger logger = EELFManager.getInstance().getLogger(MockQueryHelper.class);
77
78     /**
79      * Process service request through reading the mockFile. File should contain:
80      *   - VNF_IDS: the list of VNF IDs, separated by comma
81      *   - VMS_<a VNF id>: the list of VMs of the VNF ID, separated by comma
82      *   - STATE_<a VM id>: the state of the VM
83      *   - STATUS_<a VM id>: the status of the VMl
84      * @param input of RequestHandleInput which contains the VNF ID
85      * @return RequestHandlerOutput
86      */
87     public RequestHandlerOutput query(RequestHandlerInput input) {
88         File file = new File(MOCK_QUERY_FILENAME);
89         if (!file.exists()) {
90             // when mock file does not exist, return generic service not supported
91             status = buildStatusForErrorMsg(LCMCommandStatus.REJECTED, "The query command is not supported");
92             return setOutputStatus();
93         }
94
95         logger.debug(String.format("MockQueryHelper loading property from file %s", MOCK_QUERY_FILENAME));
96         try {
97             properties.load(new FileInputStream(MOCK_QUERY_FILENAME));
98         } catch (IOException e) {
99             // when loading propertes from mock file failed, return with associated message
100             status = buildStatusForErrorMsg(
101                 LCMCommandStatus.REJECTED, "cannot load properties from " + MOCK_QUERY_FILENAME);
102             return setOutputStatus();
103         }
104
105         String key = "VNF_IDS";
106         List<String> vnfIds =
107             Arrays.asList(properties.getProperty(key, "").split(DELIMITER_COMMA));
108         logger.debug(String.format("MockQueryHelper got vnfId %s", vnfIds.toString()));
109
110         String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
111         if (!vnfIds.contains(vnfId)) {
112             status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
113             return setOutputStatus();
114         }
115
116         key = "VMS_" + vnfId;
117         List<String> vmIds =
118             Arrays.asList(properties.getProperty(key, "").split(DELIMITER_COMMA));
119         logger.debug(String.format("MockQueryHelper got vmId %s", vmIds.toString()));
120
121         List<QueryResults> queryResultList = new ArrayList<>();
122         VmState vfState;
123         VmStatus vfStatus;
124         boolean found = false;
125         for (String vmId : vmIds) {
126             // state
127             vfState = VmState.Unknown;
128             key = "STATE_" + vmId;
129             String stateProp = properties.getProperty(key, "").toUpperCase();
130             for (Map.Entry<VmState, List<String>> aEntry: VF_STATE_MAP.entrySet()) {
131                 if (aEntry.getValue().contains(stateProp)) {
132                     vfState = aEntry.getKey();
133                     break;
134                 }
135             }
136
137             // status
138             vfStatus = VmStatus.Unknown;
139             key = "STATUS_" + vmId;
140             String statusProp = properties.getProperty(key, "unknown").toLowerCase();
141             for (VmStatus otherVfStatus : VmStatus.values()) {
142                 if (statusProp.equalsIgnoreCase(otherVfStatus.name())) {
143                     vfStatus = otherVfStatus;
144                 }
145             }
146
147             QueryResultsBuilder queryResultBuilder = new QueryResultsBuilder();
148             queryResultBuilder.setVserverId(vmId);
149             queryResultBuilder.setVmState(vfState);
150             queryResultBuilder.setVmStatus(vfStatus);
151             queryResultList.add(queryResultBuilder.build());
152             found = true;
153         }
154
155         if (found) {
156             status = buildStatusWithoutParams(LCMCommandStatus.SUCCESS);
157             requestHandlerOutput.getResponseContext().setPayloadObject(queryResultList);
158         } else {
159             status = buildStatusForErrorMsg(LCMCommandStatus.VNF_NOT_FOUND, "no detailss for vnfId");
160         }
161
162         return setOutputStatus();
163     }
164 }