[ONOS-3203] Optimize alarms codec registration.
Updating as advised at :
https://gerrit.onosproject.org/#/c/6180/3/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java@53
Copying the approach used in equivalent app at vtn/vtnweb/src/main/java/org/onosproject/vtnweb/web/VtnCodecRegistrator.java
Change-Id: Ifdcf5d69fe28070161e138217582126b135504a7
diff --git a/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodecRegistrator.java b/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodecRegistrator.java
new file mode 100644
index 0000000..b566e71
--- /dev/null
+++ b/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmCodecRegistrator.java
@@ -0,0 +1,50 @@
+/*
+ * 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 org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.codec.CodecService;
+import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of the JSON codec brokering service for FM app.
+ */
+@Component(immediate = true)
+public class AlarmCodecRegistrator {
+
+ private static final Logger log = LoggerFactory.getLogger(AlarmCodecRegistrator.class);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CodecService codecService;
+
+ @Activate
+ public void activate() {
+ codecService.registerCodec(Alarm.class, new AlarmCodec());
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ log.info("Stopped");
+ }
+}
diff --git a/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java b/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java
index c0b3d9b..d61d3e2 100644
--- a/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java
+++ b/apps/faultmanagement/fmweb/src/main/java/org/onosproject/faultmanagement/web/AlarmsWebResource.java
@@ -34,7 +34,6 @@
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;
@@ -50,7 +49,6 @@
private final Logger log = getLogger(getClass());
public AlarmsWebResource() {
- get(CodecService.class).registerCodec(Alarm.class, new AlarmCodec());
}
/**
diff --git a/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java b/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java
index 89886e8..6b80b72 100644
--- a/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java
+++ b/apps/faultmanagement/fmweb/src/test/java/org/onosproject/faultmanagement/web/AlarmCodecContext.java
@@ -15,15 +15,12 @@
*/
package org.onosproject.faultmanagement.web;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.CodecService;
import org.onosproject.codec.JsonCodec;
-
+import org.onosproject.codec.impl.CodecManager;
import com.fasterxml.jackson.databind.ObjectMapper;
-import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
/**
* Mock codec context for use in codec unit tests.
@@ -31,15 +28,16 @@
public class AlarmCodecContext implements CodecContext {
private final ObjectMapper mapper = new ObjectMapper();
- private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
+ private final CodecManager codecManager = new CodecManager();
+ private final AlarmCodecRegistrator manager = new AlarmCodecRegistrator();
/**
* Constructs a new mock codec context.
*/
public AlarmCodecContext() {
- codecs.clear();
- registerCodec(Alarm.class, new AlarmCodec());
-
+ codecManager.activate();
+ manager.codecService = codecManager;
+ manager.activate();
}
@Override
@@ -50,24 +48,20 @@
@SuppressWarnings("unchecked")
@Override
public <T> T getService(Class<T> serviceClass) {
- // TODO
return null;
}
- /**
- * Registers the specified JSON codec for the given entity class.
- *
- * @param entityClass entity class
- * @param codec JSON codec
- * @param <T> entity type
- */
- public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
- codecs.putIfAbsent(entityClass, codec);
- }
-
- @SuppressWarnings("unchecked")
@Override
public <T> JsonCodec<T> codec(Class<T> entityClass) {
- return codecs.get(entityClass);
+ return codecManager.getCodec(entityClass);
+ }
+
+ /**
+ * Get the codec manager.
+ *
+ * @return instance of codec manager
+ */
+ public CodecService codecManager() {
+ return codecManager;
}
}