blob: fe9d799569eb74c28067ccb1a9957f7617e2cd33 [file] [log] [blame]
SureshBRbf35c222015-11-05 12:35:01 +05301/*
2 * Copyright 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 */
16package org.onosproject.vtnweb.web;
17
18import java.util.Map;
19import java.util.concurrent.ConcurrentHashMap;
20
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.vtnrsc.FlowClassifier;
24import org.onosproject.vtnrsc.PortChain;
25import org.onosproject.vtnrsc.PortPair;
26import org.onosproject.vtnrsc.PortPairGroup;
27
28import com.fasterxml.jackson.databind.ObjectMapper;
29
30/**
31 * Mock codec context for use in codec unit tests.
32 */
33public class SfcCodecContext implements CodecContext {
34
35 private final ObjectMapper mapper = new ObjectMapper();
36 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
37
38 /**
39 * Constructs a new mock codec context.
40 */
41 public SfcCodecContext() {
42 codecs.clear();
43 registerCodec(PortPair.class, new PortPairCodec());
44 registerCodec(PortChain.class, new PortChainCodec());
45 registerCodec(PortPairGroup.class, new PortPairGroupCodec());
46 registerCodec(FlowClassifier.class, new FlowClassifierCodec());
47 }
48
49 @Override
50 public ObjectMapper mapper() {
51 return mapper;
52 }
53
54 @SuppressWarnings("unchecked")
55 @Override
56 public <T> T getService(Class<T> serviceClass) {
57 // TODO
58 return null;
59 }
60
61 /**
62 * Registers the specified JSON codec for the given entity class.
63 *
64 * @param entityClass entity class
65 * @param codec JSON codec
66 * @param <T> entity type
67 */
68 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
69 codecs.putIfAbsent(entityClass, codec);
70 }
71
72 @SuppressWarnings("unchecked")
73 @Override
74 public <T> JsonCodec<T> codec(Class<T> entityClass) {
75 return codecs.get(entityClass);
76 }
77}