99b60df9dfb27c2dd5a5aedcecdfd2b9823622e5
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / ParamDerivator.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * 
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
20
21 public abstract class ParamDerivator {
22
23     /**
24      * It is possible to derive missing parameters.
25      * <p>
26      * Examle:
27      * <p>
28      * Ether-type parameter is missing. Derivation policy might instruct: ipv4 and ipv6 flows need
29      * to be created if ether-type parameter is not present. In this case two derivations of
30      * {@code params} should be returned. One with ipv4 ether-type, the other with ipv6.
31      *
32      * @param params parameters inserted by user
33      * @return list of {@code params} updated with derived values
34      */
35     public abstract List<Map<String, ParameterValue>> deriveParameter(Map<String, ParameterValue> params);
36
37     public static final ParamDerivator ETHER_TYPE_DERIVATOR = new ParamDerivator() {
38
39         @Override
40         public List<Map<String, ParameterValue>> deriveParameter(Map<String, ParameterValue> params) {
41
42             if (!params.containsKey(EtherTypeClassifier.ETHER_TYPE)) {
43                 Map<String, ParameterValue> ipv4Params = new HashMap<>(params);
44                 Map<String, ParameterValue> ipv6Params = new HashMap<>(params);
45                 ipv4Params.put(EtherTypeClassifier.ETHER_TYPE, new ParameterValueBuilder().setIntValue(FlowUtils.IPv4)
46                     .build());
47                 ipv6Params.put(EtherTypeClassifier.ETHER_TYPE, new ParameterValueBuilder().setIntValue(FlowUtils.IPv6)
48                     .build());
49                 List<Map<String, ParameterValue>> derivedParams = new ArrayList<>();
50                 derivedParams.add(ipv4Params);
51                 derivedParams.add(ipv6Params);
52                 return derivedParams;
53             }
54             return Collections.singletonList(params);
55         }
56     };
57 }