diff options
Diffstat (limited to 'vesagent/src')
7 files changed, 403 insertions, 471 deletions
diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/JsonStoreMap.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/JsonStoreMap.java index deecfd8..32c6bc8 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/JsonStoreMap.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/JsonStoreMap.java @@ -30,21 +30,21 @@ import org.slf4j.LoggerFactory; * If need to add any values --> add to JsonStoreMap */ public class JsonStoreMap { - protected static final Map<String, List<JsonAlarmStorage>> map = new HashMap<String, List<JsonAlarmStorage>>(); + protected static final Map<String, List<JsonAlarmStorage>> map = new HashMap<>(); private final Logger log = LoggerFactory.getLogger(JsonStoreMap.class); - private String line = "......................"; + private String line = "......................"; public Map<String, List<JsonAlarmStorage>> addToMap(String key,List<JsonAlarmStorage> value){ - log.info ("adding to map"); + log.info("adding to map"); map.put(key, value); return map; } public Map<String, List<JsonAlarmStorage>> updateMap(String key,List<JsonAlarmStorage> value){ - log.info("updating map"); - map.put(key, value); - return map; + log.info("updating the Map"); + map.put(key, value); + return map; } public Map<String, List<JsonAlarmStorage>> updateMapBatch(String vesSendStatus){ - log.info("updating map for batch"); + log.info("updating the map for batch"); Iterator i = map.keySet().iterator(); while(i.hasNext()) { @@ -55,26 +55,19 @@ public class JsonStoreMap { log.info("updated total batch with vesSendStatus = 'failed'"); return map; } - public String retrieveAlarmConditionFromMap(String key){ - List<JsonAlarmStorage> value; - System.out.println("retriving alarm condition from map"); - if (map.containsKey(key)) { - value = map.get(key); - //System.out.println("Key : " + key +" value :"+ value); - return value.get(0).alarm; - } - return "uuid not found"; - } - public String retrieveVesSendStatusFromMap(String key){ + public String retrieveFromMap(String key, String type){ List<JsonAlarmStorage> value; log.info("retriving alarm condition from map"); if (map.containsKey(key)) { value = map.get(key); - //System.out.println("Key : " + key +" value :"+ value); - return value.get(0).vesSendStatus; + if(type=="ALARM"){ + return value.get(0).alarm; + }else if(type=="VES_STATUS"){ + return value.get(0).vesSendStatus; + } } - return "uuid not found"; + return null; } public JSONObject retrieveJsonFromMap(String key){ @@ -82,7 +75,6 @@ public class JsonStoreMap { log.info("retriving json from map"); if (map.containsKey(key)) { value = map.get(key); - //System.out.println("Key : " + key +" value :"+ value); return value.get(0).json; } return null; @@ -98,31 +90,31 @@ public class JsonStoreMap { { String key = i.next().toString(); List<JsonAlarmStorage> value = map.get(key); - String value1=value.get(0).alarm+""+value.get(0).json.toString() ; - log.info("Key : " + key +" value :"+ value.get(0).alarm+""+value.get(0).json); + String value1=value.get(0).alarm+""+value.get(0).json.toString(); + log.info("Key : " + key +" value :"+value1); JSONObject json = value.get(0).json; log.info(line); - log.info(json.get("event").toString()); + log.info(json.get("event").toString()); JSONObject obj = (JSONObject) json.get("event"); list.add(obj); } - log.info(line); + log.info(line); eventList.put("eventList", list); - log.info(eventList.toString()); - log.info(line); + log.info(eventList.toString()); + log.info(line); return eventList; } @SuppressWarnings("rawtypes") - public void displayAllEntriesInMap(){ - log.info("retrive all from map"); + public void displayALLEntriesInMap(){ + log.info("retrive all the entries from map"); Iterator i = map.keySet().iterator(); String mapValues; while(i.hasNext()){ String key = i.next().toString(); List<JsonAlarmStorage> value = map.get(key); - mapValues = "uuid: "+key+" jsonStructure: "+value.get(0).json.toString()+" AlarmCondition: "+value.get(0).alarm+": VesSend status: "+value.get(0).vesSendStatus; + mapValues = "uuid: "+key+" jsonStructure: "+value.get(0).json.toString()+" AlarmCondition: "+value.get(0).alarm+": VesSend status: "+value.get(0).vesSendStatus; log.info(mapValues); } } @@ -134,34 +126,28 @@ public class JsonStoreMap { } @SuppressWarnings("rawtypes") -public void deleteUsingAlarmCondition(String alarm){ - Iterator it = map.entrySet().iterator(); - while (it.hasNext()) - { - Entry item = (Entry) it.next(); - System.out.println(item.getKey());; - List<JsonAlarmStorage> value = map.get(item.getKey()); - if(value.get(0).alarm == alarm) - { - it.remove(); - log.info("removed.."); + public void deleteUsingAlarmCondition(String alarm){ + Iterator it = map.entrySet().iterator(); + while (it.hasNext()){ + Entry item = (Entry) it.next(); + List<JsonAlarmStorage> value = map.get(item.getKey()); + if(value.get(0).alarm == alarm){ + it.remove(); + log.info("removed.."); + } } + log.info("removed"); } - log.info("removed"); -} - - - public void deleteAllFromMap(){ - log.info("clearing map"); + log.info("clearing map..."); map.clear(); - log.info("map cleared.."); + log.info("map cleared..."); log.info(map.toString()); } public int totalEntriesInMap(){ - log.info("Total entries in map", map.size()); + log.info("Total entries in map",map.size()); return map.size(); } diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VESRestAPI.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VESRestAPI.java index db8e580..d2450cb 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VESRestAPI.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VESRestAPI.java @@ -13,9 +13,9 @@ package org.onap.multicloud.openstack.vmware; -import java.io.IOException; import java.net.Socket; import java.util.List; +import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; @@ -24,12 +24,11 @@ import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /*Contain all methods for rest api calls*/ + @SuppressWarnings("deprecation") public class VESRestAPI{ String eventType; - int counter_connectionRefused = 0; private StringEntity entity; private HttpPost post; private Socket sock; @@ -40,80 +39,79 @@ public class VESRestAPI{ private String batchEvent = "batch"; private String vesSendStatusSuccess="success"; @SuppressWarnings({ "resource" }) - public boolean publishAnyEventToVES(VesEntity ves ,JsonStoreMap map, VesTimer timer,String uuidKey,List<JsonAlarmStorage> list, Integer duration,String vesSendStatus) throws IOException{ - try{ - eventType = anyEvent; - log.info("inside publish any event method"); - sock = new Socket(ves.getVesip(), ves.getPort()); - log.info(String.valueOf(sock.isConnected())); - if(sock.isConnected()){ - log.info("list "+list.get(0).json.toString()); - client = new DefaultHttpClient(); - /* check the vesStructure whether each of the variable like ip address , portnumber , user name and password contains something or is null */ - if(vesEntitycredentialCheckSuccess(ves)){ - post = new HttpPost("http://"+ves.getVesip()+":"+ves.getPort()+"/eventListener/v5"); - log.info(ves.getPort().toString()); - }else{ - log.info("Null"); - } - entity = new StringEntity(list.get(0).json.toString()); - post.setEntity(entity); - post.setHeader("Content-type", applicationJson); - HttpResponse response = client.execute(post); - log.info(response.getStatusLine().toString()); - if(vESCollectorSendStatus(response,map,uuidKey,list,timer,vesSendStatus)){ - return true; + public boolean publishAnyEventToVES(VesEntity ves ,JsonStoreMap map, VesTimer timer,String uuidKey,List<JsonAlarmStorage> list, Integer duration,String vesSendStatus) throws IOException{ + try{ + eventType = anyEvent; + log.info("inside publish any event method"); + sock = new Socket(ves.getVesip(), ves.getPort()); + log.info(String.valueOf(sock.isConnected())); + if(sock.isConnected()){ + log.info("list "+list.get(0).json.toString()); + client = new DefaultHttpClient(); + /* check the vesStructure whether each of the variable like ip address , portnumber , user name and password contains something or is null */ + if(vesEntitycredentialCheckSuccess(ves)){ + post = new HttpPost("http://"+ves.getVesip()+":"+ves.getPort()+"/eventListener/v5"); + log.info(ves.getPort().toString()); + }else{ + log.info("Null"); + } + entity = new StringEntity(list.get(0).json.toString()); + post.setEntity(entity); + post.setHeader("Content-type", applicationJson); + HttpResponse response = client.execute(post); + log.info(response.getStatusLine().toString()); + if(vESCollectorSendStatus(response,map,uuidKey,list,timer,vesSendStatus)){ + return true; } sock.close(); client.close(); - } - }catch(Exception e){ - log.info("VES Collector connection refused exception error occured"); - log.error("error",e); - list.get(0).vesSendStatus = "failed"; - map.updateMap(uuidKey, list); - if (!timer.isTimerRunning()){ - timer.startTimer(duration); - } - else log.info("VES Collector Connection Refushed Timer is running "); -} - return false; + } + }catch(Exception e){ + log.info("VES Collector connection refused, exception occured"); + log.error("error",e); + list.get(0).vesSendStatus = "failed"; + map.updateMap(uuidKey, list); + if (!timer.isTimerRunning()){ + timer.startTimer(duration); + } + else log.info("VES Collector Connection refused, timer is running "); + } -} + return false; + } - @SuppressWarnings("resource") - public boolean publishBatchEventToVES(VesEntity ves, JSONObject alarmJsonConstructArray,JsonStoreMap map, VesTimer timer,String uuidKey,List<JsonAlarmStorage> list, Integer duration,String vesSendStatus) throws IOException{ - try { - eventType = batchEvent; - log.info("inside publish batch event method"); - sock = new Socket(ves.getVesip(), ves.getPort()); - if(sock.isConnected()){ - log.info("Sending batch"); - client = new DefaultHttpClient(); - if(vesEntitycredentialCheckSuccess(ves)) - { + @SuppressWarnings("resource") + public boolean publishBatchEventToVES(VesEntity ves, JSONObject alarmJsonConstructArray,JsonStoreMap map, VesTimer timer,String uuidKey,List<JsonAlarmStorage> list, Integer duration,String vesSendStatus) throws IOException{ + try { + eventType = batchEvent; + log.info("inside publish batch event method"); + sock = new Socket(ves.getVesip(), ves.getPort()); + if(sock.isConnected()){ + log.info("Sending batch"); + client = new DefaultHttpClient(); + if(vesEntitycredentialCheckSuccess(ves)) + { post = new HttpPost("http://"+ves.getVesip()+":"+ves.getPort()+"/eventListener/v5/eventBatch"); - }else{ - log.info("Null"); - } - entity = new StringEntity(alarmJsonConstructArray.toJSONString()); - post.setEntity(entity); - post.setHeader("Content-type", applicationJson); - post.addHeader("Accept", applicationJson); - HttpResponse response = client.execute(post); - log.info(response.getStatusLine().toString()); - if(!connectionRefused(response)){ - return true; - } - - if(vESCollectorSendStatus(response,map,uuidKey,list,timer,vesSendStatus)){ - return true; - } - } - sock.close(); - client.close(); + }else{ + log.info("Null"); + } + entity = new StringEntity(alarmJsonConstructArray.toJSONString()); + post.setEntity(entity); + post.setHeader("Content-type", applicationJson); + post.addHeader("Accept", applicationJson); + HttpResponse response = client.execute(post); + log.info(response.getStatusLine().toString()); + if(!connectionRefused(response)){ + return true; + } + if(vESCollectorSendStatus(response,map,uuidKey,list,timer,vesSendStatus)){ + return true; + } + } + sock.close(); + client.close(); }catch(Exception e){ - log.info("connection refused exception error occured"); + log.info("connection refused, exception occured"); log.error("error",e); list.get(0).vesSendStatus = "failed"; map.updateMap(uuidKey, list); //modifying map with vesSendStatus as failed @@ -121,68 +119,69 @@ public class VESRestAPI{ if (!timer.isTimerRunning()){ log.info("timer started...."); timer.startTimer(duration); - } - else{ - log.info("Connection refused timer is running"); - } + } + else{ + log.info("Connection refused, timer is running"); + } } return false; - } - public boolean vesEntitycredentialCheckSuccess(VesEntity ves){ - if(ves.getPort()==null || ves.getVesip()==null || ves.getVesPassword()==null || ves.getVesUsername()==null){ - return false; - } - return true; - } + } -public boolean vESCollectorSendStatus ( HttpResponse response,JsonStoreMap map,String uuidKey, List<JsonAlarmStorage> list,VesTimer timer,String vesSendStatus) throws ParseException { + public boolean vesEntitycredentialCheckSuccess(VesEntity ves){ + if(ves.getPort()==null || ves.getVesip()==null || ves.getVesPassword()==null || ves.getVesUsername()==null){ + return false; + } + return true; + } - JsonAlarmStorage store = list.get(0); - log.info(String.valueOf(list.size())); - log.info(store.alarm); - String alarmStatus = store.alarm; - log.info("insiide ves collector method"); - if(response.getStatusLine().getStatusCode()>=200 && response.getStatusLine().getStatusCode()<300){ - log.info("..........."); - if(alarmStatus == "ON" && eventType==anyEvent){ - log.info("alarm on ,event type is any and connection is 200 ok"); - log.info("Test list for satus"+list.get(0).vesSendStatus); - log.info("total entrie sin map: ",map.totalEntriesInMap()); - map.displayAllEntriesInMap(); - list.get(0).vesSendStatus = vesSendStatusSuccess; - map.updateMap(uuidKey, list); - }else if(alarmStatus == "ON" && eventType==batchEvent){ - log.info("alarm on ,event type is any and connection is 200 ok"); - log.info("Test list for satus"+list.get(0).vesSendStatus); - log.info("total entrie:",map.totalEntriesInMap()); - map.displayAllEntriesInMap(); - map.updateMapBatch("success"); - }else if (alarmStatus == "OFF" && (vesSendStatus=="new" || vesSendStatus ==vesSendStatusSuccess) && eventType==anyEvent){ - log.info("alarm off and connection ok"); - map.deleteFromMap(uuidKey); - } else if(alarmStatus == "OFF" && (vesSendStatus=="new" || vesSendStatus ==vesSendStatusSuccess) && eventType==batchEvent){ - log.info("alarm off and connection ok"); - map.deleteUsingAlarmCondition(alarmStatus); - } - - if(timer.isTimerRunning()){ - timer.stopTimer(); - } - else{ - log.info("timer is not running...."); - } - return true; - } - else - { - log.info("connection error !200"); - return false; - } - } - public boolean connectionRefused(HttpResponse response){ - if(response.getStatusLine().getStatusCode()!=200){ - return true; - } - return false; - } + + public boolean vESCollectorSendStatus ( HttpResponse response,JsonStoreMap map,String uuidKey, List<JsonAlarmStorage> list,VesTimer timer,String vesSendStatus) throws ParseException { + JsonAlarmStorage store = list.get(0); + log.info(String.valueOf(list.size())); + log.info(store.alarm); + String alarmStatus = store.alarm; + log.info("inside ves collector method"); + if(response.getStatusLine().getStatusCode()>=200 && response.getStatusLine().getStatusCode()<300){ + log.info("..........."); + if(alarmStatus == "ON" && eventType==anyEvent){ + log.info("alarm on ,event type is any and connection is 200 ok"); + log.info("ves send status"+list.get(0).vesSendStatus); + log.info("total entries in map: ",map.totalEntriesInMap()); + map.displayALLEntriesInMap(); + list.get(0).vesSendStatus = vesSendStatusSuccess; + map.updateMap(uuidKey, list); + }else if(alarmStatus == "ON" && eventType==batchEvent){ + log.info("alarm on ,event type is batch and connection is 200 ok"); + log.info("ves send status"+list.get(0).vesSendStatus); + log.info("total entries in map:",map.totalEntriesInMap()); + map.displayALLEntriesInMap(); + map.updateMapBatch("success"); + }else if(alarmStatus == "OFF" && (vesSendStatus=="new" || + vesSendStatus ==vesSendStatusSuccess) && eventType==anyEvent){ + log.info("alarm off and connection ok"); + map.deleteFromMap(uuidKey); + }else if(alarmStatus == "OFF" && (vesSendStatus=="new" || + vesSendStatus ==vesSendStatusSuccess) && eventType==batchEvent){ + log.info("alarm off and connection ok"); + map.deleteUsingAlarmCondition(alarmStatus); + } + if(timer.isTimerRunning()){ + timer.stopTimer(); + } + else{ + log.info("timer is not running...."); + } + return true; + }else{ + log.info("connection error !200"); + return false; + } + } + + public boolean connectionRefused(HttpResponse response){ + if(response.getStatusLine().getStatusCode()!=200){ + return true; + } + return false; + } } diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesAgent.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesAgent.java index e5ad34f..b5e5577 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesAgent.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesAgent.java @@ -27,53 +27,51 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.log4j.BasicConfigurator; public class VesAgent { - private static final Logger log = LoggerFactory.getLogger(VesAgent.class); - private static final String ERROR = "error"; - private VesAgent(){ - - } - public static void main (String[] args ) throws InterruptedException { - BasicConfigurator.configure(); - - if (args.length < 1 ) - { - log.info("Invalid Number of Arugments , provide the agent properties file path! " ); - return; - } - else - { - try { - /* in the VShpere Entity we are storing the VCentre IP Address - * , user name , - * password , vmname - */ - - int count = 0; - VsphereDataEntity vsphereDataEntity = new VsphereDataEntity(); - JsonStoreMap map = new JsonStoreMap(); - List<JsonAlarmStorage> list; - JSONParser parser = new JSONParser(); - Integer duration = 120000; - log.info("inside main method"); - String alarmCondition; // for checking alarm is raised or not - String vesSendStatus = "new"; //vesSendStatus - String filePath=args[0]; - /* VSphereData contains all method to connect , - * retrieve and - * fetch data using VShpere API - */ - VsphereData vsphereData = new VsphereData(); - VsphereEntity vsphereEntity = new VsphereEntity(); - VesEntity vesEntity = new VesEntity(); - ReadPropertiesFile file = new ReadPropertiesFile(); - if(file.readProperties(vsphereEntity,filePath) && getEnv(vsphereEntity, vesEntity)) - { - VESRestAPI vesRestAPI= new VESRestAPI(); - JSONObject eventObj = new JSONObject(); - JSONObject event = new JSONObject(); - JSONObject commonEventHeader = new JSONObject(); - JSONObject faultFields = new JSONObject(); - VesTimer timer = new VesTimer(map); + private static final Logger log = LoggerFactory.getLogger(VesAgent.class); + private static final String ERROR = "error"; + private static final Integer TIMERDURATION=360000; + private static final String GREEN="green"; + private VesAgent(){ + } + public static void main(String[] args) throws InterruptedException { + BasicConfigurator.configure(); + if (args.length < 1 ) + { + log.info("Invalid Number of Arugments , provide agent properties file path! " ); + return; + } + else + { + try { + /* in the VShpere Entity we are storing the VCentre IP Address + * , user name , + * password , vmname + */ + int count = 0; + VsphereDataEntity vsphereDataEntity = new VsphereDataEntity(); + JsonStoreMap map = new JsonStoreMap(); + List<JsonAlarmStorage> list; + JSONParser parser = new JSONParser(); + log.info("inside main method"); + String alarmCondition; // for checking alarm is raised or not + String vesSendStatus = "new"; //vesSendStatus + String filePath=args[0]; + /* VSphereData contains all method to connect , + * retrieve and + * fetch data using VShpere API + */ + VsphereData vsphereData = new VsphereData(); + VsphereEntity vsphereEntity = new VsphereEntity(); + VesEntity vesEntity = new VesEntity(); + ReadPropertiesFile file = new ReadPropertiesFile(); + if(file.readProperties(vsphereEntity,filePath) && getEnv(vsphereEntity, vesEntity)) + { + VESRestAPI vesRestAPI= new VESRestAPI(); + JSONObject eventObj = new JSONObject(); + JSONObject event = new JSONObject(); + JSONObject commonEventHeader = new JSONObject(); + JSONObject faultFields = new JSONObject(); + VesTimer timer = new VesTimer(map); int i=0; while(true) { @@ -85,77 +83,70 @@ public class VesAgent { Process p = rt.exec("python "+vsphereEntity.getPathVspherePython()+" -s "+vsphereEntity.getVsphereServerName()+" -u "+vsphereEntity.getVsphereUsername()+" -p "+vsphereEntity.getVsperePassword()); int result=p.waitFor(); if(result == 0){ - System.out.println("......done..json..."); + log.info("Excecuted python scipt. Json file created"); Thread.sleep(5000); - log.info("JSON File Path: " + vsphereEntity.getPythonOutputJson()); + log.info("JSON File Path: " + vsphereEntity.getPythonOutputJson()); /* Here we are opening the JSON File * and iterating and fetching the indivudal VM details and deciding the logic of raising the alarm on/off */ Object obj = parser.parse(new FileReader(vsphereEntity.getPythonOutputJson())); JSONArray jsonObject = (JSONArray) obj; - log.info(jsonObject.toJSONString()); - log.info("inside while loop"); + log.info(jsonObject.toString()); + log.info("Iterating Json files-vms"); for(int j = 0;j<=jsonObject.size()-1;j++){ - log.info("timer is running or not: "+timer.isTimerRunning()); - log.info("Is timed out happn: "+timer.isTimeout()); + log.info("Is timer running: ",timer.isTimerRunning()); + log.info("Is timed out happen: ",timer.isTimeout()); JSONObject js = (JSONObject) jsonObject.get(j); - log.info(""); - log.info(js.toJSONString()); count++; log.info("////////////////////////////////////////////////////////////////////////////"); - log.info("map entries in total"+map.totalEntriesInMap()); - log.info("Count:"+count); + log.info("Total entries in map: ",map.totalEntriesInMap()); + log.info("Count for checking vms :",count); VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); String heartBeatStatus = vsphereDataEntity.getStatus(); - log.info(heartBeatStatus); - - String uuid_key = vsphereDataEntity.getSourceId(); - if(heartBeatStatus!="green"){ + String uuidKey = vsphereDataEntity.getSourceId(); + log.info("Heart beat status of vm",heartBeatStatus); + if(heartBeatStatus!=GREEN){ //encode json - if(map.isJsonFound(uuid_key) && map.retrieveAlarmConditionFromMap(uuid_key) == "ON"){ - log.info("alarm ON already raised"); + if(map.isJsonFound(uuidKey) && map.retrieveFromMap(uuidKey, "ALARM") == "ON"){ + log.info("Alarm ON already raised..."); } - else if(!map.isJsonFound(uuid_key)){ + else if(!map.isJsonFound(uuidKey)){ alarmCondition = "ON"; vsphereData.encodeJson(entity, event, eventObj, commonEventHeader, faultFields,map); - list = vsphereData.listJsonAlarm2(event, alarmCondition,vesSendStatus); - log.info(list.get(0).json+" :"+list.get(0).alarm+" :"+list.get(0).vesSendStatus); - map.addToMap(uuid_key, list); + list = vsphereData.listJsonAlarm(event, alarmCondition,vesSendStatus); + log.info("json, alarm and vesSend status: "+list.get(0).json.toString()+" :"+list.get(0).alarm+" :"+list.get(0).vesSendStatus); + map.addToMap(uuidKey, list); if(map.totalEntriesInMap()>1) { JSONObject alarmJsonConstructArray = map.retrieveALLFromMapBatch(); - vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, uuid_key, list, duration , vesSendStatus); + vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, uuidKey, list, TIMERDURATION,vesSendStatus); } else { - //map.addToMap(uuid_key, list); - //vesRestAPI.publishAnyEventToVES(vesEntity, event,null,map, timer); - vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuid_key, list,duration,vesSendStatus); - log.info("published "); - + vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuidKey, list,TIMERDURATION,vesSendStatus); + log.info("published any event "); } } } - else if(heartBeatStatus == "green"){ - /* if the alarm on entry is found in Map then check for the vesSendStatus + else if(heartBeatStatus == GREEN){ /* if the alarm on entry is found in Map then check for the vesSendStatus * IF the VesSendStatus ==failed for Alarm On * in the case remove the entries from MAP and * donot encode the alarm off and donot send it to VESCollector */ - if((map.isJsonFound(uuid_key) && map.retrieveAlarmConditionFromMap(uuid_key) == "ON") && map.retrieveVesSendStatusFromMap(uuid_key)!="failed"){ + if((map.isJsonFound(uuidKey) && map.retrieveFromMap(uuidKey, "ALARM") == "ON") && map.retrieveFromMap(uuidKey,"VES_STATUS")!="failed"){ + log.info("heart beat status is green, uuid found in map "); alarmCondition = "OFF"; vsphereData.encodeJson(entity, event, eventObj, commonEventHeader, faultFields,map); - list = vsphereData.listJsonAlarm2(event, alarmCondition,vesSendStatus); + list = vsphereData.listJsonAlarm(event, alarmCondition,vesSendStatus); String json = event.toJSONString(); - log.info(json); - map.addToMap(uuid_key, list); + map.addToMap(uuidKey, list); if(map.totalEntriesInMap()>1){ //sending only entry having vesSendStatus = failed JSONObject alarmJsonConstructArray = map.retrieveALLFromMapBatch(); - vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, json, list, duration,vesSendStatus); + vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, json, list, TIMERDURATION,vesSendStatus); } else{ - vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuid_key,list,duration,vesSendStatus); + vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuidKey,list,TIMERDURATION,vesSendStatus); } } else{ @@ -163,13 +154,10 @@ public class VesAgent { } } Thread.sleep(2000); - log.info("for loop end..."); + log.info("End of iteration - for loop - vm"); } - log.info(String.valueOf(count)); - }else if(result!=0){ - log.info("Something went wrong on fetching info using python script!! Check the python script and credentials"); - break; + log.info("Process terminated! python execution failed"); } i++; if (i == Integer.MIN_VALUE) { // true at Integer.MAX_VALUE +1 @@ -177,50 +165,58 @@ public class VesAgent { } } }else{ - log.info("Missing values for python files - Env Variabl es "); - - } - } catch(java.io.FileNotFoundException e){ + log.info("Error in Vsphere Credentials/python file path/json file path/- Set Env Variables properly "); + } + } catch(java.io.FileNotFoundException e){ log.info("File not found exception. Check the json file path"); - log.error(ERROR,e); + log.error(ERROR,e); } catch (IOException e) { - log.info("Something went wrong with python in runtime"); - log.error(ERROR,e); + log.info("Something went wrong with python script - runtime"); + log.error(ERROR,e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - log.info("Interupted exception occured. Python script execution taking more time"); - log.error(ERROR,e); - } catch (ParseException e) { - log.info("Something went wrong with json created by python! Parse exception occured"); - log.error(ERROR,e); + log.info("Interupted exception occured. Python script execution taking more time"); + log.error(ERROR,e); + + } catch (ParseException e) { + log.info("Something went wrong with json created by python script! Parse exception occured"); + log.error(ERROR,e); }catch (Exception e) { - log.info("Exception Occured!!"); - log.error(ERROR,e); + log.info("Exception occured!!"); + log.error(ERROR,e); + } } } public static String getDateTime(){ DateFormat df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale ("en", "EN")); - return df.format (new Date ()); - } -public static boolean getEnv(VsphereEntity vsphereEntity, VesEntity vesEntity){ - try{ - if(System.getenv("Vsphere_Ip").isEmpty() || System.getenv("Vsphere_Username").isEmpty() || System.getenv("Vsphere_Password")==null || System.getenv("VesCollector_Ip").isEmpty() || System.getenv("VesCollector_Port").isEmpty()){ - log.info("Null values"); - return false; - } - else{ - vsphereEntity.setVsphereServerName(System.getenv("Vsphere_Ip")); - vsphereEntity.setVsphereUsername(System.getenv("Vsphere_Username")); - vsphereEntity.setVsperePassword(System.getenv("Vsphere_Password")); - vesEntity.setVesip(System.getenv("VesCollector_Ip")); - vesEntity.setVesPassword(System.getenv("VesCollector_Port")); - return true; + return df.format(new Date ()); + } + - } - }catch(NullPointerException e){ - log.error("Missing values - env var",e); - } - return false; - } + public static boolean getEnv(VsphereEntity vsphereEntity, VesEntity vesEntity){ + try{ + if(System.getenv("Vsphere_Ip").isEmpty() || System.getenv("Vsphere_Username").isEmpty() || System.getenv("Vsphere_Password").isEmpty()){ + if(System.getenv("VesCollector_Ip").isEmpty() || System.getenv("VesCollector_Port").isEmpty()){ + log.info("Null values"); + return false; + } + } + else{ + if(System.getenv("Vsphere_Password").length()==0 || System.getenv("Vsphere_Ip").length()==0 || System.getenv("Vsphere_Username").length() == 0){ + log.info("Vsphere credentials empty. Set env Var ..."); + return false; + } + vsphereEntity.setVsphereServerName(System.getenv("Vsphere_Ip")); + vsphereEntity.setVsphereUsername(System.getenv("Vsphere_Username")); + vsphereEntity.setVsperePassword(System.getenv("Vsphere_Password")); + vesEntity.setVesip(System.getenv("VesCollector_Ip")); + vesEntity.setVesPassword(System.getenv("VesCollector_Port")); + return true; + } + }catch(NullPointerException e){ + log.error("Missing values -Set env var",e); + } + return false; + } } diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesTimer.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesTimer.java index acdca74..8dddcc7 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesTimer.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VesTimer.java @@ -18,7 +18,6 @@ import java.util.TimerTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - class TimerExtend extends Timer{ public TimerExtend(){ @@ -31,25 +30,23 @@ class TimerExtend extends Timer{ } class TaskTimeout extends TimerTask{ - private static JsonStoreMap map; - private final Logger log = LoggerFactory.getLogger(TaskTimeout.class); - @SuppressWarnings("static-access") - public TaskTimeout(JsonStoreMap map) { - TaskTimeout.map = map; - } - - public static void run1(){ - VesTimer.timeoutCheck = true; - map.deleteAllFromMap(); - VesTimer.hasStarted=false; + private static JsonStoreMap map; + private final Logger log = LoggerFactory.getLogger(TaskTimeout.class); + @SuppressWarnings("static-access") + public TaskTimeout(JsonStoreMap map) { + TaskTimeout.map = map; + } + public static void run1(){ + VesTimer.timeoutCheck = true; + map.deleteAllFromMap(); + VesTimer.hasStarted=false; + } + @SuppressWarnings("resource") + @Override + public void run() { + run1(); + log.info("In Timed out run method, before deleting the entries from Map"); } - @SuppressWarnings("resource") - @Override - public void run() { - run1(); - log.info("In Time out before deleting the entries from Map"); - - } } public class VesTimer { @@ -60,8 +57,8 @@ public class VesTimer { private JsonStoreMap map; private final Logger log = LoggerFactory.getLogger(VesTimer.class); public VesTimer(JsonStoreMap map){ - this.map = map; - } + this.map = map; + } public void startTimer(Integer duration){ tt = new TimerExtend(); @@ -76,7 +73,7 @@ public class VesTimer { } public boolean isTimerRunning(){ - if(VesTimer.hasStarted == true){ + if(VesTimer.hasStarted){ log.info("timer started...."); return true; } diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereData.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereData.java index 0c56c2d..ddb963e 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereData.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereData.java @@ -18,20 +18,19 @@ import java.util.List; import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.json.simple.JSONObject; public class VsphereData { - private final Logger log = LoggerFactory.getLogger(VsphereData.class); + private final Logger log = LoggerFactory.getLogger(VsphereData.class); public VsphereDataEntity gettingVMInfo(JSONObject js,VsphereDataEntity vsphereDataEntity,VsphereEntity vsphereEntity){ String vsphereVMname = (String) js.get("Name"); vsphereEntity.setVsphereVMname(vsphereVMname); vsphereDataEntity.setSourceName(vsphereVMname); String status = (String) js.get("Heartbeatstatus"); - vsphereDataEntity.setStatus(status.toString()); + vsphereDataEntity.setStatus(status); - String instance_uuid=(String) js.get("Instance UUID"); - vsphereDataEntity.setSourceId(instance_uuid); + String instanceUuid=(String) js.get("Instance UUID"); + vsphereDataEntity.setSourceId(instanceUuid); String sourceType="VirtualMachine"; vsphereDataEntity.setSourceType(sourceType); @@ -54,23 +53,20 @@ public class VsphereData { UUID uuid = UUID.randomUUID(); - - //String eventId = "ab305d54-85b4-a31b-7db2-fb6b9e546015"; String sourceName=vsphereDataEntity.getSourceName(); String reportingEntityName = "Multi-Cloud"; Integer sequence=0; - //String eventType="GuestOS"; - String eventName=""; - String priority=""; - String eventSeverity=""; - String alarmCondition=""; - String vfStatus=""; - String specificProblem=""; + String eventName; + String priority; + String eventSeverity; + String alarmCondition; + String vfStatus; + String specificProblem; long unixTime = (System.currentTimeMillis() / 1000L)*1000000 ; - Long startEpochMicrosec = null; - Long lastEpochMicrosec = null; - String eventId=""; - String sourceId =vsphereDataEntity.getSourceId().toString(); + Long startEpochMicrosec; + Long lastEpochMicrosec; + String eventId; + String sourceId =vsphereDataEntity.getSourceId(); Double faultFieldsVersion=2.0; if (vsphereDataEntity.getStatus() !="green") { eventName="Fault_MultiCloud_VMFailure"; @@ -105,9 +101,7 @@ public class VsphereData { for alarm on case both will be the same value but for alarm off case the lastEpochMicroSec will be current System/Date/Time */ - //here we have to create jsonobjects - //JSONObject commonEventHeader = new JSONObject(); commonEventHeader.put("version", version); commonEventHeader.put("domain",domain); commonEventHeader.put("eventName",eventName); @@ -120,7 +114,6 @@ public class VsphereData { commonEventHeader.put("startEpochMicrosec",startEpochMicrosec); commonEventHeader.put("lastEpochMicrosec",lastEpochMicrosec); - //JSONObject faultFields = new JSONObject(); faultFields.put("faultFieldsVersion",faultFieldsVersion ); faultFields.put("eventSeverity",eventSeverity ); faultFields.put("alarmCondition",alarmCondition ); @@ -129,26 +122,17 @@ public class VsphereData { faultFields.put("alarmInterfaceA", "aaaa"); faultFields.put("eventSourceType", "other"); - //JSONObject eventObj = new JSONObject(); eventObj.put("commonEventHeader", commonEventHeader); eventObj.put("faultFields",faultFields); - //JSONObject event = new JSONObject(); event.put("event", eventObj); } - public List<String> listJsonAlarm(String Json,String alarmCondition){ - System.out.println("adding to list- json and alarm"); - List<String> list = new ArrayList<String>(); - list.add(Json); - list.add(alarmCondition); - return list; - } - public List<JsonAlarmStorage> listJsonAlarm2(JSONObject json,String alarmCondition, String vesSendStatus){ + public List<JsonAlarmStorage> listJsonAlarm(JSONObject json,String alarmCondition, String vesSendStatus){ log.info("adding to list- json and alarm"); JsonAlarmStorage store = new JsonAlarmStorage(); - List<JsonAlarmStorage> list = new ArrayList<JsonAlarmStorage>(); + List<JsonAlarmStorage> list = new ArrayList<>(); store.json = json; store.alarm=alarmCondition; store.vesSendStatus = vesSendStatus; diff --git a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereEntity.java b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereEntity.java index 809fd78..6aec4c0 100644 --- a/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereEntity.java +++ b/vesagent/src/main/java/org/onap/multicloud/openstack/vmware/VsphereEntity.java @@ -14,46 +14,47 @@ package org.onap.multicloud.openstack.vmware; public class VsphereEntity { - private String VsphereServerName; - private String VsphereUsername; - private String VsperePassword; - private String VsphereVMname; - private String PathVspherePython; - private String PythonOutputJson; - - public String getVsphereUsername() { - return VsphereUsername; - } - - public void setVsphereUsername(String vsphereUsername) { - this.VsphereUsername = vsphereUsername; - } - - public String getPythonOutputJson() { - return PythonOutputJson; + private String vsphereServerName; + private String vsphereUsername; + private String vsperePassword; + private String vsphereVMname; + private String pathVspherePython; + private String pythonOutputJson; + + public String getVsphereServerName() { + return vsphereServerName; } - public void setPythonOutputJson(String pythonOutputJson) { - PythonOutputJson = pythonOutputJson; + public void setVsphereServerName(String vsphereServerName) { + this.vsphereServerName = vsphereServerName; } - public String getPathVspherePython() { - return PathVspherePython; + public String getVsphereUsername() { + return vsphereUsername; } - public void setPathVspherePython(String pathVspherePython) { - PathVspherePython = pathVspherePython; + public void setVsphereUsername(String vsphereUsername) { + this.vsphereUsername = vsphereUsername; } public String getVsperePassword() { - return VsperePassword; + return vsperePassword; } public void setVsperePassword(String vsperePassword) { - VsperePassword = vsperePassword; + this.vsperePassword = vsperePassword; + } + public String getVsphereVMname() { + return vsphereVMname; } public void setVsphereVMname(String vsphereVMname) { - VsphereVMname = vsphereVMname; + this.vsphereVMname = vsphereVMname; } - public String getVsphereServerName() { - return VsphereServerName; + public String getPathVspherePython() { + return pathVspherePython; } - public void setVsphereServerName(String vsphereServerName) { - VsphereServerName = vsphereServerName; + public void setPathVspherePython(String pathVspherePython) { + this.pathVspherePython = pathVspherePython; + } + public String getPythonOutputJson() { + return pythonOutputJson; + } + public void setPythonOutputJson(String pythonOutputJson) { + this.pythonOutputJson = pythonOutputJson; } } diff --git a/vesagent/src/test/java/org/onap/multicloud/openstack/vmware/VesAgentTest.java b/vesagent/src/test/java/org/onap/multicloud/openstack/vmware/VesAgentTest.java index b73958a..60d82c0 100644 --- a/vesagent/src/test/java/org/onap/multicloud/openstack/vmware/VesAgentTest.java +++ b/vesagent/src/test/java/org/onap/multicloud/openstack/vmware/VesAgentTest.java @@ -183,23 +183,19 @@ public class VesAgent { */ public class VesAgentTest { - static VsphereDataEntity vsphereDataEntity = new VsphereDataEntity(); static JsonStoreMap map = new JsonStoreMap(); static List<JsonAlarmStorage> list; public static String output; String alarmCondition = "OFF"; // for checking alarm is raised or not String vesSendStatus = "new"; //vesSendStatus - String filepath="src/test/resources/agent.properties"; public Integer duration=120000; VsphereData vsphereData = new VsphereData(); VsphereEntity vsphereEntity = new VsphereEntity(); VesEntity vesEntity = new VesEntity(); ReadPropertiesFile file = new ReadPropertiesFile(); - // file.readProperties(vsphereEntity, vesEntity, filePath); - VESRestAPI vesRestAPI= new VESRestAPI(); JSONObject eventObj = new JSONObject(); JSONObject event = new JSONObject(); @@ -220,9 +216,7 @@ public JSONArray getFileAsJsonObject() Object obj = parser.parse (fr); jsonArrayObj = (JSONArray) obj; System.out.println(jsonArrayObj.toJSONString()); - } - catch(Exception e){ System.out.println("Exception while opening the file"); e.printStackTrace(); @@ -231,119 +225,94 @@ System.out.println("Exception while opening the file"); //close the file if (fr != null) { try { - fr.close(); - } catch (IOException e) - { - System.out.println("Error closing file reader stream : " +e.toString()); - } - } - } - return jsonArrayObj; + fr.close(); + } catch (IOException e){ + System.out.println("Error closing file reader stream : " +e.toString()); + } + } + } + return jsonArrayObj; } @Test //public void testSingleHeartBeat() throws ClientProtocolException, IOException, ParseException public void testSingleHeartBeat() throws Exception{ - JSONArray jsonArrayObject = getFileAsJsonObject(); String output = "true"; - for(int j = 0;j<=jsonArrayObject.size()-1;j++){ - JSONObject js = (JSONObject) jsonArrayObject.get(j); System.out.println(""); System.out.println(js.toJSONString()); - - VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); - String heartBeatStatus = vsphereDataEntity.getStatus(); - System.out.println(heartBeatStatus); - String uuid_key = vsphereDataEntity.getSourceId(); - vsphereData.encodeJson(entity, event, eventObj, commonEventHeader, faultFields,map); - list = vsphereData.listJsonAlarm2(event, alarmCondition,vesSendStatus); - map.addToMap(uuid_key, list); - - vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuid_key, list,duration,vesSendStatus); - - output="true"; - } - assertEquals ("true",output); - -} - + VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); + String heartBeatStatus = vsphereDataEntity.getStatus(); + System.out.println(heartBeatStatus); + String uuid_key = vsphereDataEntity.getSourceId(); + vsphereData.encodeJson(entity, event, eventObj, commonEventHeader, faultFields,map); + list = vsphereData.listJsonAlarm(event, alarmCondition,vesSendStatus); + map.addToMap(uuid_key, list); + vesRestAPI.publishAnyEventToVES(vesEntity,map, timer,uuid_key, list,duration,vesSendStatus); + output="true"; + } + assertEquals ("true",output); + } @Test - //public void testmultipleHeartbeat() throws FileNotFoundException, IOException, ParseException + //public void testmultipleHeartbeat() throws FileNotFoundException, IOException, ParseException public void testmultipleHeartbeat() throws Exception{ - - String outout = "true"; JSONArray jsonArrayObject = getFileAsJsonObject(); - for(int j = 0;j<=jsonArrayObject.size()-1;j++){ - JSONObject js = (JSONObject) jsonArrayObject.get(j); System.out.println(""); System.out.println(js.toJSONString()); - VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); String heartBeatStatus = vsphereDataEntity.getStatus(); System.out.println(heartBeatStatus); String uuid_key = vsphereDataEntity.getSourceId(); - vsphereData.encodeJson(entity, event, eventObj, commonEventHeader, faultFields,map); - list = vsphereData.listJsonAlarm2(event, alarmCondition,vesSendStatus); + list = vsphereData.listJsonAlarm(event, alarmCondition,vesSendStatus); map.addToMap(uuid_key, list); map.updateMap(uuid_key, list); - map.updateMapBatch("failed"); - - JSONObject alarmJsonConstructArray = map.retrieveALLFromMapBatch(); - vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, uuid_key, list, duration , vesSendStatus); - output ="true"; - } - assertEquals ("true",output); - } + JSONObject alarmJsonConstructArray = map.retrieveALLFromMapBatch(); + vesRestAPI.publishBatchEventToVES(vesEntity, alarmJsonConstructArray, map, timer, uuid_key, list, duration,vesSendStatus); + output = "true"; + } + assertEquals ("true",output); + } @Test //public void testmultipleHeartbeat() throws FileNotFoundException, IOException, ParseException public void testAlarmMapEntries() throws Exception{ - - - String outout = "true"; - file.readProperties(vsphereEntity, filepath); + String outout = "true"; + file.readProperties(vsphereEntity,filepath); JSONArray jsonArrayObject = getFileAsJsonObject(); - - for(int j = 0;j<=jsonArrayObject.size()-1;j++){ - - JSONObject js = (JSONObject) jsonArrayObject.get(j); - System.out.println(""); - System.out.println(js.toJSONString()); - - VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); - String heartBeatStatus = vsphereDataEntity.getStatus(); - System.out.println(heartBeatStatus); - String uuid_key = vsphereDataEntity.getSourceId(); - - map.isJsonFound(uuid_key); - map.retrieveAlarmConditionFromMap(uuid_key); - map.retrieveALLFromMapBatch(); - map.retrieveJsonFromMap(uuid_key); - map.retrieveVesSendStatusFromMap(uuid_key); - map.displayAllEntriesInMap(); - map.deleteFromMap(uuid_key); - map.deleteAllFromMap(); - map.deleteUsingAlarmCondition("AlarmOn"); - map.totalEntriesInMap(); - timer.isTimerRunning(); - timer.startTimer(5000); - timer.stopTimer(); - timer.isTimeout(); - output ="true"; - } - assertEquals ("true",output); - } - + for(int j = 0;j<=jsonArrayObject.size()-1;j++){ + JSONObject js = (JSONObject) jsonArrayObject.get(j); + System.out.println(""); + System.out.println(js.toJSONString()); + VsphereDataEntity entity = vsphereData.gettingVMInfo(js,vsphereDataEntity,vsphereEntity); + String heartBeatStatus = vsphereDataEntity.getStatus(); + System.out.println(heartBeatStatus); + String uuid_key = vsphereDataEntity.getSourceId(); + map.isJsonFound(uuid_key); + map.retrieveFromMap(uuid_key,"ALARM"); + map.retrieveALLFromMapBatch(); + map.retrieveJsonFromMap(uuid_key); + map.retrieveFromMap(uuid_key,"VES_STATUS"); + map.displayALLEntriesInMap(); + map.deleteFromMap(uuid_key); + map.deleteAllFromMap(); + map.deleteUsingAlarmCondition("AlarmOn"); + map.totalEntriesInMap(); + timer.isTimerRunning(); + timer.startTimer(5000); + timer.stopTimer(); + timer.isTimeout(); + output ="true"; + } + assertEquals ("true",output); + } } - |