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