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
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 org.apache.felix.scr.annotations.Activate;
19 +import org.apache.felix.scr.annotations.Component;
20 +import org.apache.felix.scr.annotations.Deactivate;
21 +import org.apache.felix.scr.annotations.Reference;
22 +import org.apache.felix.scr.annotations.ReferenceCardinality;
23 +import org.onosproject.codec.CodecService;
24 +import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
25 +
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +/**
30 + * Implementation of the JSON codec brokering service for FM app.
31 + */
32 +@Component(immediate = true)
33 +public class AlarmCodecRegistrator {
34 +
35 + private static final Logger log = LoggerFactory.getLogger(AlarmCodecRegistrator.class);
36 +
37 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 + protected CodecService codecService;
39 +
40 + @Activate
41 + public void activate() {
42 + codecService.registerCodec(Alarm.class, new AlarmCodec());
43 + log.info("Started");
44 + }
45 +
46 + @Deactivate
47 + public void deactivate() {
48 + log.info("Stopped");
49 + }
50 +}
...@@ -34,7 +34,6 @@ import javax.ws.rs.PathParam; ...@@ -34,7 +34,6 @@ import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces; 34 import javax.ws.rs.Produces;
35 import javax.ws.rs.QueryParam; 35 import javax.ws.rs.QueryParam;
36 import javax.ws.rs.core.MediaType; 36 import javax.ws.rs.core.MediaType;
37 -import org.onosproject.codec.CodecService;
38 import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; 37 import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
39 import org.slf4j.Logger; 38 import org.slf4j.Logger;
40 import static org.slf4j.LoggerFactory.getLogger; 39 import static org.slf4j.LoggerFactory.getLogger;
...@@ -50,7 +49,6 @@ public class AlarmsWebResource extends AbstractWebResource { ...@@ -50,7 +49,6 @@ public class AlarmsWebResource extends AbstractWebResource {
50 private final Logger log = getLogger(getClass()); 49 private final Logger log = getLogger(getClass());
51 50
52 public AlarmsWebResource() { 51 public AlarmsWebResource() {
53 - get(CodecService.class).registerCodec(Alarm.class, new AlarmCodec());
54 } 52 }
55 53
56 /** 54 /**
......
...@@ -15,15 +15,12 @@ ...@@ -15,15 +15,12 @@
15 */ 15 */
16 package org.onosproject.faultmanagement.web; 16 package org.onosproject.faultmanagement.web;
17 17
18 -import java.util.Map;
19 -import java.util.concurrent.ConcurrentHashMap;
20 -
21 import org.onosproject.codec.CodecContext; 18 import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.CodecService;
22 import org.onosproject.codec.JsonCodec; 20 import org.onosproject.codec.JsonCodec;
23 - 21 +import org.onosproject.codec.impl.CodecManager;
24 22
25 import com.fasterxml.jackson.databind.ObjectMapper; 23 import com.fasterxml.jackson.databind.ObjectMapper;
26 -import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
27 24
28 /** 25 /**
29 * Mock codec context for use in codec unit tests. 26 * Mock codec context for use in codec unit tests.
...@@ -31,15 +28,16 @@ import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; ...@@ -31,15 +28,16 @@ import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
31 public class AlarmCodecContext implements CodecContext { 28 public class AlarmCodecContext implements CodecContext {
32 29
33 private final ObjectMapper mapper = new ObjectMapper(); 30 private final ObjectMapper mapper = new ObjectMapper();
34 - private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); 31 + private final CodecManager codecManager = new CodecManager();
32 + private final AlarmCodecRegistrator manager = new AlarmCodecRegistrator();
35 33
36 /** 34 /**
37 * Constructs a new mock codec context. 35 * Constructs a new mock codec context.
38 */ 36 */
39 public AlarmCodecContext() { 37 public AlarmCodecContext() {
40 - codecs.clear(); 38 + codecManager.activate();
41 - registerCodec(Alarm.class, new AlarmCodec()); 39 + manager.codecService = codecManager;
42 - 40 + manager.activate();
43 } 41 }
44 42
45 @Override 43 @Override
...@@ -50,24 +48,20 @@ public class AlarmCodecContext implements CodecContext { ...@@ -50,24 +48,20 @@ public class AlarmCodecContext implements CodecContext {
50 @SuppressWarnings("unchecked") 48 @SuppressWarnings("unchecked")
51 @Override 49 @Override
52 public <T> T getService(Class<T> serviceClass) { 50 public <T> T getService(Class<T> serviceClass) {
53 - // TODO
54 return null; 51 return null;
55 } 52 }
56 53
54 + @Override
55 + public <T> JsonCodec<T> codec(Class<T> entityClass) {
56 + return codecManager.getCodec(entityClass);
57 + }
58 +
57 /** 59 /**
58 - * Registers the specified JSON codec for the given entity class. 60 + * Get the codec manager.
59 * 61 *
60 - * @param entityClass entity class 62 + * @return instance of codec manager
61 - * @param codec JSON codec
62 - * @param <T> entity type
63 */ 63 */
64 - public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { 64 + public CodecService codecManager() {
65 - codecs.putIfAbsent(entityClass, codec); 65 + return codecManager;
66 - }
67 -
68 - @SuppressWarnings("unchecked")
69 - @Override
70 - public <T> JsonCodec<T> codec(Class<T> entityClass) {
71 - return codecs.get(entityClass);
72 } 66 }
73 } 67 }
......