abc4ccb377a05598fc6957a87dd214f6f94ff9e8
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.service.dao.impl.aai;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22
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;
29
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
32
33 public class VnfAaiDaoImpl implements VnfDao {
34
35     private static int VNF_AAI_DAO_SUCCESS = 1;
36
37     private static int VNF_AAI_DAO_FAIL = -1;
38
39     @Override
40     public List<VnfEntity> getVnfs(Map<String, Object> condition) {
41         ArrayList<VnfEntity> vnfList = new ArrayList<>();
42
43         if(condition.containsKey("id")) {
44             vnfList.add(getVnf((String)condition.get("id")));
45         } else {
46             RestfulParametes restfulParametes = new RestfulParametes();
47             restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
48
49             RestfulResponse response = RestfulUtil.getRestfulResponse(
50                     "https://192.168.17.24:8443/aai/v11/network/generic-vnfs", restfulParametes, "get");
51
52             JSONObject jsonObject = JSONObject.fromObject(response.getResponseContent());
53             JSONArray jsonArray = jsonObject.getJSONArray("generic-vnf");
54
55             jsonArray.forEach(genericVnf -> vnfList.add(VnfEntity.toEntityFromAai((JSONObject)genericVnf)));
56         }
57         return vnfList;
58     }
59
60     @Override
61     public VnfEntity getVnf(String id) {
62         RestfulParametes restfulParametes = new RestfulParametes();
63         restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
64
65         RestfulResponse response = RestfulUtil.getRestfulResponse(
66                 "https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/" + id, restfulParametes, "get");
67
68         JSONObject jsonObject = JSONObject.fromObject(response.getResponseContent());
69         VnfEntity vnfEntity = VnfEntity.toEntityFromAai(jsonObject);
70         return vnfEntity;
71     }
72
73     @Override
74     public int addVnf(VnfEntity vnfEntity) {
75         RestfulParametes restfulParametes = new RestfulParametes();
76         restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
77         restfulParametes.setRawData(vnfEntity.toStringForAai());
78
79         RestfulResponse response = RestfulUtil.getRestfulResponse(
80                 "https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/" + vnfEntity.getVnfInstanceId(),
81                 restfulParametes, "put");
82
83         return response == null || response.getStatus() == -1 ? VNF_AAI_DAO_FAIL : VNF_AAI_DAO_SUCCESS;
84     }
85
86     @Override
87     public int deleteVnfById(String id) {
88         VnfEntity vnfEntity = getVnf(id);
89
90         if(vnfEntity != null) {
91             RestfulParametes restfulParametes = new RestfulParametes();
92             restfulParametes.setHeaderMap(RequestUtil.getAAIHeaderMap());
93             restfulParametes.put("resource-version", vnfEntity.getResourceVersion());
94
95             RestfulResponse response = RestfulUtil
96                     .getRestfulResponse("https://192.168.17.24:8443/aai/v11/network/generic-vnfs/generic-vnf/"
97                             + vnfEntity.getVnfInstanceId(), restfulParametes, "delete");
98         }
99         return 1;
100     }
101 }