blob: 6980a349b5ec14b393f75f843aaaa5d9cfc59605 [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec.impl;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080017
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.codec.CodecService;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.net.Annotations;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080028import org.onosproject.net.Host;
29import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Link;
31import org.onosproject.net.Port;
Ray Milkey4f5de002014-12-17 19:26:11 -080032import org.onosproject.net.flow.FlowEntry;
33import org.onosproject.net.flow.TrafficSelector;
34import org.onosproject.net.flow.TrafficTreatment;
Ray Milkey2b217142014-12-15 09:24:24 -080035import org.onosproject.net.intent.Intent;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
39import java.util.Map;
40import java.util.Set;
41import java.util.concurrent.ConcurrentHashMap;
42
43/**
44 * Implementation of the JSON codec brokering service.
45 */
46@Component(immediate = true)
47@Service
48public class CodecManager implements CodecService {
49
50 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
51
52 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
53
54 @Activate
55 public void activate() {
56 codecs.clear();
57 registerCodec(Annotations.class, new AnnotationsCodec());
58 registerCodec(Device.class, new DeviceCodec());
59 registerCodec(Port.class, new PortCodec());
60 registerCodec(ConnectPoint.class, new ConnectPointCodec());
61 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080062 registerCodec(Host.class, new HostCodec());
63 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkey2b217142014-12-15 09:24:24 -080064 registerCodec(Intent.class, new IntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -080065 registerCodec(FlowEntry.class, new FlowEntryCodec());
66 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
67 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080068 log.info("Started");
69 }
70
71 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080072 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080073 codecs.clear();
74 log.info("Stopped");
75 }
76
77 @Override
78 public Set<Class<?>> getCodecs() {
79 return ImmutableSet.copyOf(codecs.keySet());
80 }
81
82 @Override
83 @SuppressWarnings("unchecked")
84 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
85 return codecs.get(entityClass);
86 }
87
88 @Override
89 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
90 codecs.putIfAbsent(entityClass, codec);
91 }
92
93 @Override
94 public void unregisterCodec(Class<?> entityClass) {
95 codecs.remove(entityClass);
96 }
97
98}