Replaced use of logger abstraction COPSDebug.java to having this class log directly...
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / PCMMPepAgent.java
1 /**
2  @header@
3  */
4
5 package org.pcmm;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.umu.cops.prpep.COPSPepAgent;
10 import org.umu.cops.prpep.COPSPepConnection;
11 import org.umu.cops.prpep.COPSPepException;
12 import org.umu.cops.stack.*;
13
14 import java.io.IOException;
15 import java.net.ServerSocket;
16 import java.net.Socket;
17
18 /**
19  * This is a provisioning COPS PEP. Responsible for making connection to the PDP
20  * and maintaining it
21  */
22 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
23
24     public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
25
26     /** Well-known port for COPS */
27     public static final int WELL_KNOWN_CMTS_PORT = 3918;
28
29     /**
30      * PDP host IP
31      */
32     private ServerSocket serverSocket;
33
34     /**
35      * PDP host port
36      */
37     private int serverPort;
38
39     /**
40      * COPS error returned by PDP
41      */
42     private COPSError error;
43
44     /**
45      * Creates a PEP agent
46      *
47      * @param pepID
48      *            PEP-ID
49      * @param clientType
50      *            Client-type
51      */
52     public PCMMPepAgent(String pepID, short clientType) {
53         super(pepID, clientType);
54         serverPort = WELL_KNOWN_CMTS_PORT;
55     }
56
57     /**
58      * Creates a PEP agent with a PEP-ID equal to "noname"
59      *
60      * @param clientType
61      *            Client-type
62      */
63     public PCMMPepAgent(short clientType) {
64         super(clientType);
65         serverPort = WELL_KNOWN_CMTS_PORT;
66     }
67
68     /**
69      * Runs the PEP process XXX - not sure of the exception throwing
70      */
71     public void run() {
72         try {
73
74             logger.info("Create Server Socket on Port " + serverPort);
75
76             serverSocket = new ServerSocket(serverPort);
77             // Loop through for Incoming messages
78
79             // server infinite loop
80             while (true) {
81
82                 // Wait for an incoming connection from a PEP
83                 Socket socket = serverSocket.accept();
84
85                 logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
86
87                 processConnection(socket);
88                 /**
89                  * XXX - processConnection handles the open request from PEP And
90                  * a thread is created for conn = new
91                  * COPSPepConnection(_clientType, socket); the main processing
92                  * loop for PEP
93                  */
94
95             }
96         } catch (Exception e) {
97             logger.error("Error while processing the socket connection", e);
98         }
99     }
100
101     /**
102      * Establish connection to PDP's IP address
103      *
104      * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
105      * [<Integrity>]
106      *
107      * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
108      *
109      * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
110      * [<Integrity>]
111      *
112      * Not send [<Integrity>]
113      *
114      * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
115      *
116      * Not send [<PDPRedirAddr>], [<Integrity>]
117      *
118      * @throws IOException
119      * @throws COPSException
120      * @throws COPSPepException
121      *
122      */
123     private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
124         // Build OPN
125         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_OPN, getClientType());
126
127         COPSPepId pepId = new COPSPepId();
128         COPSData d = new COPSData(getPepID());
129         pepId.setData(d);
130
131         COPSClientOpenMsg msg = new COPSClientOpenMsg();
132         msg.add(hdr);
133         msg.add(pepId);
134
135         // Create Socket and send OPN
136         /*
137          * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
138          * Socket(addr,psPort);
139          */
140         logger.info("Send COPSClientOpenMsg to PDP");
141         msg.writeData(socket);
142
143         // Receive the response
144         logger.info("Receive the resposne from PDP");
145         COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
146
147         if (recvmsg.getHeader().isAClientAccept()) {
148             logger.info("isAClientAccept from PDP");
149             COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
150
151             // Support
152             if (cMsg.getIntegrity() != null) {
153                 throw new COPSPepException("Unsupported object (Integrity)");
154             }
155
156             // Mandatory KATimer
157             COPSKATimer kt = cMsg.getKATimer();
158             if (kt == null)
159                 throw new COPSPepException(
160                     "Mandatory COPS object missing (KA Timer)");
161             short _kaTimeVal = kt.getTimerVal();
162
163             // ACTimer
164             COPSAcctTimer at = cMsg.getAcctTimer();
165             short _acctTimer = 0;
166             if (at != null)
167                 _acctTimer = at.getTimerVal();
168
169             // Create the connection manager
170             COPSPepConnection conn = new COPSPepConnection(getClientType(),
171                     socket);
172             conn.setKaTimer(_kaTimeVal);
173             conn.setAcctTimer(_acctTimer);
174             logger.info("Thread(conn).start");
175             new Thread(conn).start();
176
177             return conn;
178         } else if (recvmsg.getHeader().isAClientClose()) {
179             logger.info("isAClientClose from PDP");
180             COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
181             error = cMsg.getError();
182             socket.close();
183             return null;
184         } else { // messages of other types are not expected
185             throw new COPSPepException(
186                 "Message not expected. Closing connection for "
187                 + socket.toString());
188         }
189     }
190
191     /**
192      * Gets the COPS error returned by the PDP
193      *
194      * @return <tt>COPSError</tt> returned by PDP
195      */
196     public COPSError getConnectionError() {
197         return error;
198     }
199
200     public void setConnectionError(COPSError _error) {
201         this.error = _error;
202     }
203
204     /**
205      * @return the serverSocket
206      */
207     public ServerSocket getServerSocket() {
208         return serverSocket;
209     }
210
211     /**
212      * @param serverSocket
213      *            the serverSocket to set
214      */
215     public void setServerSocket(ServerSocket serverSocket) {
216         this.serverSocket = serverSocket;
217     }
218
219     /**
220      * @return the serverPort
221      */
222     public int getServerPort() {
223         return serverPort;
224     }
225
226     /**
227      * @param serverPort
228      *            the serverPort to set
229      */
230     public void setServerPort(int serverPort) {
231         this.serverPort = serverPort;
232     }
233
234 }