blob: d62fa76f6eff958b940e243d5556169a2f98e5eb [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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 */
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;
kmcpeake4fe18c82015-11-17 20:07:39 +000037import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
38import org.slf4j.Logger;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Alarms on devices or ONOS.
43 */
44@Path("alarms")
45public class AlarmsWebResource extends AbstractWebResource {
46
47 public static final String ALARM_NOT_FOUND = "Alarm is not found";
48
49 private final Logger log = getLogger(getClass());
50
51 public AlarmsWebResource() {
kmcpeake4fe18c82015-11-17 20:07:39 +000052 }
53
54 /**
55 * Get all alarms. Returns a list of all alarms across all devices.
56 *
57 * @param includeCleared include recently cleared alarms in response
58 * @return JSON encoded set of alarms
59 */
60 @GET
61 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +000062 public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") boolean includeCleared
kmcpeake4fe18c82015-11-17 20:07:39 +000063 ) {
64
65 log.info("Requesting all alarms, includeCleared={}", includeCleared);
66 final AlarmService service = get(AlarmService.class);
67
68 final Iterable<Alarm> alarms = includeCleared
69 ? service.getAlarms()
70 : service.getActiveAlarms();
71
72 final ObjectNode result = new ObjectMapper().createObjectNode();
73 result.set("alarms",
74 codec(Alarm.class).
75 encode(alarms, this));
76 return ok(result.toString()).build();
77
78 }
79
80 /**
81 * Get specified alarm. Returns details of the specified alarm.
82 *
83 * @param id ONOS allocated identifier
84 * @return JSON encoded alarm
85 */
86 @GET
87 @Path("{id}")
88 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +000089 public Response getAlarm(@PathParam("id") String id) {
kmcpeake4fe18c82015-11-17 20:07:39 +000090 log.info("HTTP GET alarm for id={}", id);
91
92 final AlarmId alarmId = toAlarmId(id);
93 final Alarm alarm = get(AlarmService.class).getAlarm(alarmId);
94
95 final ObjectNode result = mapper().createObjectNode();
96 result.set("alarm", codec(Alarm.class).encode(alarm, this));
97 return ok(result.toString()).build();
98 }
99
100 /**
101 * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of its fields may have
102 * been updated since the REST client last retrieved the alarm being updated.
103 *
Brian O'Connor08eb1ac2015-12-07 00:11:32 -0800104 * @param alarmIdPath alarm id path
kmcpeake4fe18c82015-11-17 20:07:39 +0000105 * @param stream input JSON
106 * @return updated JSON encoded alarm
107 */
108 @PUT
109 @Path("{alarm_id}")
110 @Consumes(MediaType.APPLICATION_JSON)
111 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +0000112 public Response update(@PathParam("alarm_id") String alarmIdPath, InputStream stream) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000113 log.info("PUT NEW ALARM at /{}", alarmIdPath);
114
115 try {
116 final ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
117 log.info("jsonTree={}", jsonTree);
118
119 final Alarm alarm = codec(Alarm.class).decode(jsonTree, this);
120
121 final AlarmService service = get(AlarmService.class);
122
123 if (Long.parseLong(alarmIdPath) != alarm.id().fingerprint()) {
124 throw new IllegalArgumentException("id in path is " + Long.parseLong(alarmIdPath)
125 + " but payload uses id=" + alarm.id().fingerprint());
126
127 }
128 final Alarm updated = service.update(alarm);
129 final ObjectNode encoded = new AlarmCodec().encode(updated, this);
130 return ok(encoded.toString()).build();
131
132 } catch (IOException ioe) {
133 throw new IllegalArgumentException(ioe);
134 }
135 }
136
kmcpeakea5404812015-12-08 11:52:50 +0000137 private static AlarmId toAlarmId(String id) {
kmcpeake4fe18c82015-11-17 20:07:39 +0000138 try {
139 return AlarmId.valueOf(Long.parseLong(id));
140 } catch (NumberFormatException ex) {
141 throw new IllegalArgumentException("Alarm id should be numeric", ex);
142 }
143
144 }
145
146}