8260c159cf059629ee516559233b4cca28f161ec
[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.rev130715.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
26     protected static final String DESTPORT_KEY = "dst_port";
27     protected static final String REMOTE_IP_KEY = "remote_ip";
28     protected static final String REMOTE_IP_VALUE = "flow";
29     protected static final String VNID_KEY = "key";
30     protected static final String VNID_VALUE = "flow";
31
32     protected List<Options> createOptionsList(Map<String, String> optionsMap) {
33         List<Options> options = new ArrayList<Options>();
34         OptionsBuilder ob = new OptionsBuilder();
35         for (Entry<String, String> entry : optionsMap.entrySet()) {
36             ob.setOption(entry.getKey());
37             ob.setValue(entry.getValue());
38             options.add(ob.build());
39         }
40         return Collections.unmodifiableList(options);
41     }
42
43     protected boolean hasTunnelOptions(OvsdbTerminationPointAugmentation tpAugmentation, Map<String, String> optionsMap) {
44
45         Map<String, String> foundOpts = new HashMap<String, String>();
46         List<Options> options = tpAugmentation.getOptions();
47         if (options != null) {
48             for (Options opt : options) {
49                 // skip invalid options
50                 if (opt.getOption() == null || opt.getValue() == null)
51                     continue;
52
53                 if (optionsMap.containsKey(opt.getOption()) && optionsMap.get(opt.getOption()).equals(opt.getValue())) {
54                     foundOpts.put(opt.getOption(), opt.getValue());
55                 }
56             }
57             if ((foundOpts.size() == optionsMap.size()) && (options.size() == foundOpts.size())) {
58                 return true;
59             }
60         }
61         return false;
62     }
63
64     protected String getDestPort(OvsdbTerminationPointAugmentation tpAugmentation) {
65         List<Options> options = tpAugmentation.getOptions();
66         if (options == null) {
67             return null;
68         }
69         for (Options opt : options) {
70             if (DESTPORT_KEY.equals(opt.getOption())) {
71                 return opt.getValue();
72             }
73         }
74         return null;
75     }
76
77     /**
78      * Return the list of {@link Options} valid for this tunnel type
79      *
80      * @return list of {@link Options} for the tunnel, null if not supported
81      */
82     public abstract List<Options> getOptions();
83
84     /**
85      * Check if a TerminationPoint is a tunnel port that meets
86      * requirements
87      *
88      * @param tpAugmentation the {@link OvsdbTerminationPointAugmentation}
89      * @return String of the tunnel port name (null if not found)
90      */
91     public abstract boolean isValidTunnelPort(OvsdbTerminationPointAugmentation tpAugmentation);
92
93     /**
94      * Return the type of tunnel.
95      *
96      * @return type of tunnel
97      */
98     public abstract Class<? extends TunnelTypeBase> getTunnelType();
99
100     /**
101      * Some {@link AbstractTunnelType} objects have a UDP port property.
102      * This getter method applies to those ports.
103      *
104      * @return {@link PortNumber} if the {@link AbstractTunnelType} supports it, null otherwise
105      */
106     public abstract PortNumber getPortNumber();
107
108     /**
109      * Get the prefix used to create the tunnel name for any tunnels of this type
110      *
111      * @return The tunnel prefix
112      */
113     public abstract String getTunnelPrefix();
114 }