f30c694526a0e7cba50e397926efc19e2045a011
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / route / impl / src / main / java / com / highstreet / technologies / odl / app / impl / listener / ACMListener.java
1 /*
2  * Copyright © 2015 ZTE 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 com.highstreet.technologies.odl.app.impl.listener;
9
10 import com.highstreet.technologies.odl.app.impl.tools.BandwidthCalculator;
11 import com.highstreet.technologies.odl.app.impl.tools.DataBrokerHolder;
12 import com.highstreet.technologies.odl.app.impl.tools.NeExecutor;
13 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
14 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.ltp.path.rev170615.ThresholdOfPath;
15 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.ltp.path.rev170615.threshold.of.path.Threshold;
16 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.*;
17 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceConfiguration;
18 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.microwave.model.rev170324.mw.air._interface.pac.AirInterfaceStatus;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
24 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
25
26 /**
27  * Created by odl on 17-6-6.
28  */
29 public class ACMListener implements MicrowaveModelListener
30 {
31     public ACMListener(NeExecutor ne)
32     {
33         this.ne = ne;
34     }
35
36     private static final Logger LOG = LoggerFactory.getLogger(ACMListener.class);
37     private static Double bandwidth;
38
39     private final NeExecutor ne;
40
41     @Override
42     public void onAttributeValueChangedNotification(
43             AttributeValueChangedNotification notification)
44     {
45         new Thread(() ->
46         {
47             synchronized (ne)
48             {
49                 if (bandwidth == null)
50                 {
51                     readBandwidthThreshold();
52                 }
53                 try
54                 {
55                     if (notification.getAttributeName().equalsIgnoreCase("modulationCur"))
56                     {
57                         LOG.info("received notification of value changed of modulationCur, changed to " + notification.getNewValue());
58                         String lpId_airInterface = notification.getObjectIdRef().getValue();
59                         if (ne.isLtpOfThisOnPath(lpId_airInterface))
60                         {
61                             AirInterfaceConfiguration airInterfaceConfiguration = ne.getUnderAirPac(
62                                     lpId_airInterface, AirInterfaceConfiguration.class, CONFIGURATION);
63                             AirInterfaceStatus airInterfaceStatus = ne.getUnderAirPac(
64                                     lpId_airInterface, AirInterfaceStatus.class, OPERATIONAL);
65
66                             Double txCapacity = new BandwidthCalculator(
67                                     airInterfaceConfiguration.getTxChannelBandwidth(),
68                                     airInterfaceStatus.getModulationCur(),
69                                     airInterfaceStatus.getCodeRateCur()).calc();
70                             LOG.info("new txCapacity is " + txCapacity);
71                             if (txCapacity < bandwidth)
72                             {
73                                 LOG.info("new txCapacity is lower than bandwidth " + bandwidth);
74                                 ne.reportSwitch();
75                             }
76                         }
77                     }
78                 } catch (Exception e)
79                 {
80                     LOG.warn("handling attribute change: " + notification + " caught exception!", e);
81                 }
82             }
83         }).start();
84     }
85
86     private void readBandwidthThreshold()
87     {
88         ReadOnlyTransaction readOnlyTransaction = DataBrokerHolder.getDataBroker().newReadOnlyTransaction();
89         InstanceIdentifier<Threshold> instanceIdentifier = InstanceIdentifier.create(ThresholdOfPath.class).child(
90                 Threshold.class);
91         try
92         {
93             Threshold threshold = readOnlyTransaction.read(CONFIGURATION, instanceIdentifier).get().get();
94             bandwidth = threshold.getMinimumBandwidth().doubleValue();
95         } catch (Exception e)
96         {
97             LOG.warn("", e);
98         } finally
99         {
100             readOnlyTransaction.close();
101         }
102     }
103
104     @Override
105     public void onObjectCreationNotification(
106             ObjectCreationNotification notification)
107     {
108     }
109
110     @Override
111     public void onObjectDeletionNotification(
112             ObjectDeletionNotification notification)
113     {
114     }
115
116     @Override
117     public void onProblemNotification(
118             ProblemNotification notification)
119     {
120     }
121 }