Bug 3269 - Neutron External Gateway not functioning with floatingIP
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / EndpointHelper.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 package org.opendaylight.groupbasedpolicy.neutron.ovsdb.util;
9
10 import static org.opendaylight.groupbasedpolicy.util.DataStoreHelper.readFromDs;
11 import static org.opendaylight.groupbasedpolicy.util.IidFactory.endpointIid;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
18 import org.opendaylight.groupbasedpolicy.util.IidFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.common.base.Optional;
30
31 public class EndpointHelper {
32     private static final Logger LOG = LoggerFactory.getLogger(EndpointHelper.class);
33
34     /**
35      * Look up the {@link Endpoint} from the Endpoint Registry.
36      *
37      * @param epKey The {@link EndpointKey} to look up
38      * @param dataBroker The {@link DataBroker} to use for the transaction
39      * @return The corresponding {@link Endpoint}, null if not found
40      */
41     public static Endpoint lookupEndpoint(EndpointKey epKey, ReadOnlyTransaction transaction) {
42
43         Optional<Endpoint> optionalEp = readFromDs(LogicalDatastoreType.OPERATIONAL, endpointIid(epKey.getL2Context(),epKey.getMacAddress()), transaction );
44         if (optionalEp.isPresent()) {
45             return optionalEp.get();
46         }
47         return null;
48     }
49
50     /**
51      * Updates an {@link Endpoint} location based on OVSDB Termination point notification.
52      *
53      * Note this updates the datastore directly. It does not use the Endpoint RPC, as this has
54      * unfortunate side-effects on EndpointL3 augmentations.
55      *
56      * @param endpoint
57      * @param nodeIdString
58      * @param nodeConnectorIdString
59      * @param rwTx
60      */
61     public static void updateEndpointWithLocation(Endpoint endpoint, String nodeIdString,
62         String nodeConnectorIdString, ReadWriteTransaction rwTx) {
63
64         NodeId invNodeId = new NodeId(nodeIdString);
65         NodeConnectorId ncId = new NodeConnectorId(nodeConnectorIdString);
66
67         OfOverlayContext ofc = endpoint.getAugmentation(OfOverlayContext.class);
68         OfOverlayContextBuilder ofcBuilder = new OfOverlayContextBuilder(ofc).setNodeConnectorId(ncId).setNodeId(invNodeId);
69         EndpointBuilder epBuilder = new EndpointBuilder(endpoint);
70         epBuilder.addAugmentation(OfOverlayContext.class, ofcBuilder.build());
71         Endpoint newEp = epBuilder.build();
72         rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.endpointIid(newEp.getL2Context(), newEp.getMacAddress()), newEp);
73         DataStoreHelper.submitToDs(rwTx);
74
75     }
76
77 }