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