Move definitions of classifiers and actions to
[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.groupbasedpolicy.sf.classifiers.EtherTypeClassifierDefinition;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
21
22 public abstract class ParamDerivator {
23
24     /**
25      * It is possible to derive missing parameters.
26      * <p>
27      * Examle:
28      * <p>
29      * Ether-type parameter is missing. Derivation policy might instruct: ipv4 and ipv6 flows need
30      * to be created if ether-type parameter is not present. In this case two derivations of
31      * {@code params} should be returned. One with ipv4 ether-type, the other with ipv6.
32      *
33      * @param params parameters inserted by user
34      * @return list of {@code params} updated with derived values
35      */
36     public abstract List<Map<String, ParameterValue>> deriveParameter(Map<String, ParameterValue> params);
37
38     public static final ParamDerivator ETHER_TYPE_DERIVATOR = new ParamDerivator() {
39
40         @Override
41         public List<Map<String, ParameterValue>> deriveParameter(Map<String, ParameterValue> params) {
42
43             if (!params.containsKey(EtherTypeClassifierDefinition.ETHERTYPE_PARAM)) {
44                 Map<String, ParameterValue> ipv4Params = new HashMap<>(params);
45                 Map<String, ParameterValue> ipv6Params = new HashMap<>(params);
46                 ipv4Params.put(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, new ParameterValueBuilder().setIntValue(FlowUtils.IPv4)
47                     .build());
48                 ipv6Params.put(EtherTypeClassifierDefinition.ETHERTYPE_PARAM, new ParameterValueBuilder().setIntValue(FlowUtils.IPv6)
49                     .build());
50                 List<Map<String, ParameterValue>> derivedParams = new ArrayList<>();
51                 derivedParams.add(ipv4Params);
52                 derivedParams.add(ipv6Params);
53                 return derivedParams;
54             }
55             return Collections.singletonList(params);
56         }
57     };
58 }