blob: 592f6a8505424571c203c8f9db6b08acc0107650 [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;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080023import org.onlab.packet.Ethernet;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.codec.CodecService;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.net.Annotations;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080029import org.onosproject.net.Host;
30import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Link;
32import org.onosproject.net.Port;
Ray Milkey4f5de002014-12-17 19:26:11 -080033import org.onosproject.net.flow.FlowEntry;
34import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080036import org.onosproject.net.flow.criteria.Criterion;
37import org.onosproject.net.flow.instructions.Instruction;
38import org.onosproject.net.intent.ConnectivityIntent;
39import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080040import org.onosproject.net.intent.Intent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080041import org.onosproject.net.intent.PointToPointIntent;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
45import java.util.Map;
46import java.util.Set;
47import java.util.concurrent.ConcurrentHashMap;
48
49/**
50 * Implementation of the JSON codec brokering service.
51 */
52@Component(immediate = true)
53@Service
54public class CodecManager implements CodecService {
55
56 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
57
58 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
59
60 @Activate
61 public void activate() {
62 codecs.clear();
63 registerCodec(Annotations.class, new AnnotationsCodec());
64 registerCodec(Device.class, new DeviceCodec());
65 registerCodec(Port.class, new PortCodec());
66 registerCodec(ConnectPoint.class, new ConnectPointCodec());
67 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080068 registerCodec(Host.class, new HostCodec());
69 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080070 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
71 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -080072 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080073 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -080074 registerCodec(FlowEntry.class, new FlowEntryCodec());
75 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
76 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080077 registerCodec(Instruction.class, new InstructionCodec());
78 registerCodec(Criterion.class, new CriterionCodec());
79 registerCodec(Ethernet.class, new EthernetCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080080 log.info("Started");
81 }
82
83 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080084 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080085 codecs.clear();
86 log.info("Stopped");
87 }
88
89 @Override
90 public Set<Class<?>> getCodecs() {
91 return ImmutableSet.copyOf(codecs.keySet());
92 }
93
94 @Override
95 @SuppressWarnings("unchecked")
96 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
97 return codecs.get(entityClass);
98 }
99
100 @Override
101 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
102 codecs.putIfAbsent(entityClass, codec);
103 }
104
105 @Override
106 public void unregisterCodec(Class<?> entityClass) {
107 codecs.remove(entityClass);
108 }
109
110}