William Kurkian | 9600b5c | 2018-09-20 16:05:59 -0400 | [diff] [blame^] | 1 | /*
|
| 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 | */
|
| 16 | package ves;
|
| 17 |
|
| 18 | import java.util.List;
|
| 19 | import java.util.Map;
|
| 20 | import java.net.InetAddress;
|
| 21 | import java.net.UnknownHostException;
|
| 22 | import java.net.SocketException;
|
| 23 | import java.util.Enumeration;
|
| 24 | import java.net.NetworkInterface;
|
| 25 | import org.slf4j.Logger;
|
| 26 | import org.slf4j.LoggerFactory;
|
| 27 |
|
| 28 | public class EventHeader implements VesBlock {
|
| 29 | private transient static String hostname = "";
|
| 30 | private transient static String uuid = "";
|
| 31 |
|
| 32 | private transient final Logger logger = LoggerFactory.getLogger("EventHeader");
|
| 33 |
|
| 34 | private String domain;
|
| 35 | private String eventId;
|
| 36 | private String eventName;
|
| 37 | private long lastEpochMicrosec;
|
| 38 | private String priority;
|
| 39 | private String reportingEntityId;
|
| 40 | private String reportingEntityName;
|
| 41 | private int sequence;
|
| 42 | private String sourceId;
|
| 43 | private String sourceName;
|
| 44 | private long startEpochMicrosec = 0;
|
| 45 | private int version = 3;
|
| 46 | |
| 47 | public EventHeader(String domain, String eventId, String eventName) { |
| 48 | this.domain = domain; |
| 49 | this.eventId = eventId; |
| 50 | this.eventName = eventName; |
| 51 | this.priority = "High"; |
| 52 | this.sequence = 1; |
| 53 | //microseconds are not supported in java 8. So just approximating it. |
| 54 | this.lastEpochMicrosec = (long)(System.nanoTime()/1000.0); |
| 55 | |
| 56 | setIdAndName(); |
| 57 | } |
| 58 | |
| 59 | private void setIdAndName() { |
| 60 | if (!EventHeader.hostname.equals("") && !EventHeader.uuid.equals("")) { |
| 61 | this.reportingEntityId = uuid; |
| 62 | this.sourceId = uuid; |
| 63 | this.reportingEntityName = hostname; |
| 64 | this.sourceName = hostname; |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | InetAddress addr; |
| 70 | addr = InetAddress.getLocalHost(); |
| 71 | EventHeader.hostname = addr.getHostName(); |
| 72 | } |
| 73 | catch (UnknownHostException ex) |
| 74 | { |
| 75 | System.out.println("Hostname can not be resolved"); |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | Enumeration<NetworkInterface> networks = |
| 80 | NetworkInterface.getNetworkInterfaces(); |
| 81 | while(networks.hasMoreElements()) { |
| 82 | NetworkInterface network = networks.nextElement(); |
| 83 | byte[] mac = network.getHardwareAddress(); |
| 84 | |
| 85 | if(hostname.equalsIgnoreCase("")) { |
| 86 | Enumeration inetAddrs = network.getInetAddresses(); |
| 87 | while(inetAddrs.hasMoreElements()){ |
| 88 | InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); |
| 89 | if (!inetAddr.isLoopbackAddress()) { |
| 90 | EventHeader.hostname = inetAddr.getHostAddress(); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (mac != null) { |
| 97 | EventHeader.uuid = bytesToHex(mac); |
| 98 | } |
| 99 | }
|
| 100 | } catch (SocketException e) {
|
| 101 | logger.error(e.toString());
|
| 102 | } |
| 103 | |
| 104 | this.reportingEntityId = uuid; |
| 105 | this.sourceId = uuid; |
| 106 | this.reportingEntityName = hostname; |
| 107 | this.sourceName = hostname; |
| 108 | } |
| 109 | |
| 110 | private final transient char[] hexArray = "0123456789ABCDEF".toCharArray(); |
| 111 | private String bytesToHex(byte[] bytes) { |
| 112 | char[] hexChars = new char[bytes.length * 2]; |
| 113 | for ( int j = 0; j < bytes.length; j++ ) { |
| 114 | int v = bytes[j] & 0xFF; |
| 115 | hexChars[j * 2] = hexArray[v >>> 4]; |
| 116 | hexChars[j * 2 + 1] = hexArray[v & 0x0F]; |
| 117 | } |
| 118 | return new String(hexChars); |
| 119 | } |
| 120 | |
| 121 | public String getName() { |
| 122 | return "commonEventHeader"; |
| 123 | } |
| 124 | |
| 125 | public Class getType() { |
| 126 | return EventHeader.class; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Returns value of domain |
| 131 | * @return |
| 132 | */ |
| 133 | public String getDomain() { |
| 134 | return domain; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Sets new value of domain |
| 139 | * @param |
| 140 | */ |
| 141 | public void setDomain(String domain) { |
| 142 | this.domain = domain; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns value of eventId |
| 147 | * @return |
| 148 | */ |
| 149 | public String getEventId() { |
| 150 | return eventId; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Sets new value of eventId |
| 155 | * @param |
| 156 | */ |
| 157 | public void setEventId(String eventId) { |
| 158 | this.eventId = eventId; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns value of eventName |
| 163 | * @return |
| 164 | */ |
| 165 | public String getEventName() { |
| 166 | return eventName; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets new value of eventName |
| 171 | * @param |
| 172 | */ |
| 173 | public void setEventName(String eventName) { |
| 174 | this.eventName = eventName; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns value of lastEpochMicrosec |
| 179 | * @return |
| 180 | */ |
| 181 | public long getLastEpochMicrosec() { |
| 182 | return lastEpochMicrosec; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets new value of lastEpochMicrosec |
| 187 | * @param |
| 188 | */ |
| 189 | public void setLastEpochMicrosec(long lastEpochMicrosec) { |
| 190 | this.lastEpochMicrosec = lastEpochMicrosec; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Returns value of priority |
| 195 | * @return |
| 196 | */ |
| 197 | public String getPriority() { |
| 198 | return priority; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Sets new value of priority |
| 203 | * @param |
| 204 | */ |
| 205 | public void setPriority(String priority) { |
| 206 | this.priority = priority; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns value of reportingEntityId |
| 211 | * @return |
| 212 | */ |
| 213 | public String getReportingEntityId() { |
| 214 | return reportingEntityId; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Sets new value of reportingEntityId |
| 219 | * @param |
| 220 | */ |
| 221 | public void setReportingEntityId(String reportingEntityId) { |
| 222 | this.reportingEntityId = reportingEntityId; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Returns value of reportingEntityName |
| 227 | * @return |
| 228 | */ |
| 229 | public String getReportingEntityName() { |
| 230 | return reportingEntityName; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Sets new value of reportingEntityName |
| 235 | * @param |
| 236 | */ |
| 237 | public void setReportingEntityName(String reportingEntityName) { |
| 238 | this.reportingEntityName = reportingEntityName; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Returns value of sequence |
| 243 | * @return |
| 244 | */ |
| 245 | public int getSequence() { |
| 246 | return sequence; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets new value of sequence |
| 251 | * @param |
| 252 | */ |
| 253 | public void setSequence(int sequence) { |
| 254 | this.sequence = sequence; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Returns value of sourceId |
| 259 | * @return |
| 260 | */ |
| 261 | public String getSourceId() { |
| 262 | return sourceId; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Sets new value of sourceId |
| 267 | * @param |
| 268 | */ |
| 269 | public void setSourceId(String sourceId) { |
| 270 | this.sourceId = sourceId; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Returns value of sourceName |
| 275 | * @return |
| 276 | */ |
| 277 | public String getSourceName() { |
| 278 | return sourceName; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Sets new value of sourceName |
| 283 | * @param |
| 284 | */ |
| 285 | public void setSourceName(String sourceName) { |
| 286 | this.sourceName = sourceName; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Returns value of startEpochMicrosec |
| 291 | * @return |
| 292 | */ |
| 293 | public long getStartEpochMicrosec() { |
| 294 | return startEpochMicrosec; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Sets new value of startEpochMicrosec |
| 299 | * @param |
| 300 | */ |
| 301 | public void setStartEpochMicrosec(long startEpochMicrosec) { |
| 302 | this.startEpochMicrosec = startEpochMicrosec; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Returns value of version |
| 307 | * @return |
| 308 | */ |
| 309 | public int getVersion() { |
| 310 | return version; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Sets new value of version |
| 315 | * @param |
| 316 | */ |
| 317 | public void setVersion(int version) { |
| 318 | this.version = version; |
| 319 | } |
| 320 | }
|