blob: 5cfac906e10143046f907a9df7522dd798e59d1e [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08003 *
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
Jian Li72315152015-12-10 17:20:43 -080018import com.codahale.metrics.Metric;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070019import com.google.common.collect.ImmutableSet;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Service;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080024import org.onlab.packet.Ethernet;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070025import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.codec.CodecService;
27import org.onosproject.codec.JsonCodec;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import org.onosproject.core.Application;
Claudine Chiufb8b8162016-04-01 23:50:51 +000029import org.onosproject.incubator.net.virtual.TenantId;
30import org.onosproject.incubator.net.virtual.VirtualDevice;
31import org.onosproject.incubator.net.virtual.VirtualNetwork;
32import org.onosproject.incubator.net.virtual.VirtualPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Annotations;
34import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.Device;
Ray Milkey1f95bd32014-12-10 11:11:00 -080036import org.onosproject.net.Host;
37import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.Link;
Ray Milkey19ffea32015-01-28 10:03:06 -080039import org.onosproject.net.Path;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.Port;
Jian Liecb3c0f2015-12-15 10:07:49 -080041import org.onosproject.net.device.PortStatistics;
Ray Milkey39616f32015-05-14 15:43:00 -070042import org.onosproject.net.driver.Driver;
Ray Milkey4f5de002014-12-17 19:26:11 -080043import org.onosproject.net.flow.FlowEntry;
Ray Milkeyd43fe452015-05-29 09:35:12 -070044import org.onosproject.net.flow.FlowRule;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070045import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkey4f5de002014-12-17 19:26:11 -080046import org.onosproject.net.flow.TrafficSelector;
47import org.onosproject.net.flow.TrafficTreatment;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080048import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.instructions.Instruction;
Jian Li8bcef8b2016-01-06 11:35:53 -080050import org.onosproject.net.flowobjective.FilteringObjective;
51import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.NextObjective;
Ray Milkey39616f32015-05-14 15:43:00 -070053import org.onosproject.net.group.Group;
54import org.onosproject.net.group.GroupBucket;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080055import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080056import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080057import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080058import org.onosproject.net.intent.Intent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080059import org.onosproject.net.intent.PointToPointIntent;
Brian Stankeb9170d92016-02-19 14:18:42 -050060import org.onosproject.net.key.DeviceKey;
Jonathan Hart07eb0412016-02-08 16:42:29 -080061import org.onosproject.net.mcast.McastRoute;
Jian Li5c411232015-12-16 15:29:16 -080062import org.onosproject.net.meter.Band;
63import org.onosproject.net.meter.Meter;
Jian Li95678962016-01-26 17:17:52 -080064import org.onosproject.net.meter.MeterRequest;
Jian Lib68b9a82016-02-23 10:25:54 +090065import org.onosproject.net.region.Region;
Ray Milkeyb9af9462015-07-17 11:12:09 -070066import org.onosproject.net.statistic.Load;
Ray Milkey82e50312015-01-22 17:01:42 -080067import org.onosproject.net.topology.Topology;
68import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080069import org.slf4j.Logger;
70import org.slf4j.LoggerFactory;
71
Thomas Vachuskade563cf2015-04-01 00:28:50 -070072import java.util.Map;
73import java.util.Set;
74import java.util.concurrent.ConcurrentHashMap;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080075
Heedo Kang4a47a302016-02-29 17:40:23 +090076import static org.onosproject.security.AppGuard.checkPermission;
77import static org.onosproject.security.AppPermission.Type.*;
78
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080079/**
80 * Implementation of the JSON codec brokering service.
81 */
82@Component(immediate = true)
83@Service
84public class CodecManager implements CodecService {
85
86 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
87
88 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
89
90 @Activate
91 public void activate() {
92 codecs.clear();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 registerCodec(Application.class, new ApplicationCodec());
Thomas Vachuskade563cf2015-04-01 00:28:50 -070094 registerCodec(ControllerNode.class, new ControllerNodeCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080095 registerCodec(Annotations.class, new AnnotationsCodec());
96 registerCodec(Device.class, new DeviceCodec());
97 registerCodec(Port.class, new PortCodec());
98 registerCodec(ConnectPoint.class, new ConnectPointCodec());
99 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -0800100 registerCodec(Host.class, new HostCodec());
101 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800102 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
103 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -0800104 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800105 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800106 registerCodec(FlowEntry.class, new FlowEntryCodec());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700107 registerCodec(FlowRule.class, new FlowRuleCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800108 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
109 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800110 registerCodec(Instruction.class, new InstructionCodec());
111 registerCodec(Criterion.class, new CriterionCodec());
112 registerCodec(Ethernet.class, new EthernetCodec());
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800113 registerCodec(Constraint.class, new ConstraintCodec());
Ray Milkey82e50312015-01-22 17:01:42 -0800114 registerCodec(Topology.class, new TopologyCodec());
115 registerCodec(TopologyCluster.class, new TopologyClusterCodec());
Ray Milkey19ffea32015-01-28 10:03:06 -0800116 registerCodec(Path.class, new PathCodec());
Ray Milkey39616f32015-05-14 15:43:00 -0700117 registerCodec(Group.class, new GroupCodec());
118 registerCodec(Driver.class, new DriverCodec());
119 registerCodec(GroupBucket.class, new GroupBucketCodec());
Ray Milkeyb9af9462015-07-17 11:12:09 -0700120 registerCodec(Load.class, new LoadCodec());
Jian Li95678962016-01-26 17:17:52 -0800121 registerCodec(MeterRequest.class, new MeterRequestCodec());
Jian Li5c411232015-12-16 15:29:16 -0800122 registerCodec(Meter.class, new MeterCodec());
123 registerCodec(Band.class, new MeterBandCodec());
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700124 registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
Srikanth Vavilapallid120f5c2015-11-24 14:15:01 -0800125 registerCodec(PortStatistics.class, new PortStatisticsCodec());
Jian Li72315152015-12-10 17:20:43 -0800126 registerCodec(Metric.class, new MetricCodec());
Jian Li8bcef8b2016-01-06 11:35:53 -0800127 registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
128 registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
129 registerCodec(NextObjective.class, new NextObjectiveCodec());
Jonathan Hart07eb0412016-02-08 16:42:29 -0800130 registerCodec(McastRoute.class, new McastRouteCodec());
Brian Stankeb9170d92016-02-19 14:18:42 -0500131 registerCodec(DeviceKey.class, new DeviceKeyCodec());
Jian Lib68b9a82016-02-23 10:25:54 +0900132 registerCodec(Region.class, new RegionCodec());
Claudine Chiufb8b8162016-04-01 23:50:51 +0000133 registerCodec(TenantId.class, new TenantIdCodec());
134 registerCodec(VirtualNetwork.class, new VirtualNetworkCodec());
135 registerCodec(VirtualDevice.class, new VirtualDeviceCodec());
136 registerCodec(VirtualPort.class, new VirtualPortCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800137 log.info("Started");
138 }
139
140 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -0800141 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800142 codecs.clear();
143 log.info("Stopped");
144 }
145
146 @Override
147 public Set<Class<?>> getCodecs() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900148 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800149 return ImmutableSet.copyOf(codecs.keySet());
150 }
151
152 @Override
153 @SuppressWarnings("unchecked")
154 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900155 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800156 return codecs.get(entityClass);
157 }
158
159 @Override
160 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900161 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800162 codecs.putIfAbsent(entityClass, codec);
163 }
164
165 @Override
166 public void unregisterCodec(Class<?> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900167 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800168 codecs.remove(entityClass);
169 }
170
171}