blob: 4b8683c387bdfbfd78808ea5914ddc49724c613b [file] [log] [blame]
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02003 *
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 */
16package org.onosproject.ofagent.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.Sets;
23import org.onosproject.codec.CodecContext;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.incubator.net.virtual.NetworkId;
Jovana Vuletac884b692017-11-28 16:52:35 +010026import org.onosproject.incubator.net.virtual.TenantId;
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020027import org.onosproject.ofagent.api.OFAgent;
28import org.onosproject.ofagent.api.OFController;
29import org.onosproject.ofagent.impl.DefaultOFAgent;
30
31import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34import static com.google.common.base.Preconditions.checkState;
35
36/**
37 * OpenFlow agent JSON codec.
38 */
39public final class OFAgentCodec extends JsonCodec<OFAgent> {
40
41 @Override
42 public ObjectNode encode(OFAgent ofAgent, CodecContext context) {
43 checkNotNull(ofAgent, "OFAgent cannot be null");
44
45 ObjectMapper mapper = context.mapper();
46 ObjectNode ofAgentNode = mapper.createObjectNode();
47 ofAgentNode
48 .put("networkId", ofAgent.networkId().toString())
Jovana Vuletac884b692017-11-28 16:52:35 +010049 .put("tenantId", ofAgent.tenantId().toString())
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020050 .put("state", ofAgent.state().toString());
51
52 ArrayNode controllers = mapper.createArrayNode();
53 ofAgent.controllers().forEach(ofController -> controllers.add((new OFControllerCodec()).encode(ofController,
54 context)));
55 ofAgentNode.set("controllers", controllers);
56
57 return ofAgentNode;
58 }
59
60 public OFAgent decode(ObjectNode json, CodecContext context) {
61 JsonNode networkId = json.get("networkId");
62 checkNotNull(networkId);
63
Jovana Vuletac884b692017-11-28 16:52:35 +010064 JsonNode tenantId = json.get("tenantId");
65 checkNotNull(tenantId);
66
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020067 checkNotNull(json.get("controllers"));
68 checkState(json.get("controllers").isArray());
69 Set<OFController> controllers = Sets.newHashSet();
70 json.get("controllers").forEach(jsonController -> controllers.add((new
71 OFControllerCodec()).decode((ObjectNode) jsonController, context)));
72
73 return DefaultOFAgent.builder()
74 .networkId(NetworkId.networkId(networkId.asLong()))
Jovana Vuletac884b692017-11-28 16:52:35 +010075 .tenantId(TenantId.tenantId(tenantId.asText()))
Jovana Vuletafe32db7d2017-05-01 12:18:00 +020076 .controllers(controllers)
77 .state(OFAgent.State.STOPPED)
78 .build();
79 }
80
81}