kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.faultmanagement.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | |
| 24 | import javax.ws.rs.core.Response; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.DefaultValue; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.PUT; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.QueryParam; |
| 36 | import javax.ws.rs.core.MediaType; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 37 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; |
| 38 | import org.slf4j.Logger; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Alarms on devices or ONOS. |
| 43 | */ |
| 44 | @Path("alarms") |
| 45 | public 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() { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 52 | } |
| 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) |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 62 | public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") boolean includeCleared |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 63 | ) { |
| 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) |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 89 | public Response getAlarm(@PathParam("id") String id) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 90 | 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'Connor | 08eb1ac | 2015-12-07 00:11:32 -0800 | [diff] [blame] | 104 | * @param alarmIdPath alarm id path |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 105 | * @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) |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 112 | public Response update(@PathParam("alarm_id") String alarmIdPath, InputStream stream) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 113 | 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 | |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 137 | private static AlarmId toAlarmId(String id) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 138 | 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 | } |