2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package com.onap.sdnc.reports.service;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.net.InetAddress;
27 import java.net.Socket;
28 import java.net.UnknownHostException;
29 import java.text.SimpleDateFormat;
30 import java.util.Calendar;
31 import java.util.List;
33 import org.apache.log4j.Logger;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.google.gson.Gson;
38 import com.onap.sdnc.reports.model.CertificationInputs;
39 import com.onap.sdnc.reports.model.DeviceConfig;
40 import com.onap.sdnc.reports.model.ODLClientResponse;
41 import com.onap.sdnc.reports.model.Output;
42 import com.onap.sdnc.reports.model.PreTestResponse;
43 import com.onap.sdnc.reports.repository.DeviceRepository;
44 import com.onap.sdnc.reports.repository.PreTestConfigRepository;
47 public class CertificationClientService {
49 private static final Logger logger = Logger.getLogger(CertificationClientService.class);
53 DeviceRepository deviceRepository;
56 PreTestConfigRepository preTestRepo;
58 ObjectMapper mapper = new ObjectMapper();
60 public void restClient(CertificationInputs vnfinfo, List<PreTestResponse> preTestNew, String testType) {
62 PreTestResponse preTest = new PreTestResponse();
64 Output output = new Output();
65 if ("network".equalsIgnoreCase(testType)) {
66 output = pingTest(vnfinfo);
68 if ("protocol".equalsIgnoreCase(testType)) {
69 output = protocolTest(vnfinfo);
71 preTest.setStatus(output.getStatus());
72 preTest.setIpaddress(output.getIpaddress());
73 preTest.setStatistics(output.getStatistics());
74 preTest.setAvgTime(output.getAvgTime());
75 preTest.setTesttype(testType);
76 preTest.setHostname(output.getHostname());
77 preTestNew.add(preTest);
79 ODLClientResponse odlClientResponse=new ODLClientResponse();
80 odlClientResponse.setOutput(output);
82 testSaveResults(preTest, odlClientResponse);
85 public static Output pingTest(CertificationInputs vnfinfo) {
87 Output output = new Output();
88 String pingCmd = "ping " + vnfinfo.getInput().getIpaddress();
89 String pingResult = "";
90 String testResult = "fail";
91 String status = "unreachable";
93 String timeRes = null;
94 String percentile = null;
96 boolean flag1 = false;
98 InetAddress byIpaddress = InetAddress.getByName(vnfinfo.getInput().getIpaddress());
99 String byHostName=vnfinfo.getInput().getHostname();
100 flag = byIpaddress.isReachable(5000);
102 } catch (UnknownHostException e) {
103 logger.info("Network certification Exception : " + e);
104 } catch (IOException e) {
105 logger.info("Network certification Exception : " + e);
110 Runtime r = Runtime.getRuntime();
111 Process p = r.exec(pingCmd);
112 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
114 while ((inputLine = in.readLine()) != null) {
115 if (pingResult.equals("")) {
116 pingResult = inputLine;
118 pingResult += "~" + inputLine;
121 String[] results = pingResult.split("~");
122 for (String res : results) {
123 if (res.trim().contains("Packets:")) {
125 status = "reachable";
126 String packets = res.trim();
127 String[] lossPercentile = packets.split("\\(");
128 percentile = lossPercentile[1].replace(")", "").replace(",", "").trim();
130 if (res.trim().contains("Minimum")) {
131 String timeMs = res.trim();
132 String[] time = timeMs.split(",");
137 } catch (Exception e) {
138 logger.info("Network certification Exception : " + e);
140 status = "unreachable";
141 reason = e.toString();
144 output.setAvgTime(timeRes);
145 output.setStatistics(percentile);
146 output.setHostname(vnfinfo.getInput().getHostname());
147 output.setIpaddress(vnfinfo.getInput().getIpaddress());
148 output.setReason(reason);
149 output.setTestresult(testResult);
150 output.setStatus(status);
155 public static Output protocolTest(CertificationInputs vnfinfo) {
156 Output output = new Output();
158 String status = "unreachable";
159 String reason = null;
161 s = new Socket(vnfinfo.getInput().getIpaddress(), Integer.parseInt("445"));
162 status = "reachable";
163 } catch (Exception e) {
164 logger.info("Protocol certification Exception : " + e);
165 reason = e.toString();
166 status = "unreachable";
171 } catch (Exception e) {
172 logger.info("Protocol certification Exception : " + e);
173 reason = e.toString();
174 status = "unreachable";
177 output.setStatus(status);
178 output.setIpaddress(vnfinfo.getInput().getIpaddress());
179 output.setReason(reason);
184 public void testSaveResults(PreTestResponse preTest,ODLClientResponse output) {
188 String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
190 DeviceConfig devicename = deviceRepository.findDeviceIP(preTest.getIpaddress());
191 devId = devicename.getId();
192 } catch (Exception e) {
196 deviceRepository.logDeviceName(preTest.getIpaddress(), timeStamp);
199 DeviceConfig devicename = deviceRepository.findDeviceIP(preTest.getIpaddress());
200 devId = devicename.getId();
202 Gson gson = new Gson();
203 String testName= preTest.getTesttype();
204 String result = preTest.getStatus();
205 String execuationDetails = gson.toJson(output);
207 preTestRepo.logPreTestReport(testName, result, execuationDetails, timeStamp, devId);