blob: a6bb8bf135584c677961461d109c596e4cebf49d [file] [log] [blame]
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.osgi.service.component.annotations.Activate;
21import org.osgi.service.component.annotations.Component;
22import org.osgi.service.component.annotations.Deactivate;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080023import org.onlab.packet.Ethernet;
Thomas Vachuskade563cf2015-04-01 00:28:50 -070024import org.onosproject.cluster.ControllerNode;
Jian Lib68a2b02016-05-02 11:23:32 -070025import org.onosproject.cluster.RoleInfo;
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;
Jian Lie1c1c8d2016-05-09 16:24:40 -070029import org.onosproject.core.ApplicationId;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080030import org.onosproject.net.TenantId;
Jian Lib68a2b02016-05-02 11:23:32 -070031import org.onosproject.mastership.MastershipTerm;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Annotations;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.Device;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070035import org.onosproject.net.DisjointPath;
36import org.onosproject.net.FilteredConnectPoint;
Ray Milkey1f95bd32014-12-10 11:11:00 -080037import org.onosproject.net.Host;
38import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.Link;
Jian Lib68a2b02016-05-02 11:23:32 -070040import org.onosproject.net.MastershipRole;
Ray Milkey19ffea32015-01-28 10:03:06 -080041import org.onosproject.net.Path;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.Port;
Yuta HIGUCHIa255bb42016-11-03 16:04:08 -070043import org.onosproject.net.behaviour.protection.TransportEndpointDescription;
Jian Liecb3c0f2015-12-15 10:07:49 -080044import org.onosproject.net.device.PortStatistics;
Ray Milkey39616f32015-05-14 15:43:00 -070045import org.onosproject.net.driver.Driver;
Ray Milkey4f5de002014-12-17 19:26:11 -080046import org.onosproject.net.flow.FlowEntry;
Ray Milkeyd43fe452015-05-29 09:35:12 -070047import org.onosproject.net.flow.FlowRule;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070048import org.onosproject.net.flow.TableStatisticsEntry;
Ray Milkey4f5de002014-12-17 19:26:11 -080049import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080051import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.instructions.Instruction;
Jian Li8bcef8b2016-01-06 11:35:53 -080053import org.onosproject.net.flowobjective.FilteringObjective;
54import org.onosproject.net.flowobjective.ForwardingObjective;
55import org.onosproject.net.flowobjective.NextObjective;
Ray Milkey39616f32015-05-14 15:43:00 -070056import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupBucket;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080058import org.onosproject.net.intent.ConnectivityIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080059import org.onosproject.net.intent.Constraint;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080060import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey2b217142014-12-15 09:24:24 -080061import org.onosproject.net.intent.Intent;
Carmelo Cascone87892e22017-11-13 16:01:29 -080062import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080063import org.onosproject.net.intent.PointToPointIntent;
Chiara Contolia8f69ff2016-07-28 01:06:07 +090064import org.onosproject.net.intent.SinglePointToMultiPointIntent;
dvaddire95c84ed2017-06-14 15:42:24 +053065import org.onosproject.net.intent.util.IntentMiniSummary;
Brian Stankeb9170d92016-02-19 14:18:42 -050066import org.onosproject.net.key.DeviceKey;
Jonathan Hart07eb0412016-02-08 16:42:29 -080067import org.onosproject.net.mcast.McastRoute;
Jian Li5c411232015-12-16 15:29:16 -080068import org.onosproject.net.meter.Band;
69import org.onosproject.net.meter.Meter;
Jian Li95678962016-01-26 17:17:52 -080070import org.onosproject.net.meter.MeterRequest;
Claudine Chiu1f036b82017-03-09 16:45:56 -050071import org.onosproject.net.packet.PacketRequest;
Yi Tsenga87b40c2017-09-10 00:59:03 -070072import org.onosproject.net.pi.model.PiActionModel;
73import org.onosproject.net.pi.model.PiActionParamModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080074import org.onosproject.net.pi.model.PiMatchFieldModel;
Yi Tsenga87b40c2017-09-10 00:59:03 -070075import org.onosproject.net.pi.model.PiPipeconf;
76import org.onosproject.net.pi.model.PiPipelineModel;
Yi Tsenga87b40c2017-09-10 00:59:03 -070077import org.onosproject.net.pi.model.PiTableModel;
Jian Lib68b9a82016-02-23 10:25:54 +090078import org.onosproject.net.region.Region;
Ray Milkeyb9af9462015-07-17 11:12:09 -070079import org.onosproject.net.statistic.Load;
Ray Milkey82e50312015-01-22 17:01:42 -080080import org.onosproject.net.topology.Topology;
81import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080082import org.slf4j.Logger;
83import org.slf4j.LoggerFactory;
84
Thomas Vachuskade563cf2015-04-01 00:28:50 -070085import java.util.Map;
86import java.util.Set;
87import java.util.concurrent.ConcurrentHashMap;
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080088
Heedo Kang4a47a302016-02-29 17:40:23 +090089import static org.onosproject.security.AppGuard.checkPermission;
Jonathan Hart8338b8d2017-08-15 15:46:14 -070090import static org.onosproject.security.AppPermission.Type.CODEC_READ;
91import static org.onosproject.security.AppPermission.Type.CODEC_WRITE;
Heedo Kang4a47a302016-02-29 17:40:23 +090092
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080093/**
94 * Implementation of the JSON codec brokering service.
95 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070096@Component(immediate = true, service = CodecService.class)
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080097public class CodecManager implements CodecService {
98
99 private static Logger log = LoggerFactory.getLogger(CodecManager.class);
100
101 private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
102
103 @Activate
104 public void activate() {
105 codecs.clear();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800106 registerCodec(Application.class, new ApplicationCodec());
Jian Lie1c1c8d2016-05-09 16:24:40 -0700107 registerCodec(ApplicationId.class, new ApplicationIdCodec());
Thomas Vachuskade563cf2015-04-01 00:28:50 -0700108 registerCodec(ControllerNode.class, new ControllerNodeCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800109 registerCodec(Annotations.class, new AnnotationsCodec());
110 registerCodec(Device.class, new DeviceCodec());
111 registerCodec(Port.class, new PortCodec());
112 registerCodec(ConnectPoint.class, new ConnectPointCodec());
113 registerCodec(Link.class, new LinkCodec());
Ray Milkey1f95bd32014-12-10 11:11:00 -0800114 registerCodec(Host.class, new HostCodec());
115 registerCodec(HostLocation.class, new HostLocationCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800116 registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
dvaddire95c84ed2017-06-14 15:42:24 +0530117 registerCodec(IntentMiniSummary.class, new IntentMiniSummaryCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800118 registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
Chiara Contolia8f69ff2016-07-28 01:06:07 +0900119 registerCodec(SinglePointToMultiPointIntent.class, new SinglePointToMultiPointIntentCodec());
nassima toumi95761312017-05-23 14:00:33 +0300120 registerCodec(MultiPointToSinglePointIntent.class, new MultiPointToSinglePointIntentCodec());
Ray Milkey2b217142014-12-15 09:24:24 -0800121 registerCodec(Intent.class, new IntentCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800122 registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800123 registerCodec(FlowEntry.class, new FlowEntryCodec());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700124 registerCodec(FlowRule.class, new FlowRuleCodec());
Ray Milkey4f5de002014-12-17 19:26:11 -0800125 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
126 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800127 registerCodec(Instruction.class, new InstructionCodec());
128 registerCodec(Criterion.class, new CriterionCodec());
129 registerCodec(Ethernet.class, new EthernetCodec());
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800130 registerCodec(Constraint.class, new ConstraintCodec());
Ray Milkey82e50312015-01-22 17:01:42 -0800131 registerCodec(Topology.class, new TopologyCodec());
132 registerCodec(TopologyCluster.class, new TopologyClusterCodec());
Ray Milkey19ffea32015-01-28 10:03:06 -0800133 registerCodec(Path.class, new PathCodec());
Jayasree Ghoshd3ff5402016-08-17 18:41:19 +0530134 registerCodec(DisjointPath.class, new DisjointPathCodec());
Ray Milkey39616f32015-05-14 15:43:00 -0700135 registerCodec(Group.class, new GroupCodec());
136 registerCodec(Driver.class, new DriverCodec());
137 registerCodec(GroupBucket.class, new GroupBucketCodec());
Ray Milkeyb9af9462015-07-17 11:12:09 -0700138 registerCodec(Load.class, new LoadCodec());
Jian Li95678962016-01-26 17:17:52 -0800139 registerCodec(MeterRequest.class, new MeterRequestCodec());
Jian Li5c411232015-12-16 15:29:16 -0800140 registerCodec(Meter.class, new MeterCodec());
141 registerCodec(Band.class, new MeterBandCodec());
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700142 registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
Srikanth Vavilapallid120f5c2015-11-24 14:15:01 -0800143 registerCodec(PortStatistics.class, new PortStatisticsCodec());
Jian Li72315152015-12-10 17:20:43 -0800144 registerCodec(Metric.class, new MetricCodec());
Jian Li8bcef8b2016-01-06 11:35:53 -0800145 registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
146 registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
147 registerCodec(NextObjective.class, new NextObjectiveCodec());
Jonathan Hart07eb0412016-02-08 16:42:29 -0800148 registerCodec(McastRoute.class, new McastRouteCodec());
Brian Stankeb9170d92016-02-19 14:18:42 -0500149 registerCodec(DeviceKey.class, new DeviceKeyCodec());
Jian Lib68b9a82016-02-23 10:25:54 +0900150 registerCodec(Region.class, new RegionCodec());
Claudine Chiufb8b8162016-04-01 23:50:51 +0000151 registerCodec(TenantId.class, new TenantIdCodec());
Jian Lib68a2b02016-05-02 11:23:32 -0700152 registerCodec(MastershipTerm.class, new MastershipTermCodec());
153 registerCodec(MastershipRole.class, new MastershipRoleCodec());
154 registerCodec(RoleInfo.class, new RoleInfoCodec());
Yuta HIGUCHI776f0742016-11-03 15:51:14 -0700155 registerCodec(FilteredConnectPoint.class, new FilteredConnectPointCodec());
Yuta HIGUCHIa255bb42016-11-03 16:04:08 -0700156 registerCodec(TransportEndpointDescription.class, new TransportEndpointDescriptionCodec());
Claudine Chiu1f036b82017-03-09 16:45:56 -0500157 registerCodec(PacketRequest.class, new PacketRequestCodec());
Yi Tsenga87b40c2017-09-10 00:59:03 -0700158 registerCodec(PiActionModel.class, new PiActionModelCodec());
Yi Tsenga87b40c2017-09-10 00:59:03 -0700159 registerCodec(PiPipelineModel.class, new PiPipelineModelCodec());
160 registerCodec(PiPipeconf.class, new PiPipeconfCodec());
161 registerCodec(PiTableModel.class, new PiTableModelCodec());
Carmelo Cascone87892e22017-11-13 16:01:29 -0800162 registerCodec(PiMatchFieldModel.class, new PiMatchFieldModelCodec());
Yi Tsenga87b40c2017-09-10 00:59:03 -0700163 registerCodec(PiActionParamModel.class, new PiActionParamModelCodec());
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800164 log.info("Started");
165 }
166
167 @Deactivate
Ray Milkey1f95bd32014-12-10 11:11:00 -0800168 public void deactivate() {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800169 codecs.clear();
170 log.info("Stopped");
171 }
172
173 @Override
174 public Set<Class<?>> getCodecs() {
Heedo Kang4a47a302016-02-29 17:40:23 +0900175 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800176 return ImmutableSet.copyOf(codecs.keySet());
177 }
178
179 @Override
180 @SuppressWarnings("unchecked")
181 public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900182 checkPermission(CODEC_READ);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800183 return codecs.get(entityClass);
184 }
185
186 @Override
187 public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900188 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800189 codecs.putIfAbsent(entityClass, codec);
190 }
191
192 @Override
193 public void unregisterCodec(Class<?> entityClass) {
Heedo Kang4a47a302016-02-29 17:40:23 +0900194 checkPermission(CODEC_WRITE);
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -0800195 codecs.remove(entityClass);
196 }
197
198}