blob: 118cd1823644640ea4f137c83f8d15459939fd03 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
kmcpeakeb172d5f2015-12-10 11:30:43 +00002 * Copyright 2015 Open Networking Laboratory
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
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 */
16package org.onosproject.faultmanagement.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import java.io.IOException;
21import java.io.InputStream;
22import org.onosproject.rest.AbstractWebResource;
23
24import javax.ws.rs.core.Response;
25import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
26import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DefaultValue;
30import javax.ws.rs.GET;
31import javax.ws.rs.PUT;
32import javax.ws.rs.Path;
33import javax.ws.rs.PathParam;
34import javax.ws.rs.Produces;
35import javax.ws.rs.QueryParam;
36import javax.ws.rs.core.MediaType;
kmcpeakeb172d5f2015-12-10 11:30:43 +000037import org.apache.commons.lang.StringUtils;
kmcpeake4fe18c82015-11-17 20:07:39 +000038import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
kmcpeakeb172d5f2015-12-10 11:30:43 +000039import org.onosproject.net.DeviceId;
kmcpeake4fe18c82015-11-17 20:07:39 +000040import org.slf4j.Logger;
41import static org.slf4j.LoggerFactory.getLogger;
42
43/**
44 * Alarms on devices or ONOS.
45 */
46@Path("alarms")
47public class AlarmsWebResource extends AbstractWebResource {
48
49 public static final String ALARM_NOT_FOUND = "Alarm is not found";
50
51 private final Logger log = getLogger(getClass());
52
kmcpeake4fe18c82015-11-17 20:07:39 +000053 /**
kmcpeakeb172d5f2015-12-10 11:30:43 +000054 * Get alarms. Returns a list of alarms
kmcpeake4fe18c82015-11-17 20:07:39 +000055 *
kmcpeakeb172d5f2015-12-10 11:30:43 +000056 * @param includeCleared (optional) include recently cleared alarms in response
57 * @param devId (optional) include only for specified device
kmcpeake4fe18c82015-11-17 20:07:39 +000058 * @return JSON encoded set of alarms
59 */
60 @GET
61 @Produces(MediaType.APPLICATION_JSON)
kmcpeakeb172d5f2015-12-10 11:30:43 +000062 public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") boolean includeCleared,
63 @DefaultValue("") @QueryParam("devId") String devId
kmcpeake4fe18c82015-11-17 20:07:39 +000064 ) {
65
66 log.info("Requesting all alarms, includeCleared={}", includeCleared);
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000068
kmcpeakeb172d5f2015-12-10 11:30:43 +000069 Iterable<Alarm> alarms;
70 if (StringUtils.isBlank(devId)) {
71 alarms = includeCleared
72 ? service.getAlarms()
73 : service.getActiveAlarms();
74 } else {
75 alarms = service.getAlarms(DeviceId.deviceId(devId));
76 }
77 ObjectNode result = new ObjectMapper().createObjectNode();
78 result.set("alarms", new AlarmCodec().encode(alarms, this));
kmcpeake4fe18c82015-11-17 20:07:39 +000079 return ok(result.toString()).build();
80
81 }
82
83 /**
84 * Get specified alarm. Returns details of the specified alarm.
85 *
86 * @param id ONOS allocated identifier
87 * @return JSON encoded alarm
88 */
89 @GET
90 @Path("{id}")
91 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +000092 public Response getAlarm(@PathParam("id") String id) {
kmcpeake4fe18c82015-11-17 20:07:39 +000093 log.info("HTTP GET alarm for id={}", id);
94
kmcpeakeb172d5f2015-12-10 11:30:43 +000095 AlarmId alarmId = toAlarmId(id);
96 Alarm alarm = get(AlarmService.class).getAlarm(alarmId);
kmcpeake4fe18c82015-11-17 20:07:39 +000097
kmcpeakeb172d5f2015-12-10 11:30:43 +000098 ObjectNode result = new ObjectMapper().createObjectNode();
99 result.set("alarm", new AlarmCodec().encode(alarm, this));
kmcpeake4fe18c82015-11-17 20:07:39 +0000100 return ok(result.toString()).build();
101 }
102
103 /**
104 * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of its fields may have
105 * been updated since the REST client last retrieved the alarm being updated.
106 *
Brian O'Connor08eb1ac2015-12-07 00:11:32 -0800107 * @param alarmIdPath alarm id path
kmcpeake4fe18c82015-11-17 20:07:39 +0000108 * @param stream input JSON
109 * @return updated JSON encoded alarm
110 */
111 @PUT
112 @Path("{alarm_id}")
113 @Consumes(MediaType.APPLICATION_JSON)
114 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +0000115 public Response update(@PathParam("alarm_id") String alarmIdPath, InputStream stream) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000116 log.info("PUT NEW ALARM at /{}", alarmIdPath);
117
118 try {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000119 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
kmcpeake4fe18c82015-11-17 20:07:39 +0000120 log.info("jsonTree={}", jsonTree);
121
kmcpeakeb172d5f2015-12-10 11:30:43 +0000122 Alarm alarm = new AlarmCodec().decode(jsonTree, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000123
kmcpeakeb172d5f2015-12-10 11:30:43 +0000124 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +0000125
126 if (Long.parseLong(alarmIdPath) != alarm.id().fingerprint()) {
127 throw new IllegalArgumentException("id in path is " + Long.parseLong(alarmIdPath)
128 + " but payload uses id=" + alarm.id().fingerprint());
129
130 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000131 Alarm updated = service.updateBookkeepingFields(
132 alarm.id(), alarm.acknowledged(), alarm.assignedUser()
133 );
134 ObjectNode encoded = new AlarmCodec().encode(updated, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000135 return ok(encoded.toString()).build();
136
137 } catch (IOException ioe) {
138 throw new IllegalArgumentException(ioe);
139 }
140 }
141
kmcpeakea5404812015-12-08 11:52:50 +0000142 private static AlarmId toAlarmId(String id) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000143 try {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000144 return AlarmId.alarmId(Long.parseLong(id));
kmcpeake4fe18c82015-11-17 20:07:39 +0000145 } catch (NumberFormatException ex) {
146 throw new IllegalArgumentException("Alarm id should be numeric", ex);
147 }
148
149 }
150
151}