Added new FlowTables (NATMapper, ExternalMapper)
[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 java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.UnregisterEndpointInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L2Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.unregister.endpoint.input.L3Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextInputBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import com.google.common.base.Optional;
46 import com.google.common.collect.ImmutableList;
47
48 public class EndpointHelper {
49     private static final Logger LOG = LoggerFactory.getLogger(EndpointHelper.class);
50
51     /**
52      * Look up the {@link Endpoint} from the Endpoint Registry.
53      *
54      * @param epKey The {@link EndpointKey} to look up
55      * @param dataBroker The {@link DataBroker} to use for the transaction
56      * @return The corresponding {@link Endpoint}, null if not found
57      */
58     public static Endpoint lookupEndpoint(EndpointKey epKey, ReadOnlyTransaction transaction) {
59
60         Optional<Endpoint> optionalEp = readFromDs(LogicalDatastoreType.OPERATIONAL, endpointIid(epKey.getL2Context(),epKey.getMacAddress()), transaction );
61         if (optionalEp.isPresent()) {
62             return optionalEp.get();
63         }
64         return null;
65     }
66
67     /**
68      * Update a {@link Endpoint} in the Endpoint Repository with the
69      * location information and port name in the OfOverlay augmentation.
70      * It looks up the {@link EndpointKey} in a data store maintained by the
71      * neutron-mapper, and uses that to look up the {@link Endpoint}, and
72      * then uses RPCs to delete then add the updated {@link Endpoint}
73      *
74      * @param epKey The {@link EndpointKey}
75      * @param nodeIdString
76      * @param ovsdbBridge
77      */
78     public static void updateEndpointWithLocation(Endpoint endpoint, String nodeIdString,
79             String nodeConnectorIdString, String portName, EndpointService endpointService) {
80         EndpointBuilder epBuilder = new EndpointBuilder(endpoint);
81         UnregisterEndpointInput unregisterEpRpcInput = createUnregisterEndpointInput(endpoint);
82         RegisterEndpointInput registerEpRpcInput =
83             createRegisterEndpointInput(epBuilder.build(),
84                                         portName,
85                                         nodeIdString,
86                                         nodeConnectorIdString);
87         try {
88             RpcResult<Void> rpcResult = endpointService.unregisterEndpoint(unregisterEpRpcInput).get();
89             if (!rpcResult.isSuccessful()) {
90                 LOG.warn("Illegal state - RPC unregisterEndpoint failed. Input of RPC: {}", unregisterEpRpcInput);
91                 return;
92             }
93             rpcResult = endpointService.registerEndpoint(registerEpRpcInput).get();
94             if (!rpcResult.isSuccessful()) {
95                 LOG.warn("Illegal state - RPC registerEndpoint failed. Input of RPC: {}", registerEpRpcInput);
96                 return;
97             }
98         } catch (InterruptedException | ExecutionException e) {
99             LOG.error("addPort - RPC invocation failed.", e);
100             return;
101         }
102     }
103
104     /**
105      * Create the {@link UnregisterEndpointInput} state needed to remove
106      * the existing {@link Endpoint} from the Endpoint Registry.
107      *
108      * @param ep The {@link Endpoint} to remove from the registry
109      * @return The {@link UnregisterEndpointInput}, null if failure
110      */
111     public static UnregisterEndpointInput createUnregisterEndpointInput(Endpoint ep) {
112         UnregisterEndpointInputBuilder inputBuilder = new UnregisterEndpointInputBuilder();
113         L2 l2Ep = new L2Builder().setL2Context(ep.getL2Context()).setMacAddress(ep.getMacAddress()).build();
114         inputBuilder.setL2(ImmutableList.of(l2Ep));
115         // TODO Li msunal this has to be rewrite when OFOverlay renderer will support l3-endpoints.
116         // Endpoint probably will not have l3-addresses anymore, because L2 and L3 endpoints should
117         // be registered separately.
118         if (ep.getL3Address() != null && !ep.getL3Address().isEmpty()) {
119             List<L3> l3Eps = new ArrayList<>();
120             for (L3Address ip : ep.getL3Address()) {
121                 l3Eps.add(new L3Builder().setL3Context(ip.getL3Context()).setIpAddress(ip.getIpAddress()).build());
122             }
123             inputBuilder.setL3(l3Eps);
124         }
125         return inputBuilder.build();
126     }
127
128     /**
129      * Create the updated {@link RegisterEndpointInput}, using the existing
130      * {@link Endpoint} and the new OfOverlay augmentation information (i.e.
131      * port name, NodeId, and NodeConnectorId).
132      *
133      * @param ep The existing {@link Endpoint} state
134      * @param portName The new port name in the OfOverlay augmentation
135      * @param nodeIdString The new NodeId in the OfOverlay augmentation
136      * @param nodeConnectorIdString The new NodeConnectorId in the OfOverlay augmentation
137      * @return The new {@link RegisterEndpointInput}, null if failure
138      */
139     public static RegisterEndpointInput createRegisterEndpointInput(Endpoint ep,
140             String portName, String nodeIdString, String nodeConnectorIdString) {
141
142         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId invNodeId =
143                 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId(nodeIdString);
144         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId ncId =
145                 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId(nodeConnectorIdString);
146
147         RegisterEndpointInputBuilder inputBuilder = new RegisterEndpointInputBuilder().setL2Context(
148                 ep.getL2Context())
149             .setMacAddress(new MacAddress(ep.getMacAddress()))
150             .setTenant(new TenantId(ep.getTenant()))
151             .setEndpointGroups(ep.getEndpointGroups())
152             .addAugmentation(OfOverlayContextInput.class,
153                     new OfOverlayContextInputBuilder()
154                             .setPortName(new Name(portName))
155                             .setNodeId(invNodeId)
156                             .setNodeConnectorId(ncId)
157                             .build())
158             .setTimestamp(System.currentTimeMillis());
159
160         // TODO Li msunal this getting of just first IP has to be rewrite when OFOverlay renderer
161         // will support l3-endpoints. Then we will register L2 and L3 endpoints separately.
162         if (ep.getNetworkContainment() != null) {
163             inputBuilder.setNetworkContainment(new SubnetId(ep.getNetworkContainment()));
164             L3Address l3Address = new L3AddressBuilder()
165                     .setIpAddress(ep.getL3Address().get(0).getIpAddress())
166                     .setL3Context(ep.getL3Address().get(0).getL3Context())
167                     .build();
168             inputBuilder.setL3Address(ImmutableList.of(l3Address));
169         }
170         return inputBuilder.build();
171     }
172
173 }