But as for you, you meant evil against me; but God meant it for good, in order to bring it about as it is this day, to save many people alive.
Recommended Reading John 14:1-6
An operating room in the midst of a serious surgery can be a disconcerting sight. Yet it is all for a purpose. There is a greater good and higher purpose -- correcting a problem that will lead to a long and fruitful life. The discomfort is planned and carried out on purpose, but it is only temporary.
Just so, God planned 400 years of discomfort for His people, the descendants of Abraham, in order to prepare them for their role as a light to the Gentiles. God planned this period of seclusion in Egypt and revealed it to Abraham years before it happened (Genesis 15:13-16). God was not punishing the descendants of Abraham, but rather protecting them when they were the family of Jacob in Canaan and in danger of being corrupted by pagan practices. In the midst of their 400 years in Egypt, the Hebrew slaves might have resented their situation -- and some lived and died in Egypt without knowing the greater purpose.
God always is working out His will for persons and nations. Don't let the troubles of the moment hide the glory of His purpose.