William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 1 | /* |
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 2 | * Copyright 2018- Cisco |
| 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 | */ |
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 16 | package ves;
|
| 17 |
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 18 | import java.net.HttpURLConnection;
|
| 19 |
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 20 | import config.Config;
|
| 21 |
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 22 | import mapper.VesVolthaMapper;
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 23 | import mapper.VesVolthaAlarm;
|
| 24 | import mapper.VesVolthaKpi;
|
| 25 | import kafka.KafkaConsumerType;
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 26 |
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 27 | import org.slf4j.Logger;
|
| 28 | import org.slf4j.LoggerFactory;
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 29 | import com.google.gson.JsonSyntaxException;
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 30 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 31 | import java.util.List;
|
| 32 | import java.util.ArrayList;
|
| 33 |
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 34 | public class VesAgent {
|
| 35 |
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 36 | private static final Logger logger = LoggerFactory.getLogger("VesAgent");
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 37 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 38 | private VesVolthaMapper mapper;
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 39 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 40 | private VesDispatcher dispatcher;
|
| 41 |
|
| 42 | public VesAgent() {
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 43 | logger.info("Initializing VES Agent");
|
| 44 | try {
|
| 45 | mapper = new VesVolthaMapper();
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 46 | dispatcher = new VesDispatcher("http://"+Config.getVesAddress(),
|
| 47 | Config.getVesPort());
|
| 48 | } catch(Exception e) {
|
| 49 | logger.error("Failed to initialize VES", e);
|
| 50 | logger.error(e.toString());
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 51 | }
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 52 | }
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 53 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 54 | public boolean sendToVES(KafkaConsumerType type, String json) throws JsonSyntaxException {
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 55 | int code = 0;
|
| 56 |
|
| 57 | switch (type) {
|
| 58 | case ALARMS:
|
| 59 | code = sendFault(json);
|
| 60 | break;
|
| 61 | case KPIS:
|
| 62 | code = sendKpi(json);
|
| 63 | break;
|
| 64 | }
|
| 65 |
|
| 66 | if(code == 0 || code >= HttpURLConnection.HTTP_BAD_REQUEST ) {
|
| 67 | return false;
|
| 68 | } else {
|
| 69 | return true;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 73 | private int sendFault(String json) {
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 74 | VesVolthaAlarm message = mapper.parseAlarm(json);
|
| 75 |
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 76 | String id = message.getId();
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 77 | String[] idsplit = id.split("\\.");
|
| 78 | String eventType = idsplit[idsplit.length-1];
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 79 | String ldeviceId = message.getLogicalDeviceId();
|
| 80 | String ts = message.getRaisedTS();
|
| 81 | String description = message.getDescription();
|
| 82 | //Type in Voltha needs to be category in VES
|
| 83 | String category = message.getType();
|
| 84 | //Category in VOLTHA needs to be type in VES
|
| 85 | String type = message.getCategory();
|
| 86 | String severity = message.getSeverity();
|
| 87 | String state = message.getState();
|
| 88 | String resourceId = message.getResourceId();
|
William Kurkian | 7180b94 | 2018-08-15 15:26:17 -0400 | [diff] [blame] | 89 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 90 | EventHeader header = new EventHeader("fault", ldeviceId + ":" + ts,
|
| 91 | "Fault_VOLTHA_" + eventType);
|
| 92 | EventFault flt = new EventFault(
|
| 93 | id, //alarm conidition
|
| 94 | category, //eventCategory
|
| 95 | severity, //event severity
|
| 96 | type, //source type
|
| 97 | description, //specificProblem
|
| 98 | "Active" //getVfStatus
|
| 99 | );
|
| 100 | flt.addAdditionalValues("voltha", json);
|
| 101 | flt.addAdditionalValues("state", state);
|
| 102 | flt.addAdditionalValues("co_id", Config.getCoId());
|
| 103 | flt.addAdditionalValues("pod_id", Config.getPodId());
|
| 104 | flt.addAdditionalValues("resourceId", resourceId);
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 105 |
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 106 | logger.info("Sending fault event");
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 107 | List<VesBlock> blocks = new ArrayList<>();
|
| 108 | blocks.add(header);
|
| 109 | blocks.add(flt);
|
| 110 | int code = dispatcher.sendEvent(blocks);
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 111 | logger.info("Fault event http code received: " + code);
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 112 | return code;
|
| 113 | }
|
| 114 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 115 | private int sendKpi(String json) {
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 116 | VesVolthaKpi message = mapper.parseKpi(json);
|
| 117 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 118 | EventHeader header = new EventHeader("other", System.currentTimeMillis() + ":" + message.getTs(),
|
| 119 | "other_VOLTHA_KPI");
|
| 120 | EventKpi ev = new EventKpi();
|
| 121 | ev.addAdditionalValues("voltha", json);
|
| 122 | ev.addAdditionalValues("slices", message.getSliceData());
|
| 123 | ev.addAdditionalValues("co_id", Config.getCoId());
|
| 124 | ev.addAdditionalValues("pod_id", Config.getPodId());
|
| 125 | ev.addAdditionalValues("type", message.getType());
|
| 126 | ev.addAdditionalValues("ts", message.getTs());
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 127 |
|
William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame] | 128 | logger.info("Sending KPI event");
|
| 129 | List<VesBlock> blocks = new ArrayList<>();
|
| 130 | blocks.add(header);
|
| 131 | blocks.add(ev);
|
| 132 | int code = dispatcher.sendEvent(blocks);logger.info("KPI event http code received: " + code);
|
William Kurkian | 18ec344 | 2018-09-10 16:27:37 -0400 | [diff] [blame] | 133 | return code;
|
William Kurkian | 1bedb41 | 2018-07-19 12:55:41 -0400 | [diff] [blame] | 134 | }
|
William Kurkian | bde6fc9 | 2018-07-13 17:19:58 -0400 | [diff] [blame] | 135 | }
|