blob: d043832c7a8249e4892c446a4000a8c989aa262e [file] [log] [blame]
William Kurkianbde6fc92018-07-13 17:19:58 -04001/*
William Kurkian1bedb412018-07-19 12:55:41 -04002* 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 Kurkianbde6fc92018-07-13 17:19:58 -040016package ves;
17
William Kurkianbde6fc92018-07-13 17:19:58 -040018import java.net.HttpURLConnection;
19
William Kurkianbde6fc92018-07-13 17:19:58 -040020import config.Config;
21
William Kurkian1bedb412018-07-19 12:55:41 -040022import mapper.VesVolthaMapper;
William Kurkian18ec3442018-09-10 16:27:37 -040023import mapper.VesVolthaAlarm;
24import mapper.VesVolthaKpi;
25import kafka.KafkaConsumerType;
William Kurkian1bedb412018-07-19 12:55:41 -040026
William Kurkianbde6fc92018-07-13 17:19:58 -040027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
William Kurkian1bedb412018-07-19 12:55:41 -040029import com.google.gson.JsonSyntaxException;
William Kurkianbde6fc92018-07-13 17:19:58 -040030
William Kurkian9600b5c2018-09-20 16:05:59 -040031import java.util.List;
32import java.util.ArrayList;
33
William Kurkianbde6fc92018-07-13 17:19:58 -040034public class VesAgent {
35
William Kurkian1bedb412018-07-19 12:55:41 -040036 private static final Logger logger = LoggerFactory.getLogger("VesAgent");
William Kurkianbde6fc92018-07-13 17:19:58 -040037
William Kurkian9600b5c2018-09-20 16:05:59 -040038 private VesVolthaMapper mapper;
William Kurkian1bedb412018-07-19 12:55:41 -040039
William Kurkian9600b5c2018-09-20 16:05:59 -040040 private VesDispatcher dispatcher;
41
42 public VesAgent() {
William Kurkian1bedb412018-07-19 12:55:41 -040043 logger.info("Initializing VES Agent");
44 try {
45 mapper = new VesVolthaMapper();
William Kurkian9600b5c2018-09-20 16:05:59 -040046 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 Kurkian1bedb412018-07-19 12:55:41 -040051 }
William Kurkianbde6fc92018-07-13 17:19:58 -040052 }
William Kurkianbde6fc92018-07-13 17:19:58 -040053
William Kurkian9600b5c2018-09-20 16:05:59 -040054 public boolean sendToVES(KafkaConsumerType type, String json) throws JsonSyntaxException {
William Kurkian18ec3442018-09-10 16:27:37 -040055 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 Kurkian9600b5c2018-09-20 16:05:59 -040073 private int sendFault(String json) {
William Kurkian18ec3442018-09-10 16:27:37 -040074 VesVolthaAlarm message = mapper.parseAlarm(json);
75
William Kurkian1bedb412018-07-19 12:55:41 -040076 String id = message.getId();
William Kurkian18ec3442018-09-10 16:27:37 -040077 String[] idsplit = id.split("\\.");
78 String eventType = idsplit[idsplit.length-1];
William Kurkian1bedb412018-07-19 12:55:41 -040079 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 Kurkian7180b942018-08-15 15:26:17 -040089
William Kurkian9600b5c2018-09-20 16:05:59 -040090 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 Kurkianbde6fc92018-07-13 17:19:58 -0400105
William Kurkian1bedb412018-07-19 12:55:41 -0400106 logger.info("Sending fault event");
William Kurkian9600b5c2018-09-20 16:05:59 -0400107 List<VesBlock> blocks = new ArrayList<>();
108 blocks.add(header);
109 blocks.add(flt);
110 int code = dispatcher.sendEvent(blocks);
William Kurkian1bedb412018-07-19 12:55:41 -0400111 logger.info("Fault event http code received: " + code);
William Kurkian18ec3442018-09-10 16:27:37 -0400112 return code;
113 }
114
William Kurkian9600b5c2018-09-20 16:05:59 -0400115 private int sendKpi(String json) {
William Kurkian18ec3442018-09-10 16:27:37 -0400116 VesVolthaKpi message = mapper.parseKpi(json);
117
William Kurkian9600b5c2018-09-20 16:05:59 -0400118 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 Kurkian18ec3442018-09-10 16:27:37 -0400127
William Kurkian9600b5c2018-09-20 16:05:59 -0400128 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 Kurkian18ec3442018-09-10 16:27:37 -0400133 return code;
William Kurkian1bedb412018-07-19 12:55:41 -0400134 }
William Kurkianbde6fc92018-07-13 17:19:58 -0400135}