Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / EtherTypeClassifier.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.List;
12 import java.util.Map;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.ParameterBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.IsRequired;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definition.Parameter.Type;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinition;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ClassifierDefinitionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
27
28 import com.google.common.collect.ImmutableList;
29
30 /**
31  * Match on the ether type of the traffic
32  */
33 public class EtherTypeClassifier extends Classifier {
34     public static final ClassifierDefinitionId ID = 
35             new ClassifierDefinitionId("6a48ab45-a462-429d-b18c-3a575b2c8bef");
36     protected static final String TYPE = "type";
37     protected static final ClassifierDefinition DEF = 
38             new ClassifierDefinitionBuilder()
39                 .setId(ID)
40                 .setName(new ClassifierName("ether_type"))
41                 .setDescription(new Description("Match on the ether type of the traffic"))
42                 .setParameter(ImmutableList.of(new ParameterBuilder()
43                     .setName(new ParameterName(TYPE))
44                     .setDescription(new Description("The ethertype to match against"))
45                     .setIsRequired(IsRequired.Required)
46                     .setType(Type.Int)
47                     .build()))
48                 .build();
49
50     @Override
51     public ClassifierDefinitionId getId() {
52         return ID;
53     }
54
55     @Override
56     public ClassifierDefinition getClassDef() {
57         return DEF;
58     }
59
60     @Override
61     public List<MatchBuilder> updateMatch(List<MatchBuilder> matches,
62                                           Map<String, Object> params) {
63         Object t = params.get(TYPE);
64         // XXX TODO generate exception and fail the match
65         if (t == null || !(t instanceof Long)) return matches;
66         Long type = (Long)t;
67         for (MatchBuilder match : matches) {
68             EthernetMatchBuilder em;
69             if (match.getEthernetMatch() != null)
70                 em = new EthernetMatchBuilder(match.getEthernetMatch());
71             else
72                 em = new EthernetMatchBuilder();
73             em.setEthernetType(new EthernetTypeBuilder()
74                 .setType(new EtherType(type)).build());
75             match.setEthernetMatch(em.build());
76         }
77         return matches;
78     }
79
80 }