blob: cecbebdcf84da85de9ac5d68fd7663d1c7cc6d2b [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
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080028import org.onosproject.alarm.Alarm;
29import org.onosproject.alarm.AlarmId;
kmcpeake4fe18c82015-11-17 20:07:39 +000030
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;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080042import org.onosproject.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
Ray Milkey86ee5e82018-04-02 15:33:07 -070046import static org.onlab.util.Tools.readTreeFromStream;
kmcpeake4fe18c82015-11-17 20:07:39 +000047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Alarms on devices or ONOS.
51 */
52@Path("alarms")
53public class AlarmsWebResource extends AbstractWebResource {
54
Andrea Campanellae72ac552016-04-11 10:04:52 -070055 private static final String ALARM_NOT_FOUND = "Alarm is not found";
kmcpeake4fe18c82015-11-17 20:07:39 +000056
57 private final Logger log = getLogger(getClass());
58
kmcpeake4fe18c82015-11-17 20:07:39 +000059 /**
kmcpeakeb172d5f2015-12-10 11:30:43 +000060 * Get alarms. Returns a list of alarms
kmcpeake4fe18c82015-11-17 20:07:39 +000061 *
kmcpeakeb172d5f2015-12-10 11:30:43 +000062 * @param includeCleared (optional) include recently cleared alarms in response
Andrea Campanella65f9eb92017-05-02 11:36:14 -070063 * @param devId (optional) include only for specified device
kmcpeake4fe18c82015-11-17 20:07:39 +000064 * @return JSON encoded set of alarms
65 */
66 @GET
67 @Produces(MediaType.APPLICATION_JSON)
kmcpeakeb172d5f2015-12-10 11:30:43 +000068 public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") boolean includeCleared,
Andrea Campanella65f9eb92017-05-02 11:36:14 -070069 @DefaultValue("") @QueryParam("devId") String devId
kmcpeake4fe18c82015-11-17 20:07:39 +000070 ) {
71
Andrea Campanellae72ac552016-04-11 10:04:52 -070072 log.debug("Requesting all alarms, includeCleared={}", includeCleared);
kmcpeakeb172d5f2015-12-10 11:30:43 +000073 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +000074
kmcpeakeb172d5f2015-12-10 11:30:43 +000075 Iterable<Alarm> alarms;
76 if (StringUtils.isBlank(devId)) {
77 alarms = includeCleared
78 ? service.getAlarms()
79 : service.getActiveAlarms();
80 } else {
81 alarms = service.getAlarms(DeviceId.deviceId(devId));
82 }
83 ObjectNode result = new ObjectMapper().createObjectNode();
84 result.set("alarms", new AlarmCodec().encode(alarms, this));
kmcpeake4fe18c82015-11-17 20:07:39 +000085 return ok(result.toString()).build();
86
87 }
88
89 /**
90 * Get specified alarm. Returns details of the specified alarm.
91 *
92 * @param id ONOS allocated identifier
93 * @return JSON encoded alarm
94 */
95 @GET
96 @Path("{id}")
97 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +000098 public Response getAlarm(@PathParam("id") String id) {
Andrea Campanellae72ac552016-04-11 10:04:52 -070099 log.debug("HTTP GET alarm for id={}", id);
kmcpeake4fe18c82015-11-17 20:07:39 +0000100
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700101 AlarmId alarmId = AlarmId.alarmId(id);
kmcpeakeb172d5f2015-12-10 11:30:43 +0000102 Alarm alarm = get(AlarmService.class).getAlarm(alarmId);
kmcpeake4fe18c82015-11-17 20:07:39 +0000103
kmcpeakeb172d5f2015-12-10 11:30:43 +0000104 ObjectNode result = new ObjectMapper().createObjectNode();
105 result.set("alarm", new AlarmCodec().encode(alarm, this));
kmcpeake4fe18c82015-11-17 20:07:39 +0000106 return ok(result.toString()).build();
107 }
108
109 /**
110 * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of its fields may have
111 * been updated since the REST client last retrieved the alarm being updated.
112 *
Brian O'Connor08eb1ac2015-12-07 00:11:32 -0800113 * @param alarmIdPath alarm id path
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700114 * @param stream input JSON
kmcpeake4fe18c82015-11-17 20:07:39 +0000115 * @return updated JSON encoded alarm
116 */
117 @PUT
118 @Path("{alarm_id}")
119 @Consumes(MediaType.APPLICATION_JSON)
120 @Produces(MediaType.APPLICATION_JSON)
kmcpeakea5404812015-12-08 11:52:50 +0000121 public Response update(@PathParam("alarm_id") String alarmIdPath, InputStream stream) {
Andrea Campanellae72ac552016-04-11 10:04:52 -0700122 log.debug("PUT NEW ALARM at /{}", alarmIdPath);
kmcpeake4fe18c82015-11-17 20:07:39 +0000123
124 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -0700125 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
Andrea Campanellae72ac552016-04-11 10:04:52 -0700126 log.debug("jsonTree={}", jsonTree);
kmcpeake4fe18c82015-11-17 20:07:39 +0000127
kmcpeakeb172d5f2015-12-10 11:30:43 +0000128 Alarm alarm = new AlarmCodec().decode(jsonTree, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000129
kmcpeakeb172d5f2015-12-10 11:30:43 +0000130 AlarmService service = get(AlarmService.class);
kmcpeake4fe18c82015-11-17 20:07:39 +0000131
Andrea Campanella65f9eb92017-05-02 11:36:14 -0700132 if (!alarmIdPath.equals(alarm.id().toString())) {
133 throw new IllegalArgumentException("id in path is " + alarmIdPath
134 + " but payload uses id=" + alarm.id().toString());
kmcpeake4fe18c82015-11-17 20:07:39 +0000135
136 }
kmcpeakeb172d5f2015-12-10 11:30:43 +0000137 Alarm updated = service.updateBookkeepingFields(
Ray Milkey9c7c3692018-02-09 16:29:09 -0800138 alarm.id(), alarm.cleared(), alarm.acknowledged(), alarm.assignedUser()
kmcpeakeb172d5f2015-12-10 11:30:43 +0000139 );
140 ObjectNode encoded = new AlarmCodec().encode(updated, this);
kmcpeake4fe18c82015-11-17 20:07:39 +0000141 return ok(encoded.toString()).build();
142
143 } catch (IOException ioe) {
144 throw new IllegalArgumentException(ioe);
145 }
146 }
147
kmcpeake4fe18c82015-11-17 20:07:39 +0000148}