Merge "Bug 3051: Fix Unit Tests for IpPrefix."
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / AbstractTunnelType.java
1 /*
2  * Copyright (c) 2015 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.neutron.ovsdb;
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 import java.util.Map.Entry;
17
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
23
24 public abstract class AbstractTunnelType {
25     protected static final String REMOTE_IP_KEY = "remote_ip";
26     protected static final String REMOTE_IP_VALUE = "flow";
27     protected static final String VNID_KEY = "key";
28     protected static final String VNID_VALUE = "flow";
29
30     protected List<Options> createOptionsList(Map<String, String> optionsMap) {
31                 List<Options> options = new ArrayList<Options>();
32         OptionsBuilder ob = new OptionsBuilder();
33         for (Entry<String, String> entry: optionsMap.entrySet()) {
34             ob.setOption(entry.getKey());
35             ob.setValue(entry.getValue());
36             options.add(ob.build());
37         }
38         return Collections.unmodifiableList(options);
39         }
40
41     protected boolean hasTunnelOptions(OvsdbTerminationPointAugmentation tpAugmentation,
42             Map<String, String> optionsMap) {
43
44         Map<String, String> foundOpts = new HashMap<String, String>();
45         List<Options> options = tpAugmentation.getOptions();
46         if (options != null) {
47             for (Options opt: options) {
48                 // skip invalid options
49                 if (opt.getOption() == null
50                     || opt.getValue() == null) continue;
51
52                 if (optionsMap.containsKey(opt.getOption())
53                          && optionsMap.get(opt.getOption())
54                                    .equals(opt.getValue())) {
55                     foundOpts.put(opt.getOption(), opt.getValue());
56                 }
57             }
58             if ((foundOpts.size() == optionsMap.size())
59                     && (options.size() == foundOpts.size())) {
60                 return true;
61             }
62         }
63         return false;
64     }
65
66     /**
67      * Return the {@link List<Options>} valid for this tunnel type
68      *
69      * @return {@link List<Options>} for the tunnel, null if not supported
70      */
71     public abstract List<Options> getOptions();
72
73     /**
74      * Check if a TerminationPoint is a tunnel port that meets
75      * requirements
76      *
77      * @param tpAugmentation
78      * @return String of the tunnel port name (null if not found)
79      */
80     public abstract boolean isValidTunnelPort(OvsdbTerminationPointAugmentation tpAugmentation);
81
82     /**
83      * Return the type of tunnel.
84      *
85      * @return type of tunnel
86      */
87     public abstract Class<? extends TunnelTypeBase> getTunnelType();
88
89     /**
90      * Some {@link AbstractTunnelType} objects have a UDP port property.
91      * This getter method applies to those ports.
92      *
93      * @return {@link PortNumber} if the {@link AbstractTunnelType} supports it, null otherwise
94      */
95     public abstract PortNumber getPortNumber();
96
97     /**
98      * Get the prefix used to create the tunnel name for any tunnels of this type
99      *
100      * @return The tunnel prefix
101      */
102     public abstract String getTunnelPrefix();
103 }