2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.resmanagement.service.dao.impl.aai;
19 import java.util.ArrayList;
20 import java.util.List;
23 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
24 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
25 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
26 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
27 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.VnfDao;
28 import org.onap.vfc.nfvo.resmanagement.service.entity.VnfEntity;
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
33 public class VnfAaiDaoImpl implements VnfDao {
35 private static int VNF_AAI_DAO_SUCCESS = 1;
37 private static int VNF_AAI_DAO_FAIL = -1;
40 public List<VnfEntity> getVnfs(Map<String, Object> condition) {
41 ArrayList<VnfEntity> vnfList = new ArrayList<>();
43 if(condition.containsKey("id")) {
44 vnfList.add(getVnf((String)condition.get("id")));
46 RestfulParametes restfulParametes = new RestfulParametes();
47 restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
49 RestfulResponse response = RestfulUtil.getRestfulResponse(
50 "https://192.168.17.24:8443/aai/v11/network/generic-vnfs", restfulParametes, "get");
52 JSONObject jsonObject = JSONObject.fromObject(response.getResponseContent());
53 JSONArray jsonArray = jsonObject.getJSONArray("generic-vnf");
55 jsonArray.forEach(genericVnf -> vnfList.add(VnfEntity.toEntityFromAai((JSONObject)genericVnf)));
61 public VnfEntity getVnf(String id) {
62 RestfulParametes restfulParametes = new RestfulParametes();
63 restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
65 RestfulResponse response = RestfulUtil.getRestfulResponse(
66 "https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/" + id, restfulParametes, "get");
68 JSONObject jsonObject = JSONObject.fromObject(response.getResponseContent());
69 VnfEntity vnfEntity = VnfEntity.toEntityFromAai(jsonObject);
74 public int addVnf(VnfEntity vnfEntity) {
75 RestfulParametes restfulParametes = new RestfulParametes();
76 restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
77 restfulParametes.setRawData(vnfEntity.toStringForAai());
79 RestfulResponse response = RestfulUtil.getRestfulResponse(
80 "https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/" + vnfEntity.getVnfInstanceId(),
81 restfulParametes, "put");
83 return response == null || response.getStatus() == -1 ? VNF_AAI_DAO_FAIL : VNF_AAI_DAO_SUCCESS;
87 public int deleteVnfById(String id) {
88 VnfEntity vnfEntity = getVnf(id);
90 if(vnfEntity != null) {
91 RestfulParametes restfulParametes = new RestfulParametes();
92 restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
93 restfulParametes.put("resource-version", vnfEntity.getResourceVersion());
95 RestfulResponse response = RestfulUtil
96 .getRestfulResponse("https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/"
97 + vnfEntity.getVnfInstanceId(), restfulParametes, "delete");