blob: a45ae876d2df5429aadea2bc5cb307a8cddc96ca [file] [log] [blame]
Ray Milkey2b217142014-12-15 09:24:24 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Ray Milkey2b217142014-12-15 09:24:24 -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 */
16package org.onosproject.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.NetworkResource;
21import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070022import org.onosproject.net.intent.IntentService;
23import org.onosproject.net.intent.IntentState;
Ray Milkey2b217142014-12-15 09:24:24 -080024
25import com.fasterxml.jackson.databind.node.ArrayNode;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Intent JSON codec.
32 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080033public final class IntentCodec extends JsonCodec<Intent> {
Ray Milkey2b217142014-12-15 09:24:24 -080034
35 @Override
36 public ObjectNode encode(Intent intent, CodecContext context) {
37 checkNotNull(intent, "Intent cannot be null");
38 final ObjectNode result = context.mapper().createObjectNode()
39 .put("type", intent.getClass().getSimpleName())
40 .put("id", intent.id().toString())
41 .put("appId", intent.appId().toString())
42 .put("details", intent.toString());
43
44 final ArrayNode jsonResources = result.putArray("resources");
45
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080046 for (final NetworkResource resource : intent.resources()) {
47 jsonResources.add(resource.toString());
Ray Milkey2b217142014-12-15 09:24:24 -080048 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070049
50 IntentService service = context.getService(IntentService.class);
51 IntentState state = service.getIntentState(intent.key());
Ray Milkeyccab22d2015-05-18 13:24:50 -070052 if (state != null) {
53 result.put("state", state.toString());
54 }
Ray Milkey1534f8d2015-05-13 15:42:50 -070055
Ray Milkey2b217142014-12-15 09:24:24 -080056 return result;
57 }
58}