kmcpeake
Committed by Gerrit Code Review

[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
/*
* 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");
}
}
......@@ -34,7 +34,6 @@ 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;
......@@ -50,7 +49,6 @@ public class AlarmsWebResource extends AbstractWebResource {
private final Logger log = getLogger(getClass());
public AlarmsWebResource() {
get(CodecService.class).registerCodec(Alarm.class, new AlarmCodec());
}
/**
......
......@@ -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 @@ import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
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 @@ public class AlarmCodecContext implements CodecContext {
@SuppressWarnings("unchecked")
@Override
public <T> T getService(Class<T> serviceClass) {
// TODO
return null;
}
@Override
public <T> JsonCodec<T> codec(Class<T> entityClass) {
return codecManager.getCodec(entityClass);
}
/**
* Registers the specified JSON codec for the given entity class.
* Get the codec manager.
*
* @param entityClass entity class
* @param codec JSON codec
* @param <T> entity type
* @return instance of codec manager
*/
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);
public CodecService codecManager() {
return codecManager;
}
}
......