49a24fce8e86c960bf1aafe3d8cb6df3b886fa9f
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpdp / COPSPdpReqStateMan.java
1 /*
2  * Copyright (c) 2004 University of Murcia.  All rights reserved.
3  * --------------------------------------------------------------
4  * For more information, please see <http://www.umu.euro6ix.org/>.
5  */
6
7 package org.umu.cops.prpdp;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.umu.cops.COPSStateMan;
12 import org.umu.cops.stack.*;
13
14 import java.net.Socket;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /**
19  * State manager class for provisioning requests, at the PDP side.
20  */
21 public class COPSPdpReqStateMan extends COPSStateMan {
22
23     private final static Logger logger = LoggerFactory.getLogger(COPSPdpReqStateMan.class);
24
25     /**
26      * Object for performing policy data processing
27      */
28     private final COPSPdpDataProcess _process;
29
30     /** COPS message transceiver used to send COPS messages */
31     protected transient COPSPdpMsgSender _sender;
32
33     /**
34      * Creates a request state manager
35      * @param clientType    Client-type
36      * @param clientHandle  Client handle
37      */
38     public COPSPdpReqStateMan(final short clientType, final COPSHandle clientHandle, final COPSPdpDataProcess process) {
39         super(clientType, clientHandle);
40         this._process = process;
41     }
42
43     @Override
44     protected void initRequestState(final Socket sock) throws COPSException {
45         // Inits an object for sending COPS messages to the PEP
46         _sender = new COPSPdpMsgSender(_clientType, _handle, sock);
47
48         // Initial state
49         _status = Status.ST_INIT;
50     }
51
52     /**
53      * Processes a COPS request
54      * @param msg   COPS request received from the PEP
55      * @throws COPSException
56      */
57     protected void processRequest(final COPSReqMsg msg) throws COPSException {
58         // TODO - Implement me - see commented out code from history prior to May 4, 2015...
59     }
60
61     /**
62      * Processes a report
63      * @param msg   Report message from the PEP
64      * @throws COPSException
65      */
66     protected void processReport(final COPSReportMsg msg) throws COPSException {
67         if (msg.getClientSI() != null) {
68             // Report Type
69             final COPSReportType rtypemsg = msg.getReport();
70             final Map<String, String> repSIs = new HashMap<>();
71             String strobjprid = "";
72             final COPSPrObjBase obj = new COPSPrObjBase(msg.getClientSI().getData().getData());
73             switch (obj.getSNum()) {
74                 case COPSPrObjBase.PR_PRID:
75                     strobjprid = obj.getData().str();
76                     break;
77                 case COPSPrObjBase.PR_EPD:
78                     // TODO FIXME - strobjprid is always empty here???
79                     repSIs.put(strobjprid, obj.getData().str());
80                     logger.info("PRID: " + strobjprid);
81                     logger.info("EPD: " + obj.getData().str());
82                     break;
83                 default:
84                     break;
85             }
86
87             //** Here we must act in accordance with
88             //** the report received
89             switch (rtypemsg.getReportType()) {
90                 case SUCCESS:
91                     _status = Status.ST_REPORT;
92                     _process.successReport(this, repSIs);
93                     break;
94                 case FAILURE:
95                     _status = Status.ST_REPORT;
96                     _process.failReport(this, repSIs);
97                     break;
98                 case ACCOUNTING:
99                     _status = Status.ST_ACCT;
100                     _process.acctReport(this, repSIs);
101                     break;
102             }
103         }
104     }
105
106     /**
107      * Called when connection is closed
108      * @param error Reason
109      * @throws COPSPdpException
110      */
111     public void processClosedConnection(final COPSError error) throws COPSException {
112         if (_process != null)
113             _process.notifyClosedConnection(this, error);
114
115         _status = Status.ST_CCONN;
116     }
117
118     /**
119      * Called when no keep-alive is received
120      * @throws COPSPdpException
121      */
122     public void processNoKAConnection() throws COPSException {
123         if (_process != null)
124             _process.notifyNoKAliveReceived(this);
125
126         _status = Status.ST_NOKA;
127     }
128
129     /**
130      * Deletes the request state
131      * @throws COPSPdpException
132      */
133     protected void finalizeRequestState() throws COPSException {
134         _sender.sendDeleteRequestState();
135         _status = Status.ST_FINAL;
136     }
137
138     /**
139      * Asks for a COPS sync
140      * @throws COPSPdpException
141      */
142     public void syncRequestState() throws COPSException {
143         _sender.sendSyncRequestState();
144         _status = Status.ST_SYNC;
145     }
146
147     /**
148      * Opens a new request state
149      * @throws COPSPdpException
150      */
151     protected void openNewRequestState() throws COPSException {
152         _sender.sendOpenNewRequestState();
153         _status = Status.ST_NEW;
154     }
155
156     /**
157      * Processes a COPS delete message
158      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PEP
159      * @throws COPSPdpException
160      */
161     public void processDeleteRequestState(final COPSDeleteMsg dMsg) throws COPSException {
162         if (_process != null)
163             _process.closeRequestState(this);
164
165         _status = Status.ST_DEL;
166     }
167
168 }