Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.stream.IntStream; |
| 20 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.intent.ConnectivityIntent; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.Intent; |
| 28 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Connectivity intent codec. |
| 37 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 38 | public final class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 39 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 40 | private static final String CONSTRAINTS = "constraints"; |
| 41 | private static final String SELECTOR = "selector"; |
| 42 | private static final String TREATMENT = "treatment"; |
| 43 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 44 | @Override |
| 45 | public ObjectNode encode(ConnectivityIntent intent, CodecContext context) { |
| 46 | checkNotNull(intent, "Connectivity intent cannot be null"); |
| 47 | |
| 48 | final JsonCodec<Intent> intentCodec = context.codec(Intent.class); |
| 49 | final ObjectNode result = intentCodec.encode(intent, context); |
| 50 | |
| 51 | if (intent.selector() != null) { |
| 52 | final JsonCodec<TrafficSelector> selectorCodec = |
| 53 | context.codec(TrafficSelector.class); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 54 | result.set(SELECTOR, selectorCodec.encode(intent.selector(), context)); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (intent.treatment() != null) { |
| 58 | final JsonCodec<TrafficTreatment> treatmentCodec = |
| 59 | context.codec(TrafficTreatment.class); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 60 | result.set(TREATMENT, treatmentCodec.encode(intent.treatment(), context)); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 63 | result.put(IntentCodec.PRIORITY, intent.priority()); |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 64 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 65 | if (intent.constraints() != null) { |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 66 | final ArrayNode jsonConstraints = result.putArray(CONSTRAINTS); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 67 | |
| 68 | if (intent.constraints() != null) { |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 69 | final JsonCodec<Constraint> constraintCodec = |
| 70 | context.codec(Constraint.class); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 71 | for (final Constraint constraint : intent.constraints()) { |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 72 | final ObjectNode constraintNode = |
| 73 | constraintCodec.encode(constraint, context); |
| 74 | jsonConstraints.add(constraintNode); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 79 | return result; |
| 80 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Extracts connectivity intent specific attributes from a JSON object |
| 84 | * and adds them to a builder. |
| 85 | * |
| 86 | * @param json root JSON object |
| 87 | * @param context code context |
| 88 | * @param builder builder to use for storing the attributes. Constraints, |
| 89 | * selector and treatment are modified by this call. |
| 90 | */ |
| 91 | public static void intentAttributes(ObjectNode json, CodecContext context, |
| 92 | ConnectivityIntent.Builder builder) { |
| 93 | JsonNode constraintsJson = json.get(CONSTRAINTS); |
| 94 | if (constraintsJson != null) { |
| 95 | JsonCodec<Constraint> constraintsCodec = context.codec(Constraint.class); |
| 96 | ArrayList<Constraint> constraints = new ArrayList<>(constraintsJson.size()); |
| 97 | IntStream.range(0, constraintsJson.size()) |
| 98 | .forEach(i -> constraints.add( |
| 99 | constraintsCodec.decode(get(constraintsJson, i), |
| 100 | context))); |
| 101 | builder.constraints(constraints); |
| 102 | } |
| 103 | |
| 104 | ObjectNode selectorJson = get(json, SELECTOR); |
| 105 | if (selectorJson != null) { |
| 106 | JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class); |
| 107 | TrafficSelector selector = selectorCodec.decode(selectorJson, context); |
| 108 | builder.selector(selector); |
| 109 | } |
| 110 | |
| 111 | ObjectNode treatmentJson = get(json, TREATMENT); |
| 112 | if (treatmentJson != null) { |
| 113 | JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class); |
| 114 | TrafficTreatment treatment = treatmentCodec.decode(treatmentJson, context); |
| 115 | builder.treatment(treatment); |
| 116 | } |
| 117 | } |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 118 | } |