Phaneendra Manda
Committed by Gerrit Code Review

[ONOS-3114] Vtn codec registrator added

Change-Id: I500e8a9c5f67603cb287d8deab33cddf2e93cbaa
/*
* 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.vtnweb.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.vtnrsc.FlowClassifier;
import org.onosproject.vtnrsc.PortChain;
import org.onosproject.vtnrsc.PortPair;
import org.onosproject.vtnrsc.PortPairGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of the JSON codec brokering service for VTN app.
*/
@Component(immediate = true)
public class VtnCodecRegistrator {
private static Logger log = LoggerFactory.getLogger(VtnCodecRegistrator.class);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CodecService codecService;
@Activate
public void activate() {
codecService.registerCodec(PortPair.class, new PortPairCodec());
codecService.registerCodec(PortPairGroup.class, new PortPairGroupCodec());
codecService.registerCodec(FlowClassifier.class, new FlowClassifierCodec());
codecService.registerCodec(PortChain.class, new PortChainCodec());
log.info("Started");
}
@Deactivate
public void deactivate() {
log.info("Stopped");
}
}
......@@ -15,15 +15,10 @@
*/
package org.onosproject.vtnweb.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.vtnrsc.FlowClassifier;
import org.onosproject.vtnrsc.PortChain;
import org.onosproject.vtnrsc.PortPair;
import org.onosproject.vtnrsc.PortPairGroup;
import org.onosproject.codec.impl.CodecManager;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -33,17 +28,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public class SfcCodecContext implements CodecContext {
private final ObjectMapper mapper = new ObjectMapper();
private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
private final CodecManager codecManager = new CodecManager();
private final VtnCodecRegistrator manager = new VtnCodecRegistrator();
/**
* Constructs a new mock codec context.
*/
public SfcCodecContext() {
codecs.clear();
registerCodec(PortPair.class, new PortPairCodec());
registerCodec(PortChain.class, new PortChainCodec());
registerCodec(PortPairGroup.class, new PortPairGroupCodec());
registerCodec(FlowClassifier.class, new FlowClassifierCodec());
codecManager.activate();
manager.codecService = codecManager;
manager.activate();
}
@Override
......@@ -58,20 +52,17 @@ public class SfcCodecContext implements CodecContext {
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;
}
}
......