2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.mock.query;
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;
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;
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.
50 * When backend is implemented, this file should be removed.
52 public class MockQueryHelper extends AbstractMockHelper {
53 private final String MOCK_QUERY_FILENAME = "/tmp/lcm/query";
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>>() {
58 put(VmState.Inactive, Arrays.asList(
66 put(VmState.Active, Arrays.asList(
70 put(VmState.Standby, Arrays.asList(
76 private final EELFLogger logger = EELFManager.getInstance().getLogger(MockQueryHelper.class);
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
87 public RequestHandlerOutput query(RequestHandlerInput input) {
88 File file = new File(MOCK_QUERY_FILENAME);
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();
95 logger.debug(String.format("MockQueryHelper loading property from file %s", MOCK_QUERY_FILENAME));
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();
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()));
110 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
111 if (!vnfIds.contains(vnfId)) {
112 status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
113 return setOutputStatus();
116 key = "VMS_" + vnfId;
118 Arrays.asList(properties.getProperty(key, "").split(DELIMITER_COMMA));
119 logger.debug(String.format("MockQueryHelper got vmId %s", vmIds.toString()));
121 List<QueryResults> queryResultList = new ArrayList<>();
124 boolean found = false;
125 for (String vmId : vmIds) {
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();
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;
147 QueryResultsBuilder queryResultBuilder = new QueryResultsBuilder();
148 queryResultBuilder.setVserverId(vmId);
149 queryResultBuilder.setVmState(vfState);
150 queryResultBuilder.setVmStatus(vfStatus);
151 queryResultList.add(queryResultBuilder.build());
156 status = buildStatusWithoutParams(LCMCommandStatus.SUCCESS);
157 requestHandlerOutput.getResponseContext().setPayloadObject(queryResultList);
159 status = buildStatusForErrorMsg(LCMCommandStatus.VNF_NOT_FOUND, "no detailss for vnfId");
162 return setOutputStatus();