2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2018 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package com.onap.sdnc.vnfbackupservice.service;
22 import java.io.IOException;
23 import java.sql.Timestamp;
24 import java.util.ArrayList;
25 import java.util.List;
28 import org.apache.log4j.Logger;
29 import org.json.JSONArray;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.http.HttpEntity;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpMethod;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Service;
39 import org.springframework.web.client.RestTemplate;
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.databind.JsonMappingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.onap.sdnc.vnfbackupservice.dao.VnfBackupServiceRepo;
45 import com.onap.sdnc.vnfbackupservice.model.VnfConfigDetailsDB;
46 import com.onap.sdnc.vnfbackupservice.model.VnfDisplayParams;
47 import com.onap.sdnc.vnfbackupservice.model.VnfServiceResponse;
49 import com.onap.sdnc.vnfbackupservice.scheduler.VnfConfigBackupScheduler;
50 import com.onap.sdnc.vnfbackupservice.scheduler.VnfRestClient;
53 public class VnfbackupServiceImpl implements VnfbackupService {
56 private RestTemplate restTemplate;
59 VnfBackupServiceRepo vnfBackupServiceDao;
62 VnfConfigBackupScheduler vnfConfigBackupScheduler;
64 @Value("${sdnc.rest.vnf.api.host}")
67 @Value("${sdnc.rest.vnf.api.port}")
70 @Value("${sdnc.rest.vnf.api.basepath}")
71 private String basePath;
73 @Value("${sdnc.rest.vnf.api.username}")
74 private String username;
76 @Value("${sdnc.rest.vnf.api.password}")
77 private String password;
80 VnfRestClient vnfRestClientlocal;
82 private static final Logger logger = Logger.getLogger(VnfbackupServiceImpl.class);
85 public VnfServiceResponse getAllVnfDetails() {
86 VnfServiceResponse vnfServiceResponse = new VnfServiceResponse();
87 String finalURL = "http://" + host + ":" + port + basePath + "/config/VNF-API:vnfs";
88 String response = vnfRestClientlocal.vnfRestClient(finalURL, username, password);
89 List<VnfDisplayParams> displayParams = parseVnfConfigDetails(response);
90 vnfServiceResponse.setVnfDisplayList(displayParams);
91 return vnfServiceResponse;
94 public List<VnfDisplayParams> parseVnfConfigDetails(String jsonInput) {
95 List<VnfDisplayParams> displayParams = new ArrayList<VnfDisplayParams>();
98 JSONObject vnf = new JSONObject(jsonInput);
99 JSONArray vnfList = vnf.getJSONObject("vnfs").getJSONArray("vnf-list");
101 for (int i = 0; i < vnfList.length(); i++) {
102 VnfDisplayParams vnfDisplayParams = new VnfDisplayParams();
103 String responseStatus = vnfList.getJSONObject(i).getJSONObject("service-status").get("response-code")
105 if ("200".equalsIgnoreCase(responseStatus)) {
106 String vnfId = vnfList.getJSONObject(i).get("vnf-id").toString();
107 String vnfName = vnfList.getJSONObject(i).getJSONObject("service-data")
108 .getJSONObject("vnf-request-information").get("vnf-name").toString();
109 vnfDisplayParams.setVnfId(vnfId);
110 vnfDisplayParams.setVnfName(vnfName);
111 displayParams.add(vnfDisplayParams);
114 } catch (JSONException e) {
115 logger.error("Exception is at parseVnfConfigDetails() : " + e);
117 return displayParams;
121 public String backupVnfconfig(String vnfId) {
122 long millis = System.currentTimeMillis();
123 Timestamp date = new java.sql.Timestamp(millis);
124 String finalURL = "http://" + host + ":" + port + basePath + "/config/VNF-API:vnfs/vnf-list/" + vnfId;
125 logger.debug("connecting to restconf device:::" + finalURL);
126 String response = vnfRestClientlocal.vnfRestClient(finalURL, username, password);
127 String configInfo = response;
128 Timestamp creationDate = date;
129 Timestamp lastupdated = date;
131 String vnfid = vnfId;
133 VnfConfigDetailsDB getVnfDetails = null;
134 String vnfversion = "Version-1";
136 getVnfDetails = vnfBackupServiceDao.getVnfDetail(vnfId);
137 } catch (Exception e) {
138 logger.error("exception is at getVnfdetails() : " + e);
140 if (getVnfDetails == null) {
143 vnf = new JSONObject(response);
144 vnfname = vnf.getJSONArray("vnf-list").getJSONObject(0).getJSONObject("service-data")
145 .getJSONObject("vnf-request-information").get("vnf-name").toString();
147 } catch (JSONException e) {
148 logger.error("exception is at getVnfdetails() : " + e);
150 vnfBackupServiceDao.saveVnfDetails(configInfo, creationDate, lastupdated, status, vnfid, vnfname,
154 String[] vnfvesionsplit = getVnfDetails.getVnfversion().split("-");
155 int tmpVnfversion = Integer.parseInt(vnfvesionsplit[1]) + 1;
156 vnfversion = vnfvesionsplit[0] + "-" + String.valueOf(tmpVnfversion);
158 ObjectMapper om = new ObjectMapper();
160 Map<String, Object> m1 = (Map<String, Object>) (om.readValue(getVnfDetails.getConfiginfo(),
162 Map<String, Object> m2 = (Map<String, Object>) (om.readValue(response, Map.class));
165 vnf = new JSONObject(response);
166 vnfname = vnf.getJSONArray("vnf-list").getJSONObject(0).getJSONObject("service-data")
167 .getJSONObject("vnf-request-information").get("vnf-name").toString();
169 if (!m1.equals(m2)) {
170 vnfBackupServiceDao.saveVnfDetails(configInfo, creationDate, lastupdated, status, vnfid,
171 vnfname, vnfversion);
173 } catch (Exception e) {
174 logger.error("exception is at getVnfdetails() : " + e);
176 } catch (Exception e) {
177 logger.error("exception is at getVnfdetails() : " + e);
184 public String putVnfconfig(String configfile, String vnfId) {
186 String indented = null;
188 if (configfile != null) {
189 restTemplate = new RestTemplate();
190 String finalURL = "http://" + host + ":" + port + basePath + "/config/VNF-API:vnfs/vnf-list/" + vnfId;
192 HttpHeaders headers = new HttpHeaders();
194 logger.info("connecting to restconf device:::" + finalURL);
195 String response = vnfRestClientlocal.vnfRestClient(finalURL, username, password);
196 logger.info(response);
197 ObjectMapper mapper = new ObjectMapper();
199 Object json = mapper.readValue(response, Object.class);
200 indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
201 logger.info(indented);
202 } catch (JsonParseException e) {
203 logger.error("exception occer" + e);
204 } catch (JsonMappingException e) {
205 logger.error("exception occer" + e);
206 } catch (IOException e) {
207 logger.error("exception occer" + e);
210 if (!(configfile.equals(indented))) {
211 HttpEntity<String> requestEntity = new HttpEntity<String>(configfile,
212 vnfRestClientlocal.generateHeaders(headers, username, password));
214 ResponseEntity<String> uri = restTemplate.exchange(finalURL, HttpMethod.PUT, requestEntity,
216 logger.info(uri.getStatusCode());
217 if (uri.getStatusCodeValue() == 200) {
218 vnfConfigBackupScheduler.initiateBackupService();
222 throw new RuntimeException("Both configurations are same");
229 public String updatedBackuptime() {
230 String sdtime = vnfBackupServiceDao.getvnfschedulertime();