2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.provider.lcm.mock.query;
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;
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;
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.
51 * When backend is implemented, this file should be removed.
53 public class MockQueryHelper extends AbstractMockHelper {
54 private final String MOCK_QUERY_FILENAME = "/tmp/lcm/query";
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>>() {
59 put(VmState.Inactive, Arrays.asList(
67 put(VmState.Active, Arrays.asList(
71 put(VmState.Standby, Arrays.asList(
77 private final EELFLogger logger = EELFManager.getInstance().getLogger(MockQueryHelper.class);
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
88 public RequestHandlerOutput query(RequestHandlerInput input) {
89 File file = new File(MOCK_QUERY_FILENAME);
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();
96 logger.debug(String.format("MockQueryHelper loading property from file %s", MOCK_QUERY_FILENAME));
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();
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()));
111 String vnfId = input.getRequestContext().getActionIdentifiers().getVnfId();
112 if (!vnfIds.contains(vnfId)) {
113 status = buildStatusForVnfId(LCMCommandStatus.VNF_NOT_FOUND, vnfId);
114 return setOutputStatus();
117 key = "VMS_" + vnfId;
119 Arrays.asList(properties.getProperty(key, "").split(DELIMITER_COMMA));
120 logger.debug(String.format("MockQueryHelper got vmId %s", vmIds.toString()));
122 List<QueryResults> queryResultList = new ArrayList<>();
125 boolean found = false;
126 for (String vmId : vmIds) {
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();
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;
148 QueryResultsBuilder queryResultBuilder = new QueryResultsBuilder();
149 queryResultBuilder.setVserverId(vmId);
150 queryResultBuilder.setVmState(vfState);
151 queryResultBuilder.setVmStatus(vfStatus);
152 queryResultList.add(queryResultBuilder.build());
157 status = buildStatusWithoutParams(LCMCommandStatus.SUCCESS);
158 requestHandlerOutput.getResponseContext().setPayloadObject(queryResultList);
160 status = buildStatusForErrorMsg(LCMCommandStatus.VNF_NOT_FOUND, "no detailss for vnfId");
163 return setOutputStatus();