Created abstract super class for all COPS Message Senders as each contained duplicate...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / prpdp / COPSPdpMsgSender.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.umu.cops.COPSMsgSender;
10 import org.umu.cops.stack.*;
11 import org.umu.cops.stack.COPSContext.RType;
12 import org.umu.cops.stack.COPSDecision.Command;
13 import org.umu.cops.stack.COPSDecision.DecisionFlag;
14 import org.umu.cops.stack.COPSObjHeader.CType;
15
16 import java.io.IOException;
17 import java.net.Socket;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 /**
24  * COPS message transceiver class for provisioning connections at the PDP side.
25  *
26  * TODO - Need to continue refactoring by removing all instances of Hashtable (change to Map<>)
27  */
28 public class COPSPdpMsgSender extends COPSMsgSender {
29
30     /**
31      * Creates a COPSPepMsgSender
32      *
33      * @param clientType        COPS client-type
34      * @param clientHandle      Client handle
35      * @param sock              Socket to the PEP
36      */
37     public COPSPdpMsgSender (final short clientType, final COPSHandle clientHandle, final Socket sock) {
38         super(clientType, clientHandle, sock);
39     }
40
41     /**
42      * Sends a decision message
43      * @param removeDecs    Decisions to be removed
44      * @param installDecs   Decisions to be installed
45      * @throws COPSPdpException
46      */
47     public void sendDecision(final Map<String, String> removeDecs, Map<String, String> installDecs)
48             throws COPSPdpException {
49
50         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
51
52         // Decisions (no flags supplied)
53         //  <Context>
54         final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
55
56         // Remove Decisions
57         //  <Decision: Flags>
58         final COPSDecision rdec1 = new COPSDecision(Command.REMOVE);
59
60         final Set<COPSDecision> decisionSet = new HashSet<>();
61         decisionSet.add(rdec1);
62         decisionMap.put(cntxt, decisionSet);
63
64         for (final Map.Entry<String, String> entry : removeDecs.entrySet()) {
65             //  <Named Decision Data: Provisioning> (PRID)
66             final COPSPrID prid = new COPSPrID();
67             prid.setData(new COPSData(entry.getKey()));
68             final COPSDecision decisionPrid = new COPSDecision(CType.NAMED,
69                     new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
70
71             decisionMap.get(cntxt).add(decisionPrid);
72
73             final COPSPrEPD epd = new COPSPrEPD();
74             final COPSDecision decisionPrepd = new COPSDecision(CType.NAMED,
75                     new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
76
77             decisionMap.get(cntxt).add(decisionPrepd);
78         }
79
80         // Install Decisions
81         //  <Decision: Flags>
82         final COPSDecision idec1 = new COPSDecision(Command.INSTALL);
83         decisionMap.get(cntxt).add(idec1);
84
85         for (final Map.Entry<String, String> entry : installDecs.entrySet()) {
86             //  <Named Decision Data: Provisioning> (PRID)
87             final COPSPrID prid = new COPSPrID();
88             prid.setData(new COPSData(entry.getKey()));
89             final COPSDecision decisionPrid2 = new COPSDecision(CType.NAMED,
90                     new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
91
92             decisionMap.get(cntxt).add(decisionPrid2);
93         }
94
95         // Common Header with the same ClientType as the request
96         // Client Handle with the same clientHandle as the request
97         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
98
99         //** Send the decision
100         try {
101             decisionMsg.writeData(_sock);
102         } catch (IOException e) {
103             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
104         }
105     }
106
107     /**
108      * Sends a decision message which was not requested by the PEP
109      * @param removeDecs    Decisions to be removed
110      * @param installDecs   Decisions to be installed
111      * @throws COPSPdpException
112      */
113     public void sendUnsolicitedDecision(final Map<String, String> removeDecs, final Map<String, String> installDecs)
114             throws COPSPdpException {
115         // Common Header with the same ClientType as the request
116         // Client Handle with the same clientHandle as the request
117         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
118         // Decisions (no flags supplied)
119         //  <Context>
120         final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
121
122         // Remove Decisions
123         //  <Decision: Flags>
124         final Set<COPSDecision> decisionSet = new HashSet<>();
125         decisionSet.add(new COPSDecision(Command.REMOVE));
126         decisionMap.put(cntxt, decisionSet);
127
128         for (final Map.Entry<String, String> entry : removeDecs.entrySet()) {
129             //  <Named Decision Data: Provisioning> (PRID)
130             final COPSPrID prid = new COPSPrID();
131             prid.setData(new COPSData(entry.getKey()));
132             decisionMap.get(cntxt).add(new COPSDecision(CType.NAMED,
133                     new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
134
135             //  <Named Decision Data: Provisioning> (EPD)
136             final COPSPrEPD epd = new COPSPrEPD();
137             epd.setData(new COPSData(entry.getValue()));
138             decisionMap.get(cntxt).add(
139                     new COPSDecision(CType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
140         }
141
142         // Install Decisions
143         //  <Decision: Flags>
144         decisionMap.get(cntxt).add(new COPSDecision(Command.INSTALL));
145
146         for (final Map.Entry<String, String> entry : installDecs.entrySet()) {
147             //  <Named Decision Data: Provisioning> (PRID)
148             final COPSPrID prid = new COPSPrID();
149             prid.setData(new COPSData(entry.getKey()));
150             decisionMap.get(cntxt).add(new COPSDecision(CType.NAMED,
151                     new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
152
153             final COPSPrEPD epd = new COPSPrEPD();
154             epd.setData(new COPSData(entry.getValue()));
155             decisionMap.get(cntxt).add(
156                     new COPSDecision(CType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
157         }
158
159         /**
160         COPSIntegrity intr = new COPSIntegrity();
161         intr.setKeyId(19);
162         intr.setSeqNum(9);
163         intr.setKeyDigest(new COPSData("KEY DIGEST"));
164         decisionMsg.add(intr);
165         /**/
166
167         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
168
169         //** Send the decision
170         try {
171             decisionMsg.writeData(_sock);
172         } catch (IOException e) {
173             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
174         }
175     }
176
177     /**
178      * Sends a message asking that the request state be deleted
179      * @throws   COPSPdpException
180      */
181     public void sendDeleteRequestState() throws COPSPdpException {
182         final COPSDecision dec = new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE);
183         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
184         final Set<COPSDecision> decisionSet = new HashSet<>();
185         decisionSet.add(dec);
186         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
187
188         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
189         try {
190             decisionMsg.writeData(_sock);
191         } catch (IOException e) {
192             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
193         }
194     }
195
196     /**
197      * Sends a request asking that a new request state be created
198      * @throws   COPSPdpException
199      */
200     public void sendOpenNewRequestState() throws COPSPdpException {
201         final COPSDecision dec = new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE);
202         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
203         final Set<COPSDecision> decisionSet = new HashSet<>();
204         decisionSet.add(dec);
205         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
206
207         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(_clientType, _handle, decisionMap, null, null);
208
209         try {
210             decisionMsg.writeData(_sock);
211         } catch (IOException e) {
212             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
213         }
214     }
215
216     /**
217      * Sends a message asking for a COPS sync operation
218      * @throws COPSPdpException
219      */
220     public void sendSyncRequestState() throws COPSPdpException {
221         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
222         try {
223             msg.writeData(_sock);
224         } catch (IOException e) {
225             throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());
226         }
227     }
228 }