blob: 1963b7c61fbf7077738885d7ad2c1cb4b688a2e8 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070020
kmcpeake4fe18c82015-11-17 20:07:39 +000021import java.io.IOException;
22import java.io.InputStream;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070023
kmcpeake4fe18c82015-11-17 20:07:39 +000024import org.onosproject.rest.AbstractWebResource;
25
26import javax.ws.rs.core.Response;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070027
kmcpeake4fe18c82015-11-17 20:07:39 +000028import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
29import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
30
31import javax.ws.rs.Consumes;
32import javax.ws.rs.DefaultValue;
33import javax.ws.rs.GET;
34import javax.ws.rs.PUT;
35import javax.ws.rs.Path;
36import javax.ws.rs.PathParam;
37import javax.ws.rs.Produces;
38import javax.ws.rs.QueryParam;
39import javax.ws.rs.core.MediaType;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070040
kmcpeakeb172d5f2015-12-10 11:30:43 +000041import org.apache.commons.lang.StringUtils;
kmcpeake4fe18c82015-11-17 20:07:39 +000042import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
kmcpeakeb172d5f2015-12-10 11:30:43 +000043import org.onosproject.net.DeviceId;
kmcpeake4fe18c82015-11-17 20:07:39 +000044import org.slf4j.Logger;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070045
kmcpeake4fe18c82015-11-17 20:07:39 +000046import static org.slf4j.LoggerFactory.getLogger;
47
48/**
49 * Alarms on devices or ONOS.
50 */
51@Path("alarms")
52public class AlarmsWebResource extends AbstractWebResource {
53
Andrea Campanellae72ac552016-04-11 10:04:52 -070054 private static final String ALARM_NOT_FOUND = "Alarm is not found";
kmcpeake4fe18c82015-11-17 20:07:39 +000055
56 private final Logger log = getLogger(getClass());
57
kmcpeake4fe18c82015-11-17 20:07:39 +000058 /**
kmcpeakeb172d5f2015-12-10 11:30:43 +000059 * Get alarms. Returns a list of alarms
kmcpeake4fe18c82015-11-17 20:07:39 +000060 *
kmcpeakeb172d5f2015-12-10 11:30:43 +000061 * @param includeCleared (optional) include recently cleared alarms in response
Andrea Campanella65f9eb92017-05-02 11:36:14 -070062 * @param devId (optional) include only for specified device
kmcpeake4fe18c82015-11-17 20:07:39 +000063 * @return JSON encoded set of alarms
64 */
65 @GET
66 @Produces(MediaType.APPLICATION_JSON)
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") boolean includeCleared,
Andrea Campanella65f9eb92017-05-02 11:36:14 -070068 @DefaultValue("") @QueryParam("devId") String devId
kmcpeake4fe18c82015-11-17 20:07:39 +000069 ) {
70
Andrea Campanellae72ac552016-04-11 10:04:52 -070071 log.debug("Requesting all alarms, includeCleared={}", includeCleared);
kmcpeakeb172d5f2015-12-10 11:30:43 +000072 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000073
kmcpeakeb172d5f2015-12-10 11:30:43 +000074 Iterable<Alarm> alarms;
75 if (StringUtils.isBlank(devId)) {
76 alarms = includeCleared
77 ? service.getAlarms()
78 : service.getActiveAlarms();
79 } else {
80 alarms = service.getAlarms(DeviceId.deviceId(devId));
81 }
82 ObjectNode result = new ObjectMapper().createObjectNode();
83 result.set("alarms", new AlarmCodec().encode(alarms, this));
kmcpeake4fe18c82015-11-17 20:07:39 +000084 return ok(result.toString()).build();
85
86 }
87
88 /**
89 * Get specified alarm. Returns details of the specified alarm.
90 *
91 * @param id ONOS allocated identifier
92 * @return JSON encoded alarm
93 */
94 @GET
95 @Path("{id}")
96 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +000097 public Response getAlarm(@PathParam("id") String id) {
Andrea Campanellae72ac552016-04-11 10:04:52 -070098 log.debug("HTTP GET alarm for id={}", id);
kmcpeake4fe18c82015-11-17 20:07:39 +000099
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700100 AlarmId alarmId = AlarmId.alarmId(id);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000101 Alarm alarm = get(AlarmService.class).getAlarm(alarmId);
kmcpeake4fe18c82015-11-17 20:07:39 +0000102
kmcpeakeb172d5f2015-12-10 11:30:43 +0000103 ObjectNode result = new ObjectMapper().createObjectNode();
104 result.set("alarm", new AlarmCodec().encode(alarm, this));
kmcpeake4fe18c82015-11-17 20:07:39 +0000105 return ok(result.toString()).build();
106 }
107
108 /**
109 * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of its fields may have
110 * been updated since the REST client last retrieved the alarm being updated.
111 *
Brian O'Connor08eb1ac2015-12-07 00:11:32 -0800112 * @param alarmIdPath alarm id path
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700113 * @param stream input JSON
kmcpeake4fe18c82015-11-17 20:07:39 +0000114 * @return updated JSON encoded alarm
115 */
116 @PUT
117 @Path("{alarm_id}")
118 @Consumes(MediaType.APPLICATION_JSON)
119 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +0000120 public Response update(@PathParam("alarm_id") String alarmIdPath, InputStream stream) {
Andrea Campanellae72ac552016-04-11 10:04:52 -0700121 log.debug("PUT NEW ALARM at /{}", alarmIdPath);
kmcpeake4fe18c82015-11-17 20:07:39 +0000122
123 try {
kmcpeakeb172d5f2015-12-10 11:30:43 +0000124 ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
Andrea Campanellae72ac552016-04-11 10:04:52 -0700125 log.debug("jsonTree={}", jsonTree);
kmcpeake4fe18c82015-11-17 20:07:39 +0000126
kmcpeakeb172d5f2015-12-10 11:30:43 +0000127 Alarm alarm = new AlarmCodec().decode(jsonTree, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000128
kmcpeakeb172d5f2015-12-10 11:30:43 +0000129 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +0000130
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700131 if (!alarmIdPath.equals(alarm.id().toString())) {
132 throw new IllegalArgumentException("id in path is " + alarmIdPath
133 + " but payload uses id=" + alarm.id().toString());
kmcpeake4fe18c82015-11-17 20:07:39 +0000134
135 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000136 Alarm updated = service.updateBookkeepingFields(
Ray Milkey9c7c3692018-02-09 16:29:09 -0800137 alarm.id(), alarm.cleared(), alarm.acknowledged(), alarm.assignedUser()
kmcpeakeb172d5f2015-12-10 11:30:43 +0000138 );
139 ObjectNode encoded = new AlarmCodec().encode(updated, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000140 return ok(encoded.toString()).build();
141
142 } catch (IOException ioe) {
143 throw new IllegalArgumentException(ioe);
144 }
145 }
146
kmcpeake4fe18c82015-11-17 20:07:39 +0000147}