blob: 10f2b57e4eecce0ac53d104252019a434849903b [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;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
Ray Milkeyb9a8a182015-01-20 14:15:35 -080049import com.google.common.collect.ImmutableSet;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080050
51/**
52 * Implementation of the JSON codec brokering service.
53 */
54@Component(immediate = true)
55@Service
56public class CodecManager implements CodecService {
57
58 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
59
60 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
61
62 @Activate
63 public void activate() {
64 codecs.clear();
65 registerCodec(Annotations.class, new AnnotationsCodec());
66 registerCodec(Device.class, new DeviceCodec());
67 registerCodec(Port.class, new PortCodec());
68 registerCodec(ConnectPoint.class, new ConnectPointCodec());
69 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -080070 registerCodec(Host.class, new HostCodec());
71 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080072 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
73 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -080074 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080075 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -080076 registerCodec(FlowEntry.class, new FlowEntryCodec());
77 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
78 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080079 registerCodec(Instruction.class, new InstructionCodec());
80 registerCodec(Criterion.class, new CriterionCodec());
81 registerCodec(Ethernet.class, new EthernetCodec());
Ray Milkeyb9a8a182015-01-20 14:15:35 -080082 registerCodec(Constraint.class, new ConstraintCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080083 log.info("Started");
84 }
85
86 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -080087 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080088 codecs.clear();
89 log.info("Stopped");
90 }
91
92 @Override
93 public Set<Class<?>> getCodecs() {
94 return ImmutableSet.copyOf(codecs.keySet());
95 }
96
97 @Override
98 @SuppressWarnings("unchecked")
99 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
100 return codecs.get(entityClass);
101 }
102
103 @Override
104 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
105 codecs.putIfAbsent(entityClass, codec);
106 }
107
108 @Override
109 public void unregisterCodec(Class<?> entityClass) {
110 codecs.remove(entityClass);
111 }
112
113}