faaaa835231928a8f4db884d17e2eeb908874b51
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / translator / iaware / impl / NeutronLoadBalancerPoolChangeListener.java
1 /*
2  * Copyright (C) 2015 Red Hat, 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.ovsdb.openstack.netvirt.translator.iaware.impl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map.Entry;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronLoadBalancerPool;
21 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronLoadBalancerPoolMember;
22 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronLoadBalancer_SessionPersistence;
23 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_ID;
24 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronLoadBalancerPoolAware;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolHttps;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.Pools;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.pools.Pool;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.lbaasv2.rev150712.lbaas.attributes.pools.pool.members.Member;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.google.common.collect.ImmutableBiMap;
42
43 public class NeutronLoadBalancerPoolChangeListener implements DataChangeListener, AutoCloseable {
44     private static final Logger LOG = LoggerFactory.getLogger(NeutronLoadBalancerPoolChangeListener.class);
45
46     private static final ImmutableBiMap<Class<? extends ProtocolBase>,String> PROTOCOL_MAP
47             = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>,String>()
48             .put(ProtocolHttp.class, "HTTP")
49             .put(ProtocolHttps.class, "HTTPS")
50             .put(ProtocolIcmp.class, "ICMP")
51             .put(ProtocolTcp.class,"TCP")
52             .build();
53
54     private ListenerRegistration<DataChangeListener> registration;
55     private DataBroker db;
56
57     public NeutronLoadBalancerPoolChangeListener(DataBroker db){
58         this.db = db;
59         InstanceIdentifier<Pool> path = InstanceIdentifier
60                 .create(Neutron.class)
61                 .child(Pools.class)
62                 .child(Pool.class);
63         LOG.debug("Register listener for Neutron Load Balancer Pool model data changes");
64         registration =
65                 this.db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, AsyncDataBroker.DataChangeScope.ONE);
66     }
67
68     @Override
69     public void close() throws Exception {
70         registration.close();
71     }
72
73     @Override
74     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
75         LOG.trace("Data changes : {}", changes);
76
77         Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronLoadBalancerPoolAware.class, this);
78         createPool(changes, subscribers);
79         updatePool(changes, subscribers);
80         deletePool(changes, subscribers);
81     }
82
83     private void createPool(
84             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
85             Object[] subscribers) {
86         for (Entry<InstanceIdentifier<?>, DataObject> newPool : changes.getCreatedData().entrySet()) {
87                 if(newPool.getValue() instanceof Pool){
88                 NeutronLoadBalancerPool loadBalancerPool = fromMd((Pool) newPool.getValue());
89                 for (Object entry : subscribers) {
90                     INeutronLoadBalancerPoolAware subscriber = (INeutronLoadBalancerPoolAware) entry;
91                     subscriber.neutronLoadBalancerPoolCreated(loadBalancerPool);
92                 }
93                 }
94         }
95     }
96     private void updatePool(
97             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
98             Object[] subscribers) {
99         for (Entry<InstanceIdentifier<?>, DataObject> updatePool : changes.getUpdatedData().entrySet()) {
100                 if(updatePool.getValue() instanceof Pool){
101                 NeutronLoadBalancerPool loadBalancerPool = fromMd((Pool)updatePool.getValue());
102                 for(Object entry: subscribers){
103                     INeutronLoadBalancerPoolAware subscriber = (INeutronLoadBalancerPoolAware) entry;
104                     subscriber.neutronLoadBalancerPoolUpdated(loadBalancerPool);
105                 }
106                 }
107         }
108     }
109     private void deletePool(
110             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
111             Object[] subscribers) {
112         for (InstanceIdentifier<?> deletedPoolPath : changes.getRemovedPaths()) {
113                 if(deletedPoolPath.getTargetType().equals(Pool.class)){
114                 NeutronLoadBalancerPool loadBalancerPool = fromMd((Pool)changes.getOriginalData().get(deletedPoolPath));
115                 for(Object entry: subscribers){
116                     INeutronLoadBalancerPoolAware subscriber = (INeutronLoadBalancerPoolAware) entry;
117                     subscriber.neutronLoadBalancerPoolDeleted(loadBalancerPool);
118                 }
119                 }
120         }
121     }
122
123     /*
124      * This method is borrowed from NeutronLoadBalancerPool.java class of Neutron Northbound class.
125      * in the original location, this method is called extractFields.
126      * We will be utilizing similar code from other classes from the same package of neutron project.
127      */
128     private NeutronLoadBalancerPool fromMd(Pool pool) {
129         NeutronLoadBalancerPool result = new NeutronLoadBalancerPool();
130
131         result.setID(pool.getUuid().getValue());
132         if (pool.getTenantId() != null) {
133             result.setLoadBalancerPoolTenantID(pool.getTenantId().getValue());
134         }
135         if (pool.getName() != null) {
136             result.setLoadBalancerPoolName(pool.getName());
137         }
138         if (pool.getDescr() != null) {
139             result.setLoadBalancerPoolDescription(pool.getDescr());
140         }
141         if (pool.getProtocol() != null) {
142             result.setLoadBalancerPoolProtocol(PROTOCOL_MAP.get(pool.getProtocol()));
143         }
144         if (pool.getLbAlgorithm() != null) {
145             result.setLoadBalancerPoolLbAlgorithm(pool.getLbAlgorithm());
146         }
147
148         // TODO: setNeutronLoadBalancerPoolHealthMonitorID is a list? Fill in, when its needed.
149         if (pool.getHealthmonitorId() != null) {
150                 result.setNeutronLoadBalancerPoolHealthMonitorID(pool.getHealthmonitorId().getValue());
151         }
152
153         if (pool.isAdminStateUp() != null) {
154             result.setLoadBalancerPoolAdminStateIsUp(pool.isAdminStateUp());
155         }
156
157         List<Neutron_ID> listeners = new ArrayList();
158         if (pool.getListeners() != null) {
159             for (Uuid listenerUuid : pool.getListeners()) {
160                 listeners.add(new Neutron_ID(listenerUuid.getValue()));
161             }
162         }
163         result.setLoadBalancerPoolListeners(listeners);
164
165         if (pool.getSessionPersistence() != null) {
166             NeutronLoadBalancer_SessionPersistence sessionPersistence = new NeutronLoadBalancer_SessionPersistence();
167             sessionPersistence.setCookieName(pool.getSessionPersistence().getCookieName());
168             sessionPersistence.setType(pool.getSessionPersistence().getType());
169             result.setLoadBalancerSessionPersistence(sessionPersistence);
170         }
171
172         List<NeutronLoadBalancerPoolMember> loadBalancerPoolMembers = new ArrayList();
173         if (pool.getMembers() != null) {
174             for (Member member : pool.getMembers().getMember()) {
175                 NeutronLoadBalancerPoolMember neutronMember = new NeutronLoadBalancerPoolMember();
176
177                 neutronMember.setPoolID(pool.getUuid().getValue());
178                 neutronMember.setPoolMemberID(member.getUuid().getValue());
179
180                 // TODO: locate and populate remainder attributes, when its needed
181                 // member.setPoolMemberAddress(xxx);
182                 // member.setPoolMemberAdminStateIsUp(xxx);
183                 // member.setPoolMemberProtoPort(xxx);
184                 // member.setPoolMemberSubnetID(xxx);
185                 // member.setPoolMemberTenantID(xxx);
186                 // member.setPoolMemberWeight(xxx);
187
188                 loadBalancerPoolMembers.add(neutronMember);
189             }
190         }
191         result.setLoadBalancerPoolMembers(loadBalancerPoolMembers);
192
193         return result;
194     }
195 }