blob: d1760773dc69f586d4df5c31f2998e230b72525b [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
Ray Milkeyb9a8a182015-01-20 14:15:35 -080018import java.util.Map;
19import java.util.Set;
20import java.util.concurrent.ConcurrentHashMap;
21
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Service;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080026import org.onlab.packet.Ethernet;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.codec.CodecService;
28import org.onosproject.codec.JsonCodec;
29import org.onosproject.net.Annotations;
30import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080032import org.onosproject.net.Host;
33import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Link;
35import org.onosproject.net.Port;
Ray Milkey4f5de002014-12-17 19:26:11 -080036import org.onosproject.net.flow.FlowEntry;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080039import org.onosproject.net.flow.criteria.Criterion;
40import org.onosproject.net.flow.instructions.Instruction;
41import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080042import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080043import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080044import org.onosproject.net.intent.Intent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080045import org.onosproject.net.intent.PointToPointIntent;
Ray Milkey82e50312015-01-22 17:01:42 -080046import org.onosproject.net.topology.Topology;
47import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Ray Milkeyb9a8a182015-01-20 14:15:35 -080051import com.google.common.collect.ImmutableSet;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080052
53/**
54 * Implementation of the JSON codec brokering service.
55 */
56@Component(immediate = true)
57@Service
58public class CodecManager implements CodecService {
59
60 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
61
62 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
63
64 @Activate
65 public void activate() {
66 codecs.clear();
67 registerCodec(Annotations.class, new AnnotationsCodec());
68 registerCodec(Device.class, new DeviceCodec());
69 registerCodec(Port.class, new PortCodec());
70 registerCodec(ConnectPoint.class, new ConnectPointCodec());
71 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080072 registerCodec(Host.class, new HostCodec());
73 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080074 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
75 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -080076 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080077 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -080078 registerCodec(FlowEntry.class, new FlowEntryCodec());
79 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
80 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080081 registerCodec(Instruction.class, new InstructionCodec());
82 registerCodec(Criterion.class, new CriterionCodec());
83 registerCodec(Ethernet.class, new EthernetCodec());
Ray Milkeyb9a8a182015-01-20 14:15:35 -080084 registerCodec(Constraint.class, new ConstraintCodec());
Ray Milkey82e50312015-01-22 17:01:42 -080085 registerCodec(Topology.class, new TopologyCodec());
86 registerCodec(TopologyCluster.class, new TopologyClusterCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080087 log.info("Started");
88 }
89
90 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080091 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080092 codecs.clear();
93 log.info("Stopped");
94 }
95
96 @Override
97 public Set<Class<?>> getCodecs() {
98 return ImmutableSet.copyOf(codecs.keySet());
99 }
100
101 @Override
102 @SuppressWarnings("unchecked")
103 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
104 return codecs.get(entityClass);
105 }
106
107 @Override
108 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
109 codecs.putIfAbsent(entityClass, codec);
110 }
111
112 @Override
113 public void unregisterCodec(Class<?> entityClass) {
114 codecs.remove(entityClass);
115 }
116
117}