Created abstract super class for all COPS Message Senders as each contained duplicate...
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospdp / COPSPdpOSMsgSender.java
1 package org.umu.cops.ospdp;
2
3 import org.umu.cops.COPSMsgSender;
4 import org.umu.cops.stack.*;
5 import org.umu.cops.stack.COPSContext.RType;
6 import org.umu.cops.stack.COPSDecision.Command;
7 import org.umu.cops.stack.COPSDecision.DecisionFlag;
8 import org.umu.cops.stack.COPSHeader.Flag;
9
10 import java.io.IOException;
11 import java.net.Socket;
12 import java.util.*;
13
14 /**
15  * COPS message transceiver class for outsourcing connections at the PDP side.
16  *
17  * TODO - change all references of Vector to List<>
18  */
19 public class COPSPdpOSMsgSender extends COPSMsgSender {
20
21     /**
22      * Creates a COPSPepMsgSender
23      *
24      * @param clientType        COPS client-type
25      * @param clientHandle      Client handle
26      * @param sock              Socket to the PEP
27      */
28     public COPSPdpOSMsgSender(final short clientType, final COPSHandle clientHandle, final Socket sock) {
29         super(clientType, clientHandle, sock);
30     }
31
32     /**
33      * Sends a decision message which was requested by the PEP
34      * @param removeDecs    Decisions to be removed
35      * @param installDecs   Decisions to be installed
36      * @throws COPSPdpException
37      */
38     public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
39         sendDecision(removeDecs, installDecs, true);
40     }
41
42     /**
43      * Sends a decision message which was not requested by the PEP
44      * @param removeDecs    Decisions to be removed
45      * @param installDecs   Decisions to be installed
46      * @throws COPSPdpException
47      */
48     public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
49         sendDecision(removeDecs, installDecs, false);
50     }
51
52     /**
53      * Sends a decision message to the PEP
54      * @param removeDecs    Decisions to be removed
55      * @param installDecs   Decisions to be installed
56      * @param solicited     <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
57      * @throws COPSPdpException
58      */
59     public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {
60         // Common Header holding the same ClientType as the request
61         final Flag flag;
62         if (solicited)
63             flag= Flag.SOLICITED;
64         else
65             flag = Flag.UNSOLICITED;
66
67         final Map<COPSContext, Set<COPSDecision>> decisions = new HashMap<>();
68
69         // Decisions (no flags supplied)
70         //  <Context>
71         final COPSContext cntxt = new COPSContext(RType.CONFIG, (short)0);
72
73         // Remove Decisions
74         //  <Decision: Flags>
75         final COPSDecision rdec1;
76         if (installDecs.size() == 0)
77             rdec1 = new COPSDecision(Command.REMOVE);
78         else
79             rdec1 = new COPSDecision(Command.INSTALL);
80
81         if (decisions.get(cntxt) == null) {
82             final Set<COPSDecision> decisionSet = new HashSet<>();
83             decisionSet.add(rdec1);
84             decisions.put(cntxt, decisionSet);
85         } else {
86             decisions.get(cntxt).add(rdec1);
87         }
88
89         // Client Handle with the same clientHandle as the request
90         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(1, flag, getClientType(), _handle, decisions,
91                 null, null);
92
93         //** Send decision
94         //**
95         try {
96             decisionMsg.writeData(_sock);
97         } catch (IOException e) {
98             throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
99         }
100     }
101
102     /**
103      * Sends a message asking that the request state be deleted
104      * @throws   COPSPdpException
105      */
106     public void sendDeleteRequestState() throws COPSPdpException {
107         final Set<COPSDecision> decisionSet = new HashSet<>();
108         decisionSet.add(new COPSDecision(Command.REMOVE, DecisionFlag.REQSTATE));
109         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
110         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
111
112         // Common Header with the same ClientType as the request (default UNSOLICITED)
113         // Client Handle with the same clientHandle as the request
114         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
115
116         try {
117             decisionMsg.writeData(_sock);
118         } catch (IOException e) {
119             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
120         }
121     }
122
123     /**
124      * Method sendOpenNewRequestState
125      *
126      * @throws   COPSPdpException
127      *
128      */
129     public void sendOpenNewRequestState() throws COPSPdpException {
130         final Set<COPSDecision> decisionSet = new HashSet<>();
131         decisionSet.add(new COPSDecision(Command.INSTALL, DecisionFlag.REQSTATE));
132         final Map<COPSContext, Set<COPSDecision>> decisionMap = new HashMap<>();
133         decisionMap.put(new COPSContext(RType.CONFIG, (short)0), decisionSet);
134
135         // Common Header with the same ClientType as the request (default UNSOLICITED)
136         // Client Handle with the same clientHandle as the request
137         final COPSDecisionMsg decisionMsg = new COPSDecisionMsg(getClientType(), _handle, decisionMap, null, null);
138
139         try {
140             decisionMsg.writeData(_sock);
141         } catch (IOException e) {
142             throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
143         }
144     }
145
146     /**
147      * Sends a message asking for a COPS sync operation
148      * @throws COPSPdpException
149      */
150     public void sendSyncRequestState() throws COPSPdpException {
151         // Client Handle with the same clientHandle as the request
152         final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
153         try {
154             msg.writeData(_sock);
155         } catch (IOException e) {
156             throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());
157         }
158     }
159
160 }