From 76dc892491948adae5e5e62cf94448967e8d865b Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Sun, 6 Dec 2015 07:15:03 -0800 Subject: Fixes bad POM file with ONOS commit 8c68536972f63069c263635c9d9f4f31d7f3e9a2 Change-Id: I7adb5a2d3738d53dbc41db7577768b0e7ced5450 Signed-off-by: Ashlee Young --- .../src/onos/apps/faultmanagement/fmweb/pom.xml | 141 ++++++++++++++++++++ .../faultmanagement/web/AlarmCodec.java | 101 ++++++++++++++ .../faultmanagement/web/AlarmsWebResource.java | 148 +++++++++++++++++++++ .../faultmanagement/web/package-info.java | 20 +++ .../fmweb/src/main/webapp/WEB-INF/web.xml | 42 ++++++ .../faultmanagement/web/AlarmCodecContext.java | 73 ++++++++++ .../faultmanagement/web/AlarmCodecTest.java | 140 +++++++++++++++++++ .../faultmanagement/web/AlarmJsonMatcher.java | 135 +++++++++++++++++++ .../faultmanagement/web/AlarmsWebResourceTest.java | 72 ++++++++++ .../faultmanagement/web/alarm-full.json | 15 +++ .../faultmanagement/web/alarm-minimal.json | 12 ++ 11 files changed, 899 insertions(+) create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/pom.xml create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodec.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/package-info.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/main/webapp/WEB-INF/web.xml create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-full.json create mode 100644 framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-minimal.json (limited to 'framework/src/onos/apps/faultmanagement/fmweb') diff --git a/framework/src/onos/apps/faultmanagement/fmweb/pom.xml b/framework/src/onos/apps/faultmanagement/fmweb/pom.xml new file mode 100644 index 00000000..8d759710 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/pom.xml @@ -0,0 +1,141 @@ + + + + 4.0.0 + + org.onosproject + onos-app-fm + 1.4.0-SNAPSHOT + ../pom.xml + + + + onos-app-fm-web + bundle + + /onos/v1/fm + 1.0.0 + ONOS Fault Management Application REST API + + APIs for interacting with the Fault Management application. + + org.onos.faultmanagement.web + + + + + javax.ws.rs + jsr311-api + 1.1.1 + + + org.onosproject + onos-incubator-api + ${project.version} + jar + + + org.onosproject + onlab-osgi + ${project.version} + tests + test + + + com.sun.jersey + jersey-client + 1.19 + test + jar + + + org.onosproject + onos-core-common + ${project.version} + test + jar + + + org.onosproject + onos-rest + ${project.version} + test + jar + + + + org.onosproject + onos-rest + ${project.version} + tests + test + + + + com.sun.jersey.jersey-test-framework + jersey-test-framework-core + 1.19 + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + <_wab>src/main/webapp/ + + WEB-INF/classes/apidoc/swagger.json=target/swagger.json, + {maven-resources} + + + ${project.groupId}.${project.artifactId} + + + org.slf4j, + org.osgi.framework, + javax.ws.rs, + javax.ws.rs.core, + com.sun.jersey.api.core, + com.sun.jersey.spi.container.servlet, + com.sun.jersey.server.impl.container.servlet, + com.fasterxml.jackson.databind, + com.fasterxml.jackson.databind.node, + com.fasterxml.jackson.core, + org.apache.karaf.shell.commands, + org.apache.commons.lang.math.*, + com.google.common.*, + org.onlab.packet.*, + org.onlab.rest.*, + org.onosproject.*, + org.onlab.util.*, + org.jboss.netty.util.* + + ${web.context} + + + + + + + diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodec.java b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodec.java new file mode 100644 index 00000000..a764c908 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodec.java @@ -0,0 +1,101 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import com.fasterxml.jackson.databind.JsonNode; +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +import static com.google.common.base.Preconditions.checkNotNull; +import org.onosproject.net.DeviceId; +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; +import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; +import org.slf4j.Logger; +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Implementation of encoder for Alarm codec. + */ +public final class AlarmCodec extends JsonCodec { + + private final Logger log = getLogger(getClass()); + + @Override + public ObjectNode encode(final Alarm alarm, final CodecContext context) { + checkNotNull(alarm, "Alarm cannot be null"); + + return context.mapper().createObjectNode() + .put("id", alarm.id().fingerprint()) + .put("deviceId", alarm.deviceId().toString()) + .put("description", alarm.description()) + .put("source", + alarm.source() == null ? null + : alarm.source().toString()) + .put("timeRaised", alarm.timeRaised()) + .put("timeUpdated", alarm.timeUpdated()) + .put("timeCleared", alarm.timeCleared()) + .put("severity", alarm.severity().toString()) + .put("serviceAffecting", alarm.serviceAffecting()) + .put("acknowledged", alarm.acknowledged()) + .put("manuallyClearable", alarm.manuallyClearable()) + .put("assignedUser", alarm.assignedUser()); + + } + + @Override + public Alarm decode(final ObjectNode json, final CodecContext context) { + if (json == null || !json.isObject()) { + return null; + } + + log.debug("id={}, full json={} ", json.get("id"), json); + final Long id = json.get("id").asLong(); + + final DeviceId deviceId = DeviceId.deviceId(json.get("deviceId").asText()); + final String description = json.get("description").asText(); + final Long timeRaised = json.get("timeRaised").asLong(); + final Long timeUpdated = json.get("timeUpdated").asLong(); + + final JsonNode jsonTimeCleared = json.get("timeCleared"); + final Long timeCleared = jsonTimeCleared == null || jsonTimeCleared.isNull() ? null : jsonTimeCleared.asLong(); + + final Alarm.SeverityLevel severity + = Alarm.SeverityLevel.valueOf(json.get("severity").asText().toUpperCase()); + + final Boolean serviceAffecting = json.get("serviceAffecting").asBoolean(); + final Boolean acknowledged = json.get("acknowledged").asBoolean(); + final Boolean manuallyClearable = json.get("manuallyClearable").asBoolean(); + + final JsonNode jsonAssignedUser = json.get("assignedUser"); + final String assignedUser + = jsonAssignedUser == null || jsonAssignedUser.isNull() ? null : jsonAssignedUser.asText(); + + return new DefaultAlarm.Builder( + AlarmId.valueOf(id), deviceId, description, severity, timeRaised).forSource(AlarmEntityId.NONE). + withTimeUpdated(timeUpdated). + withTimeCleared(timeCleared). + withServiceAffecting(serviceAffecting). + withAcknowledged(acknowledged). + withManuallyClearable(manuallyClearable). + withAssignedUser(assignedUser). + build(); + + } +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java new file mode 100644 index 00000000..56c891cd --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java @@ -0,0 +1,148 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import java.io.IOException; +import java.io.InputStream; +import org.onosproject.rest.AbstractWebResource; + +import javax.ws.rs.core.Response; +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; +import org.onosproject.codec.CodecService; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; +import org.slf4j.Logger; +import static org.slf4j.LoggerFactory.getLogger; + +/** + * Alarms on devices or ONOS. + */ +@Path("alarms") +public class AlarmsWebResource extends AbstractWebResource { + + public static final String ALARM_NOT_FOUND = "Alarm is not found"; + + private final Logger log = getLogger(getClass()); + + public AlarmsWebResource() { + get(CodecService.class).registerCodec(Alarm.class, new AlarmCodec()); + } + + /** + * Get all alarms. Returns a list of all alarms across all devices. + * + * @param includeCleared include recently cleared alarms in response + * @return JSON encoded set of alarms + */ + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getAlarms(@DefaultValue("false") @QueryParam("includeCleared") final boolean includeCleared + ) { + + log.info("Requesting all alarms, includeCleared={}", includeCleared); + final AlarmService service = get(AlarmService.class); + + final Iterable alarms = includeCleared + ? service.getAlarms() + : service.getActiveAlarms(); + + final ObjectNode result = new ObjectMapper().createObjectNode(); + result.set("alarms", + codec(Alarm.class). + encode(alarms, this)); + return ok(result.toString()).build(); + + } + + /** + * Get specified alarm. Returns details of the specified alarm. + * + * @param id ONOS allocated identifier + * @return JSON encoded alarm + */ + @GET + @Path("{id}") + @Produces(MediaType.APPLICATION_JSON) + public Response getAlarm(@PathParam("id") final String id) { + log.info("HTTP GET alarm for id={}", id); + + final AlarmId alarmId = toAlarmId(id); + final Alarm alarm = get(AlarmService.class).getAlarm(alarmId); + + final ObjectNode result = mapper().createObjectNode(); + result.set("alarm", codec(Alarm.class).encode(alarm, this)); + return ok(result.toString()).build(); + } + + /** + * Update book-keeping fields on the alarm. Returns an up-to-date version of the alarm. Some of its fields may have + * been updated since the REST client last retrieved the alarm being updated. + * + * @param alarmIdPath + * @param stream input JSON + * @return updated JSON encoded alarm + */ + @PUT + @Path("{alarm_id}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response update(@PathParam("alarm_id") final String alarmIdPath, final InputStream stream) { + log.info("PUT NEW ALARM at /{}", alarmIdPath); + + try { + final ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); + log.info("jsonTree={}", jsonTree); + + final Alarm alarm = codec(Alarm.class).decode(jsonTree, this); + + final AlarmService service = get(AlarmService.class); + + if (Long.parseLong(alarmIdPath) != alarm.id().fingerprint()) { + throw new IllegalArgumentException("id in path is " + Long.parseLong(alarmIdPath) + + " but payload uses id=" + alarm.id().fingerprint()); + + } + final Alarm updated = service.update(alarm); + final ObjectNode encoded = new AlarmCodec().encode(updated, this); + return ok(encoded.toString()).build(); + + } catch (IOException ioe) { + throw new IllegalArgumentException(ioe); + } + } + + private static AlarmId toAlarmId(final String id) { + try { + return AlarmId.valueOf(Long.parseLong(id)); + } catch (NumberFormatException ex) { + throw new IllegalArgumentException("Alarm id should be numeric", ex); + } + + } + +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/package-info.java b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/package-info.java new file mode 100644 index 00000000..9c291231 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Fault management web support. + */ +package org.onosproject.faultmanagement.web; diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/main/webapp/WEB-INF/web.xml b/framework/src/onos/apps/faultmanagement/fmweb/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..18671f01 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + FM2 REST API v1.0 + + + JAX-RS Service + com.sun.jersey.spi.container.servlet.ServletContainer + + com.sun.jersey.config.property.resourceConfigClass + com.sun.jersey.api.core.ClassNamesResourceConfig + + + com.sun.jersey.config.property.classnames + + org.onosproject.faultmanagement.web.AlarmsWebResource + + 10 + + + + JAX-RS Service + /* + + diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java new file mode 100644 index 00000000..89886e8a --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java @@ -0,0 +1,73 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.onosproject.codec.CodecContext; +import org.onosproject.codec.JsonCodec; + + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; + +/** + * Mock codec context for use in codec unit tests. + */ +public class AlarmCodecContext implements CodecContext { + + private final ObjectMapper mapper = new ObjectMapper(); + private final Map, JsonCodec> codecs = new ConcurrentHashMap<>(); + + /** + * Constructs a new mock codec context. + */ + public AlarmCodecContext() { + codecs.clear(); + registerCodec(Alarm.class, new AlarmCodec()); + + } + + @Override + public ObjectMapper mapper() { + return mapper; + } + + @SuppressWarnings("unchecked") + @Override + public T getService(Class serviceClass) { + // TODO + return null; + } + + /** + * Registers the specified JSON codec for the given entity class. + * + * @param entityClass entity class + * @param codec JSON codec + * @param entity type + */ + public void registerCodec(Class entityClass, JsonCodec codec) { + codecs.putIfAbsent(entityClass, codec); + } + + @SuppressWarnings("unchecked") + @Override + public JsonCodec codec(Class entityClass) { + return codecs.get(entityClass); + } +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java new file mode 100644 index 00000000..3009b99a --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecTest.java @@ -0,0 +1,140 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import java.io.IOException; +import java.io.InputStream; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; + +import org.junit.Test; +import org.onosproject.codec.JsonCodec; +import static org.onosproject.faultmanagement.web.AlarmJsonMatcher.matchesAlarm; +import org.onosproject.net.DeviceId; +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; +import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; +import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; + +public class AlarmCodecTest { + + private final AlarmCodecContext context = new AlarmCodecContext(); + + // Use this to check handling for miminal Alarm + private final Alarm alarmMinimumFields = new DefaultAlarm.Builder( + new AlarmId(44), + DeviceId.deviceId("of:2222000000000000"), + "NE unreachable", + Alarm.SeverityLevel.CLEARED, + 1). + build(); + + // Use this to check handling for fully populated Alarm + private final Alarm alarmWithSource = new DefaultAlarm.Builder( + new AlarmId(44), + DeviceId.deviceId("of:2222000000000000"), + "NE unreachable", + Alarm.SeverityLevel.CLEARED, 1). + forSource(AlarmEntityId.alarmEntityId("port:1/2/3/4")). + withTimeUpdated(2). + withTimeCleared(3L). + withServiceAffecting(true). + withAcknowledged(true). + withManuallyClearable(true). + withAssignedUser("the assigned user").build(); + + @Test + public void alarmCodecTestWithOptionalFieldMissing() { + //context.registerService(AlarmService.class, new AlarmServiceAdapter()); + final JsonCodec codec = context.codec(Alarm.class); + assertThat(codec, is(notNullValue())); + + final ObjectNode alarmJson = codec.encode(alarmMinimumFields, context); + assertThat(alarmJson, notNullValue()); + assertThat(alarmJson, matchesAlarm(alarmMinimumFields)); + + } + + @Test + public void alarmCodecTestWithOptionalField() { + final JsonCodec codec = context.codec(Alarm.class); + assertThat(codec, is(notNullValue())); + + final ObjectNode alarmJson = codec.encode(alarmWithSource, context); + assertThat(alarmJson, notNullValue()); + assertThat(alarmJson, matchesAlarm(alarmWithSource)); + + } + + @Test + public void verifyMinimalAlarmIsEncoded() throws Exception { + final JsonCodec alarmCodec = context.codec(Alarm.class); + + final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-minimal.json"); + assertCommon(alarm); + + assertThat(alarm.timeCleared(), nullValue()); + assertThat(alarm.assignedUser(), nullValue()); + + } + + @Test + public void verifyFullyLoadedAlarmIsEncoded() throws Exception { + final JsonCodec alarmCodec = context.codec(Alarm.class); + + final Alarm alarm = getDecodedAlarm(alarmCodec, "alarm-full.json"); + assertCommon(alarm); + + assertThat(alarm.timeCleared(), is(2222L)); + assertThat(alarm.assignedUser(), is("foo")); + + } + + private void assertCommon(final Alarm alarm) { + assertThat(alarm.id(), is(new AlarmId(10L))); + assertThat(alarm.description(), is("NE is not reachable")); + assertThat(alarm.source(), is(AlarmEntityId.NONE)); + assertThat(alarm.timeRaised(), is(999L)); + assertThat(alarm.timeUpdated(), is(1111L)); + assertThat(alarm.severity(), is(Alarm.SeverityLevel.MAJOR)); + assertThat(alarm.serviceAffecting(), is(true)); + assertThat(alarm.acknowledged(), is(false)); + assertThat(alarm.manuallyClearable(), is(true)); + } + + /** + * Reads in a rule from the given resource and decodes it. + * + * @param resourceName resource to use to read the JSON for the rule + * @return decoded flow rule + * @throws IOException if processing the resource failsdecode + */ + private Alarm getDecodedAlarm(final JsonCodec codec, final String resourceName) throws IOException { + final InputStream jsonStream = AlarmCodecTest.class + .getResourceAsStream(resourceName); + final JsonNode json = context.mapper().readTree(jsonStream); + assertThat(json, notNullValue()); + final Alarm result = codec.decode((ObjectNode) json, context); + assertThat(result, notNullValue()); + return result; + } + + +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java new file mode 100644 index 00000000..14bb45f3 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmJsonMatcher.java @@ -0,0 +1,135 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; + +import com.fasterxml.jackson.databind.JsonNode; + +/** + * Hamcrest matcher for alarms. + */ +public final class AlarmJsonMatcher extends TypeSafeDiagnosingMatcher { + + private final Alarm alarm; + + private AlarmJsonMatcher(final Alarm alarm) { + this.alarm = alarm; + } + + @Override + public boolean matchesSafely(final JsonNode jsonAlarm, final Description description) { + final String jsonAlarmId = jsonAlarm.get("id").asText(); + final String alarmId = Long.toString(alarm.id().fingerprint()); + if (!jsonAlarmId.equals(alarmId)) { + description.appendText("alarm id was " + jsonAlarmId); + return false; + } + + final String jsonDeviceId = jsonAlarm.get("deviceId").asText(); + final String alarmDeviceId = alarm.deviceId().toString(); + if (!jsonDeviceId.equals(alarmDeviceId)) { + description.appendText("DeviceId was " + jsonDeviceId); + return false; + } + + + final String jsonDescription = jsonAlarm.get("description").asText(); + final String alarmDesc = alarm.description(); + if (!jsonDescription.equals(alarmDesc)) { + description.appendText("description was " + jsonDescription); + return false; + } + + final long jsonTimeRaised = jsonAlarm.get("timeRaised").asLong(); + final long timeRaised = alarm.timeRaised(); + if (timeRaised != jsonTimeRaised) { + description.appendText("timeRaised was " + jsonTimeRaised); + return false; + } + + + final long jsonTimeUpdated = jsonAlarm.get("timeUpdated").asLong(); + final long timeUpdated = alarm.timeUpdated(); + if (timeUpdated != jsonTimeUpdated) { + description.appendText("timeUpdated was " + jsonTimeUpdated); + return false; + } + + final JsonNode jsonTimeClearedNode = jsonAlarm.get("timeCleared"); + + if (alarm.timeCleared() != null) { + final Long jsonTimeCleared = jsonTimeClearedNode.longValue(); + final Long timeCleared = alarm.timeCleared(); + + if (!timeCleared.equals(jsonTimeCleared)) { + description.appendText("Time Cleared was " + jsonTimeCleared); + return false; + } + } else { + // No clear time not specified, JSON representation must be empty + if (!jsonTimeClearedNode.isNull()) { + description.appendText("Time Cleared should be null"); + return false; + } + } + + final String jsonSeverity = jsonAlarm.get("severity").asText(); + final String severity = alarm.severity().toString(); + if (!severity.equals(jsonSeverity)) { + description.appendText("severity was " + jsonSeverity); + return false; + } + + final JsonNode jsonAlarmNode = jsonAlarm.get("source"); + + if (alarm.source() != null) { + final String jsonSource = jsonAlarmNode.textValue(); + final String source = alarm.source().toString(); + + if (!source.equals(jsonSource)) { + description.appendText("source was " + jsonSource); + return false; + } + } else { + // source not specified, JSON representation must be empty + if (!jsonAlarmNode.isNull()) { + description.appendText("source should be null"); + return false; + } + } + + // In progress + return true; + } + + @Override + public void describeTo(final Description description) { + description.appendText(alarm.toString()); + } + + /** + * Factory to allocate a alarm matcher. + * + * @param alarm alarm object we are looking for + * @return matcher + */ + public static AlarmJsonMatcher matchesAlarm(final Alarm alarm) { + return new AlarmJsonMatcher(alarm); + } +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java new file mode 100644 index 00000000..0b7d9811 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmsWebResourceTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.faultmanagement.web; + +import com.sun.jersey.api.client.WebResource; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import org.junit.Ignore; +import org.onlab.osgi.ServiceDirectory; +import org.onlab.osgi.TestServiceDirectory; +import org.onlab.rest.BaseResource; +import org.onosproject.codec.CodecService; +import org.onosproject.codec.impl.CodecManager; +import org.onosproject.rest.ResourceTest; + +/** + * Test of the Fault Management Web REST API for Alarms. + */ +public class AlarmsWebResourceTest extends ResourceTest { + + @Before + public void setUp() { + + final CodecManager codecService = new CodecManager(); + codecService.activate(); + + final ServiceDirectory testDirectory + = new TestServiceDirectory() + // Currently no alarms-service implemented + // .add(AlarmsService.class, alarmsService) + .add(CodecService.class, codecService); + BaseResource.setServiceDirectory(testDirectory); + } + + @Test + @Ignore + public void getAllAlarms() { + final WebResource rs = resource(); + final String response = rs.path("/alarms").get(String.class); + // Ensure hard-coded alarms returned okay + assertThat(response, containsString("\"NE is not reachable\",")); + assertThat(response, containsString("\"Equipment Missing\",")); + } + + @Test + @Ignore + public void getAlarm() { + final WebResource rs = resource(); + final String response = rs.path("/alarms/1").get(String.class); + // Ensure hard-coded alarms returned okay + assertThat(response, containsString("\"NE is not reachable\",")); + assertThat(response, not(containsString("\"Equipment Missing\","))); + } + +} diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-full.json b/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-full.json new file mode 100644 index 00000000..215fc938 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-full.json @@ -0,0 +1,15 @@ +{ + "id": 10, + "deviceId": "of:123", + "description": "NE is not reachable", + "source": "none:none", + "timeRaised": 999, + "timeUpdated": 1111, + "timeUpdated": 1111, + "timeCleared": 2222, + "severity": "MAJOR", + "serviceAffecting": true, + "acknowledged": false, + "manuallyClearable": true, + "assignedUser": "foo" +} \ No newline at end of file diff --git a/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-minimal.json b/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-minimal.json new file mode 100644 index 00000000..4a24db09 --- /dev/null +++ b/framework/src/onos/apps/faultmanagement/fmweb/src/test/resources/org/onosproject/faultmanagement/web/alarm-minimal.json @@ -0,0 +1,12 @@ +{ + "id": 10, + "deviceId": "of:123", + "description": "NE is not reachable", + "source": "none:none", + "timeRaised": 999, + "timeUpdated": 1111, + "severity": "MAJOR", + "serviceAffecting": true, + "acknowledged": false, + "manuallyClearable": true +} \ No newline at end of file -- cgit 1.2.3-korg